Ansible - How to condition execution of the version of the Operating System.

Friends, Good afternoon!

Well, first of all, I didn’t know where to get help.

Then, here at VOL I didn’t find a correct classification, so, if necessary,

I reissue it to send it correctly. Well, as the title of the message says, “Ansible - How to condition execution of the version of the Operating System”, I am starting the application of Ansible in our park.

Today we have about 90/100 Linux workstations, where most are of different flavors. The idea, after what happened around the security around the world, is to unify their management, management, support and maintenance.

Before taking it to the production field, I created a LAB with a separate network in order to simulate our park as well as foresee some needs and of course, train, why are these “YML” files boring hehehehe

The LAB is as follows:

192.168.129.242 - server - Ubuntu 18.04

192.168.129.125 - client 1 - Ubuntu 18.04

192.168.129.109 - client 2 - Ubuntu 20.04

On this mainstay, I carried out a series of tests, and adapting everything right. BUT I crashed just in the penultimate test, which is: If it’s a version of Ubuntu do X, if it’s another version, do Y

My “teste.yml” file is as follows:

---
- hosts: all
  remote_user: meuuser
  become: true
  gather_facts: True
  tasks:
  - name: "TESTE TESTE TESTE TESTE TESTE TESTE"
    ansible.builtin.debug:
    var: ansible_facts
    command: "init 6"
    when: ansible_facts['distribution_version'] == "18.04"

  - name: "2 2 2 2 2 2 2 2 2"
    ansible.builtin.debug:
    var: ansible_facts
    command: "ls -l /home"
    when: ansible_facts['distribution_version'] == "20.04"

Notably, it has changed in some items, as I added it as examples that I found on the Internet, but all without success.
This error always occurs to me:

$ ansible-playbook playbooks/playbook_019.yml -i inventario/inventory

/usr/lib/python2.7/dist-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 the next release.

  from cryptography.exceptions import InvalidSignature

 [WARNING]: Ignoring invalid attribute: var

 [WARNING]: Ignoring invalid attribute: ansible.builtin.debug

PLAY [all] 
**********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] 
**********************************************************************************************************************************************************************************************************************
ok: [192.168.129.125]

 [WARNING]: Module invocation had junk after the JSON data: AttributeError("module 'platform' has no attribute 'dist'")

ok: [192.168.129.109]

TASK [2 2 2 2 2 2 2 2 2] 
********************************************************************************************************************************************************************************************************************

fatal: [192.168.129.109]: FAILED! => {"msg": "The conditional check 
'ansible_facts['distribution'] == \"Ubuntu\"' failed. The error was: 
error while evaluating conditional (ansible_facts['distribution'] == 
\"Ubuntu\"): 'dict object' has no attribute 'distribution'\n\nThe error 
appears to have been in 
'/home/administrador/ansible/playbooks/playbook_019.yml': line 10, 
column 5, but may\nbe elsewhere in the file depending on the exact 
syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: \"2 2
 2 2 2 2 2 2 2\"\n    ^ here\n"}

changed: [192.168.129.125]

TASK [3 3 3 3 3 3 3 3 3 3 3 3] 
**************************************************************************************************************************************************************************************************************
skipping: [192.168.129.125]

PLAY RECAP 
**********************************************************************************************************************************************************************************************************************************

192.168.129.109            : ok=1    changed=0    unreachable=0    failed=1   
192.168.129.125            : ok=2    changed=1    unreachable=0    failed=0   

The error message always appears, although for client 1 - Ubuntu 18.04 - it always runs. As for 2, no.
The version of Ansible is:

administrador@SERVER01:~/ansible$ ansible --version

/usr/lib/python2.7/dist-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 the next release.
  from cryptography.exceptions import InvalidSignature
ansible 2.5.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/administrador/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0] 

Well, I've read and re-read the tool's documentation. I've simulated without making mistakes, dozens of tests - all without success.
I tried to search for a light on the entire Internet, and was unsuccessful. Evidently it must be my search failure, but I continue to search and test without success.

So I come to ask friends for help.

A big hug, and I'm available for more details.

Thanks in advance!

