1

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)

icmtf
  • 11
  • 1
  • 4

2 Answers2

0

(ansible 2.7.9)

The conditions work as expected for me. The tasks below

  vars:
    ansible_net_image: "bootflash:/isr4300-universalk9.03.16.04b.S.155-3.S4b-ext.SPA.bi"
  tasks:
    - set_fact:
        compliant_image: "16.04"
    - debug: msg="INFO - Compliant image"
      when: ansible_net_image is search(compliant_image)
    - debug: msg="WARNING - Not compliant image"
      when: ansible_net_image is not search(compliant_image)

    - set_fact:
        compliant_image: "16.05"
    - debug: msg="INFO - Compliant image"
      when: ansible_net_image is search(compliant_image)
    - debug: msg="WARNING - Not compliant image"
      when: ansible_net_image is not search(compliant_image)

give

PLAY [localhost] *******************************************************************************************

TASK [set_fact] ********************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************
ok: [localhost] => {
    "msg": "INFO - Compliant image"
}
TASK [debug] ***********************************************************************************************
skipping: [localhost]

TASK [set_fact] ********************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************
skipping: [localhost]

TASK [debug] ***********************************************************************************************
ok: [localhost] => {
    "msg": "WARNING - Not compliant image"
}
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • So there's basically no chance to have warning or info value in `compliance_output.stdout` based on the if condition. After all Ansible isn't a pure programming language. – icmtf Aug 09 '19 at 20:54
0

I managed to reach semi-satisfactionary solution with this:

# playbook.yml

---                                                                                                               
- name: task1                                                                                                     
  hosts: all                                                                                                      
  gather_facts: no                                                                                                
  tasks:                                                                                                          
    - name: Get facts                                                                                             
      ios_facts:                                                                                                  
        gather_subset: all                                                                                        
    - name: Define variable (non)                                                                                 
      set_fact:                                                                                                   
        compliant_image: "{{ param1 }}"                                                                           
    - 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)                                                      
    - debug: var=compliance_output                                                                                
    - name: Define variable (non)                                                                                 
      set_fact:                                                                                                   
        compliant_image: "{{ param1 }}"                                                                           
    - name: Print information if ok                                                                               
      shell: echo "INFO"                                                                                          
      register: compliance_output                                                                                 
      when: ansible_net_image is search(compliant_image)                                                          
    - debug: var=compliance_output 

Passing the param1 from the CLI: ansible-playbook playbook.yml -e "param1=16.03" triggers the first condition and skips the second one.

Thank you Vladimir once again! Had to sleep with it to kinda understand Ansible's way of handling variables.

icmtf
  • 11
  • 1
  • 4