当条件失败时 ansible

ansible when condition failing

谁能帮我理解为什么这个 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

如果您有 gather_facts: true,我认为您的播放部分没有任何问题。

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

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

gathering               = False