Sunday 15 January 2012

Ansible: understanding a compound conditional when statement -



Ansible: understanding a compound conditional when statement -

consider trivial ansible playbook , associated output below. why task 5 executed? these tasks run against debian. task 1 fails expected. so, why and'ing 'ansible_lsb.major_release|int < 14' create true? have operator precedence?

-jk

--- - name: these tests run against debian hosts: frontend001 vars: - bcbio_dir: /mnt/bcbio - is_ubuntu: "'{{ansible_distribution}}' == 'ubuntu'" - is_debian: "'{{ansible_distribution}}' == 'debian'" tasks: - name: 1. expect skip because test is_ubuntu debug: msg="ansible distribution - {{ansible_distribution}}, release - {{ansible_distribution_release}}, {{ ansible_lsb.major_release }}" when: is_ubuntu - name: 2. expect print msg because test is_debian debug: msg="ansible distribution - {{ansible_distribution}}, release - {{ansible_distribution_release}}, {{ ansible_lsb.major_release }}" when: is_debian - name: 3. expect print msg because release 7 of wheezy debug: msg="ansible distribution - {{ansible_distribution}}, release - {{ansible_distribution_release}}, {{ ansible_lsb.major_release }}" when: ansible_lsb.major_release|int < 14 - name: 4. expect print msg because true , true true debug: msg="ansible distribution - {{ansible_distribution}}, release - {{ansible_distribution_release}}, {{ ansible_lsb.major_release }}" when: is_debian , ansible_lsb.major_release|int < 14 - name: 5. expect skip because false , true false debug: msg="ansible distribution - {{ansible_distribution}}, release - {{ansible_distribution_release}}, {{ ansible_lsb.major_release }}" when: is_ubuntu , ansible_lsb.major_release|int < 14 $ ansible-playbook -i ~/.elasticluster/storage/ansible-inventory.jkcluster zbcbio.yml play [these tests run against debian] ***************************************** gathering facts *************************************************************** ok: [frontend001] task: [1. expect skip because test is_ubuntu] ********************************* skipping: [frontend001] task: [2. expect print msg because test is_debian] ************************* ok: [frontend001] => { "msg": "ansible distribution - debian, release - wheezy, 7" } task: [3. expect print msg because release 7 of wheezy] ******************** ok: [frontend001] => { "msg": "ansible distribution - debian, release - wheezy, 7" } task: [4. expect print msg because true , true true] ****************** ok: [frontend001] => { "msg": "ansible distribution - debian, release - wheezy, 7" } task: [5. expect skip because false , true false] ********************* ok: [frontend001] => { "msg": "ansible distribution - debian, release - wheezy, 7" } play recap ******************************************************************** frontend001 : ok=5 changed=0 unreachable=0 failed=0

edited: listing changes based on tedder42's reply below in case next along @ home.

1) changed

- is_ubuntu: "'{{ansible_distribution}}' == 'ubuntu'"

to

- is_ubuntu: "{{ansible_distribution == 'ubuntu'}}"

2) change

when: is_ubuntu , ansible_lsb.major_release|int < 14

to

when: is_ubuntu|bool , ansible_lsb.major_release|int < 14

that did it!

-jk

tldr: variable beingness output string, not evaluated. prepare evaluation using jinja2 , filter var |bool.

debugging

you're missing 1 thing debug problem. here's ran on local osx box:

- name: stackoverflow 26188055 hosts: local vars: - bcbio_dir: /mnt/bcbio - is_ubuntu: "'{{ansible_distribution}}' == 'ubuntu'" - is_debian: "'{{ansible_distribution}}' == 'debian'" tasks: - debug: var=is_ubuntu - debug: var=is_debian - debug: msg="this shows conditional passes though shouldnt" when: is_ubuntu , true

and output:

task: [debug var=is_ubuntu] *************************************************** ok: [127.0.0.1] => { "is_ubuntu": "'macosx' == 'ubuntu'" } task: [debug var=is_debian] *************************************************** ok: [127.0.0.1] => { "is_debian": "'macosx' == 'debian'" } task: [debug msg="this shows conditional passes though shouldnt"] *** ok: [127.0.0.1] => { "msg": "this shows conditional passes though shouldnt" } evaluating

as far know, can't evaluate downwards boolean. typically done unrolling variable (placing in every "when"). however, can accomplished can boolean string, cast bool as hinted on variables ansible page (search "boolean value").

- name: stackoverflow 26188055 hosts: local vars: - bcbio_dir: /mnt/bcbio - is_ubuntu: "{{ansible_distribution == 'ubuntu'}}" - is_debian: "{{ansible_distribution == 'debian'}}" tasks: - debug: var=is_ubuntu - debug: var=is_debian - debug: msg="this shows conditional passes though shouldnt" when: is_ubuntu|bool , true

and here's output.

task: [debug var=is_ubuntu] *************************************************** ok: [127.0.0.1] => { "is_ubuntu": "false" } task: [debug var=is_debian] *************************************************** ok: [127.0.0.1] => { "is_debian": "false" } task: [debug msg="this shows conditional passes though shouldnt"] *** skipping: [127.0.0.1] version comparing filter

note may want take advantage of ansible's version_compare filter. usage left exercise reader.

ansible ansible-playbook

No comments:

Post a Comment