基于操作系统版本的Ansible set_facts不起作用

时间:2020-03-14 22:33:04

标签: ansible

我想根据操作系统版本设置补丁。想到了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"}

谢谢

1 个答案:

答案 0 :(得分:3)

TLDR;

使用

when: ansible_distribution_major_version == "8"

when: ansible_distribution_major_version | int == 8

说明

注意:以下所有示例都是针对centos:8泊坞窗映像播放的。

您所照顾的事实以字符串形式返回:

[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