Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can someone help me to understand why this when condition is failing.

Below is my Playbook:

  - 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

Error:

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 version: 2.9

question from:https://stackoverflow.com/questions/66050841/ansible-when-condition-failing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

I do not see any problem in your play section if you have gather_facts: true there.

However, I see many newbie guys overwriting this while disabling it in the ansible.cfg file.

Please check below in your ansible.cfg file, it should be the reason.

gathering               = False

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...