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

**URL:** <https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 21, 2020, 7:18pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363 "2020-12-21T19:18:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_Boeira](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@Thiago\_Boeira](https://forum.ansible.com/u/Thiago_Boeira)\
**Post date:** [December 21, 2020, 7:18pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363/1 "2020-12-21T19:18:43Z")

</div>

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:

```auto
---
- 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](mailto:tcboeira@gmail.com) |

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 21, 2020, 7:38pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363/2 "2020-12-21T19:38:25Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Thiago\_Boeira](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@Thiago\_Boeira](https://forum.ansible.com/u/Thiago_Boeira)\
**Post date:** [December 21, 2020, 7:42pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363/3 "2020-12-21T19:42:16Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 21, 2020, 7:55pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363/4 "2020-12-21T19:55:15Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;- name: Show distribution information  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OS family: {{ ansible\_os\_family }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Distribution: {{ ansible\_distribution }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Release: {{ ansible\_distribution\_release }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version: {{ ansible\_distribution\_version }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Major version: {{ ansible\_distribution\_major\_version }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Architecture: {{ ansible\_architecture }}

For a single fact:

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_os\_family

So for the condition you can use for example is

&nbsp;&nbsp;&nbsp;&nbsp;- name: Run silly command  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "ls -l /home"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ansible\_distribution == 'Ubuntu'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ansible\_distribution\_version == '18.04'

(The list implies AND for all conditions).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Thiago\_Boeira](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@Thiago\_Boeira](https://forum.ansible.com/u/Thiago_Boeira)\
**Post date:** [January 7, 2021, 3:02pm UTC](https://forum.ansible.com/t/ansible-how-to-condition-execution-of-the-version-of-the-operating-system/34363/5 "2021-01-07T15:02:26Z")

</div>

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 ?!
