Ansible when语句带有另一个任务的变量

时间:2018-06-26 18:45:07

标签: ansible .when

有人可以帮我吗? 我想在主机“ cfme_tester-0”上创建一个文件夹。 为此,我从“ undercloud-0”主机检查变量“ osp_version”,然后根据结果,我应该在“ cfme_tester-0”主机上创建一个文件夹。

这是我的剧本:

- name: take openstack version
  hosts: undercloud-0
  become: true
  become_user: stack
  tasks:
    - name: creating flavor
      shell: |
        source /home/stack/stackrc
        cat /etc/rhosp-release | egrep -o '[0-9]+' | head -1
      register: osp_version
      ignore_errors: True
    - debug: msg="{{ osp_version.stdout }}"


- name: set up CFME tester
  hosts: cfme_tester-0
  become: yes
  become_user: root
  tasks:
    - name: Run prepare script for OSP10
      debug:
        shell: |
          cd /tmp/cfme/ && mkdir osp10
      when: "'10' in osp_version.stdout"

    - name: Run prepare script for OSP13
      debug:
        shell: |
          cd /tmp/cfme/ && mkdir osp13
      when: "'13' in osp_version.stdout"

但是发生错误:

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [undercloud-0] => {
    "msg": "10"
}

PLAY [set up CFME tester] *****************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [cfme_tester-0]

TASK [Run prepare script for OSP10] *******************************************************************************************************************************************************************************
fatal: [cfme_tester-0]: FAILED! => {"msg": "The conditional check ''10' in osp_version.stdout' failed. The error was: error while evaluating conditional ('10' in osp_version.stdout): 'osp_version' is undefined\n\nThe error appears to have been in '/root/infrared/rhos-qe-core-installer/playbooks/my_setup.yaml': line 20, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: Run prepare script for OSP10\n      ^ here\n"}
    to retry, use: --limit @/root/infrared/rhos-qe-core-installer/playbooks/my_setup.retry

PLAY RECAP ********************************************************************************************************************************************************************************************************
cfme_tester-0              : ok=1    changed=0    unreachable=0    failed=1   
undercloud-0               : ok=3    changed=1    unreachable=0    failed=0   

1 个答案:

答案 0 :(得分:1)

变量是每个主机的变量(否则,当您在多个主机上运行任务并register一个变量时会发生什么?)。在第一个任务中,您要为主机osp_version设置undercloud-0变量。

如果您想在第二场剧中使用此变量(正在cfme_tester-0上运行),则应该阅读Ansible文档的Magic Variables, and How To Access Information About Other Hosts部分。您将需要通过hostvars字典来引用该变量,因此您的第二次播放将类似于:

- name: set up CFME tester
  hosts: cfme_tester-0
  become: yes
  become_user: root
  tasks:
    - name: Run prepare script for OSP10
      shell: |
        cd /tmp/cfme/ && mkdir osp10
      when: "'10' in hostvars['undercloud-0'].osp_version.stdout"

    - name: Run prepare script for OSP13
      shell: |
        cd /tmp/cfme/ && mkdir osp13
      when: "'13' in hostvars['undercloud-0'].osp_version.stdout"

...但是请注意,如果您仅创建目录,最好使用file模块代替:

    - name: Run prepare script for OSP10
      file:
        path: /tmp/cfme/osp10
        state: directory
      when: "'10' in hostvars['undercloud-0'].osp_version.stdout"