代码之家  ›  专栏  ›  技术社区  ›  sfgroups

基于操作系统版本的Ansible集合事实不起作用

  •  0
  • sfgroups  · 技术社区  · 4 年前

    我想根据操作系统版本设置修补程序。在Ansible版本2.8中提出了这个剧本。但它的付出 The task includes an option with an undefined variable. 调试行中的错误消息。

    ---
    - hosts: all
      gather_facts: yes
      vars:
          patch_name_8: 'centos8-updates'
          patch_name_7: 'centos7-updates'
      tasks:
        - name: Set fact for CentOS 7
          set_fact:
            install_patch_name: "{{ patch_name_7 }}"
          when: ansible_distribution_major_version == 7
    
        - name: Set fact for CentOS 8
          set_fact:
            install_patch_name: "{{ patch_name_8 }}"
          when: ansible_distribution_major_version == 8
    
        - name: patch name display
          debug:
            msg: "install {{ install_patch_name }}"
    

    如何设置 install_patch_name

    添加错误消息:

    TASK [patch name display] ************************************************************************************************************
    fatal: [host01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'install_patch_name' is undefined\n\nThe error appears to be in 't.yaml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n
    - name: patch name display\n      ^ here\n"}
    

    0 回复  |  直到 4 年前
        1
  •  2
  •   Zeitounator Vladimir Botka    4 年前

    TLDR;

    使用

    when: ansible_distribution_major_version == "8"
    

    when: ansible_distribution_major_version | int == 8
    

    解释

    centos:8 docker图像。

    您正在处理的事实作为字符串返回:

    [root@f6408271fc8c ~]# ansible localhost -m setup -a filter=ansible_distribution_major_version
    localhost | SUCCESS => {
        "ansible_facts": {
            "ansible_distribution_major_version": "8"
        },
        "changed": false
    }
    

    变量在使用比较时保留其类型,如果需要,则需要正确地进行类型转换,如下面的剧本所示。

    ---
    - hosts: localhost
    
      tasks:
        - name: default compare
          debug:
            msg: Comparison is true
          when: ansible_distribution_major_version == 8
    
        - name: compare as strings
          debug:
            msg: Comparison is true
          when: ansible_distribution_major_version == "8"
    
        - name: compare as ints
          debug:
            msg: Comparison is true
          when: ansible_distribution_major_version | int == 8
    
    

    [root@f6408271fc8c ~]# ansible-playbook play.yml 
    
    PLAY [localhost] **************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
    ok: [localhost]
    
    TASK [default compare] ********************************************************************************************************************************************************************************************
    skipping: [localhost]
    
    TASK [compare as strings] *****************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "msg": "Comparison is true"
    }
    
    TASK [compare as ints] ********************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "msg": "Comparison is true"
    }
    
    PLAY RECAP ********************************************************************************************************************************************************************************************************
    localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0