Dan
Dan

Reputation: 67

Ansible when statement with variable from another task

can someone help me please? I want to create a folder at host "cfme_tester-0". For this I check variable "osp_version" from "undercloud-0" host and based on the result I should create a folder at the "cfme_tester-0" host.

Here is my playbook:

- 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"

But an error occurs:

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   

Upvotes: 0

Views: 4135

Answers (1)

larsks
larsks

Reputation: 312630

Variables are per-host (because otherwise, what happens when you run a task on multiple hosts and register a variable?). In your first task, you are setting the osp_version variable for host undercloud-0.

If you want to use this variable in your second play, which is running on cfme_tester-0, then you should read the Magic Variables, and How To Access Information About Other Hosts section of the Ansible documentation. You'll need to refer to the variable via the hostvars dictionary, so your second play will look like:

- 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"

...but note that if you're just creating a directory, you would be better off just using the file module instead:

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

Upvotes: 1

Related Questions