【问题标题】:Ansible set_facts based on OS version not working基于操作系统版本的 Ansible set_facts 不起作用
【发布时间】:2020-03-14 22:33:04
【问题描述】:

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

谢谢

【问题讨论】:

  • 虽然这不是你问的,但你想要的不是set_fact:install_patch_name: centos{{ ansible_distribution_major_version }}-updates吗?
  • 此外,拥有近 12k 的声誉,您肯定知道最好不要在不包含日志或 debug: var=ansible_distribution_major_version 的输出的情况下发布这样的问题,这将使某人能够,您知道,真正帮助您跨度>
  • @mdaniel,道歉不包括错误消息。现在添加它。
  • 所需的信息(我在下面的答案中猜到了)将是完整的剧本结果,每个人都可以看到每个 set_fact 操作实际上被跳过了。

标签: ansible


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 2019-06-09
    • 2015-08-26
    • 2012-02-05
    • 2020-05-11
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多