I would like to see/learn a practical example of if-else logic in ansible based on the fact I gathered from the IOS host. For example if the running firmware version is higher do that, if not - do something else.
- name: task1
hosts: all
gather_facts: no
tasks:
- name: Get facts
ios_facts:
gather_subset: all
- name: Define variable (version of compliant image version)
set_fact:
compliant_image: "16.04"
- debug:
var: ansible_net_image
- name: Print warning if not compliant
shell: echo "WARNING - Non compliant image! Should be {{ compliant_image }}"
register: compliance_output
when: ansible_net_image is not search(compliant_image)
- name: Print information if ok
shell: echo "INFO - Compliant image"
register: compliance_output
when: ansible_net_image is search(compliant_image)
- debug: var=compliance_output
This is what I get:
TASK [Define variable (version of compliant image version)] ******
ok: [My_IOS_Host]
TASK [debug] *****************************************************
ok: [My_IOS_Host] => {
"ansible_net_image": "bootflash:/isr4300-universalk9.03.16.04b.S.155-3.S4b-ext.SPA.bi"
}
TASK [Print warning if not compliant] ****************************
changed: [My_IOS_Host]
TASK [Print information if ok] ***********************************
skipping: [My_IOS_Host]
TASK [debug] *****************************************************
ok: [My_IOS_Host] => {
"compliance_output": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
To my understanding the Task: Print information if ok
should have a value: INFO - Compliant image
.
I would like to use that information or further logic but it seems that I don't get Ansible's variables logic (yet)