【问题标题】:Create Playbook with a few tasks and another playbook使用一些任务和另一个剧本创建剧本
【发布时间】:2020-12-15 15:37:16
【问题描述】:

目前我正在寻找一种解决方案,我可以在其中创建一个变量,然后使用该变量导入 Playbook。

这是我的代码:

- name: test
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
    playbook1: server_provision
    playbook2: server_provision_oracle
  gather_facts: no
  tasks:
  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook1 }}"
    when: OS == "RHEL"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "RHEL"

  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook2 }}"
    when: OS == "Oracle"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "Oracle"


- name: test
  vars_files:
    - /tmp/server_provision.yml
  import_playbook: "{{ server_provision }}.yaml"

但是当我执行剧本时,我得到了错误,变量 server_provision 没有价值。

问题是我的 unimportet 剧本没有被执行,它从 importet 开始。

你有解决办法吗?

【问题讨论】:

    标签: ansible yaml ansible-awx ansible-tower


    【解决方案1】:

    您可以使用 pre_tasks。 Pre Taks 在任务部分之前执行。 Ansible Docs

    类似这样的:

    ---
      hosts: localhost
      vars:
        ansible_python_interpreter: /usr/bin/python3
        playbook1: server_provision
        playbook2: server_provision_oracle
      gather_facts: no
    
      vars_files:
        - /tmp/server_provision.yml
    
      pre_tasks:
      - name: define Variable with when condition
        set_fact:
          server_provision: "{{ playbook1 }}"
        when: OS == "RHEL"
    
      - name: store variable to file
        copy:
          content: "server_provision: {{ server_provision }}"
          dest: /tmp/server_provision.yml
        when: OS == "RHEL"
    
      - name: define Variable with when condition
        set_fact:
          server_provision: "{{ playbook2 }}"
        when: OS == "Oracle"
    
      - name: store variable to file
        copy:
          content: "server_provision: {{ server_provision }}"
          dest: /tmp/server_provision.yml
        when: OS == "Oracle"
    
      tasks:
        - name: test
          import_playbook: "{{ server_provision }}.yaml"
    

    【讨论】:

    • 您在什么时候收到此错误?在 include 之后还是在 pre_tasks 中?
    • 在 pre_tasks 之后。在他应该包括剧本的任务中
    • 您可以使用-v 命令行开关获取更多信息。更多 -vvvv = 更详细的输出
    • 我明白了;为用户 SSH 建立 SSH 连接:EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with -mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o -o ConnectTimeout=10 -o ControlPath=/tmp/awx_1428_h8t3shy7/cp/16f8e91cd -tt '/bin/sh -c /usr/bin/python3 / tmp/ansible-tmp-1608049946.7190742-16657-173060652088279/AnsiballZ_import_playbook.py /home//.ansible/tmp/ansible-tmp-1608049946.7190742-16657-173060652088279/args'''0'''
    • 与 XXXX 的共享连接已关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多