基于收集到的事实的ansif if-else条件

时间:2019-08-09 17:59:40

标签: ansible logic

基于从IOS主机收集到的事实,我想看/学一个if-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 

这就是我得到的:

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

据我了解,任务:Print information if ok应该具有值:INFO - Compliant image。 我想使用这些信息或进一步的逻辑,但似乎还没有获得Ansible的变量逻辑

2 个答案:

答案 0 :(得分:0)

(可使用2.7.9)

条件符合我的预期。以下任务

  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)

给予

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

答案 1 :(得分:0)

我设法达到了半满意的解决方案:

# 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 

从CLI传递param1ansible-playbook playbook.yml -e "param1=16.03"触发第一个条件,而跳过第二个条件。

再次感谢弗拉基米尔!必须睡一会才能了解Ansible处理变量的方式。