Thiago Boeira | Redes & TIC | CREA/RS 200288 | E-mail: tcboeira@gmail.com |

Friends, Good afternoon!

Well, first of all, I didn't know where to get help.

Then, here at VOL I didn't find a correct classification, so, if necessary,

I reissue it to send it correctly. Well, as the title of the message says, "Ansible - How to condition execution of the
version of the Operating System", I am starting the application of Ansible in our park.

Today we have about 90/100 Linux workstations, where most are of different flavors. The idea, after what happened around
the security around the world, is to unify their management, management, support and maintenance.

Before taking it to the production field, I created a LAB with a separate network in order to simulate our park as well
as foresee some needs and of course, train, why are these "YML" files boring hehehehe

The LAB is as follows:
192.168.129.242 - server - Ubuntu 18.04
192.168.129.125 - client 1 - Ubuntu 18.04
192.168.129.109 - client 2 - Ubuntu 20.04

On this mainstay, I carried out a series of tests, and adapting everything right. BUT I crashed just in the penultimate
test, which is: If it's a version of Ubuntu do X, if it's another version, do Y

My "teste.yml" file is as follows:

----hosts:allremote_user:meuuserbecome:truegather_facts:Truetasks:-name:"TESTE TESTE TESTE TESTE TESTE
TESTE"ansible.builtin.debug:var:ansible_factscommand:"init 6"when:ansible_facts['distribution_version']=="18.04"-name:"2
2 2 2 2 2 2 2 2"ansible.builtin.debug:var:ansible_factscommand:"ls -l
/home"when:ansible_facts['distribution_version']=="20.04"

Notably, it has changed in some items, as I added it as examples that I found on the Internet, but all without

success.This error always occurs to me:

$ ansible-playbook playbooks/playbook_019.yml -i inventario/inventory
/usr/lib/python2.7/dist-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 the
next release.
from cryptography.exceptions import InvalidSignature
[WARNING]: Ignoring invalid attribute: var

[WARNING]: Ignoring invalid attribute: ansible.builtin.debug
PLAY [all]

Your syntax is completely wrong, your are mixing debug with command and there is no indentation at all.

Regards
     Racke

Sorry, the moment I paste it in the body of the email, it stays in this format. I apologize for that … But there is, yes, identification. I’ll try to write instead of pasting, to see if it helps …

Sorry, the moment I paste it in the body of the email, it stays in this format. I apologize for that ...But there is,
yes, identification.I'll try to write instead of pasting, to see if it helps ....

---
- hosts: all
remote_user: myuser
become: true
gather_facts: True
tasks:
- name: "TESTE TESTE TESTE TESTE TESTE TESTE"
ansible.builtin.debug:
var: ansible_facts
command: "init 6"
when: ansible_facts['distribution_version'] == "18.04"

- name: "2 2 2 2 2 2 2 2 2"
ansible.builtin.debug:
var: ansible_facts
command: "ls -l /home"
when: ansible_facts['distribution_version'] == "20.04"

Yes, but you are still mixing things and don't use proper indentation.

This is useful to determine the facts for the OS distribution on the target:

    - name: Show distribution information
      debug:
        msg: |
          OS family: {{ ansible_os_family }}
          Distribution: {{ ansible_distribution }}
          Release: {{ ansible_distribution_release }}
          Version: {{ ansible_distribution_version }}
          Major version: {{ ansible_distribution_major_version }}
          Architecture: {{ ansible_architecture }}

For a single fact:

    - debug:
        var: ansible_os_family

So for the condition you can use for example is

    - name: Run silly command
      command: "ls -l /home"
      when:
        - ansible_distribution == 'Ubuntu'
        - ansible_distribution_version == '18.04'

(The list implies AND for all conditions).

Regards
          Racke

Well, thank you very much for the guidance.

I sincerely apologize for the delay in responding to this message, but adding to the holiday season still work commitments.

Well, back to my topic: And I ask to forget the indentation since when I put it here it deforms …

In an eventual playbook file, is it all within it ?!

I apologize but I still have difficulty understanding - and running a test …

So, minimally, collecting what you tell me, would that be ?!