【问题标题】:ansible when condition failing条件失败时的ansible
【发布时间】:2021-02-04 17:33:53
【问题描述】:

谁能帮我理解为什么这个when 条件失败了。

下面是我的剧本:

  - name: restart pkgd once finally for RHEL6 and RHEL7 systems
    service:
      name: pkgd
      enabled: yes
      state: restarted
    when: ansible_os_family == 'RedHat' and ansible_distribution_major_version|int >= 7

错误:

TASK [restart pkgd once finally for RHEL6 and RHEL7 systems] ******************************************************************************************************
task path: /home/user/tr_vdi.yml:250
fatal: [myhost]: FAILED! => {}

MSG:

The conditional check 'ansible_os_family == 'RedHat' and ansible_distribution_major_version|int >= 7' failed. The error was: error while evaluating conditional (ansible_os_family == 'RedHat' and ansible_distribution_major_version|int >= 7): 'ansible_os_family' is undefined

Ansible Ad hoc:

devSanbox$ ansible myhost -m setup -a "filter=ansible_distribution_*"
/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  from cryptography.exceptions import InvalidSignature
  myhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_search_string": "Red Hat",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "7",
        "ansible_distribution_release": "Maipo",
        "ansible_distribution_version": "7.8",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

devSanbox$ ansible myhost -m setup -a "filter=ansible_os_family*"
  from cryptography.exceptions import InvalidSignature
  myhost | SUCCESS => {
    "ansible_facts": {
        "ansible_os_family": "RedHat",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false

ansible 版本:2.9

【问题讨论】:

  • 手动运行setup 模块将为您提供事实。但是你玩gather_facts吗?
  • @seshadri_c,是的,我的剧本有 gather_facts: true

标签: ansible ansible-2.x


【解决方案1】:

如果你有gather_facts: true,我在你的播放部分看不到任何问题。

但是,我看到许多新手在 ansible.cfg 文件中禁用它时覆盖了它。

请在您的ansible.cfg 文件中检查以下内容,应该是这个原因。

gathering               = False

【讨论】:

  • 非常感谢,我很挣扎,你准确地指出了问题所在。我在 ansible.cfg 中做到了false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 2017-01-03
  • 1970-01-01
  • 2016-09-09
相关资源
最近更新 更多