Ansible Playbook Task Gets Skipped

ansible-playbook

I have 3 aws ec2 instances, 2 are ubuntu and 1 amazon linux 2. I have configured the master slave setup and works fine.

Master Instance:

NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Slave 1 Instance:

NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Slave 2 Instance:

NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

I'm trying to install HTTPD package on both slaves using the below playbook.

---
- hosts: test
  become: true
  tasks:
     - name: Install the latest version of Apache on CentOS
       yum: name=httpd state=present
       when: ansible_os_family == "Amazon Linux"

     - name: install the latest version of Apache on Debian
       apt: name=httpd state=present
       when: ansible_os_family == "Ubuntu"

The tasks gets skipped. Output below

PLAY [test] *********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [172.31.4.19]
ok: [172.31.38.171]

TASK [Install the latest version of Apache on CentOS] ***************************************************************
skipping: [172.31.38.171]
skipping: [172.31.4.19]

TASK [install the latest version of Apache on Debian] ***************************************************************
skipping: [172.31.38.171]
skipping: [172.31.4.19]

PLAY RECAP **********************************************************************************************************
172.31.38.171              : ok=1    changed=0    unreachable=0    failed=0
172.31.4.19                : ok=1    changed=0    unreachable=0    failed=0

I'm not sure how else to write this playbook. I tried using the generic 'Package' module instead of 'YUM' and 'APT' but that failed too, hence decided to split it. I do understand that I can break the code into separate files and then store them in Roles/Tasks directory and then use the 'Include' module. But I wanted to find out if the above method is possible to install a package on multiple OS family in one playbook. Thanks!

Best Answer

Most probably the tasks get skipped because the value of ansible_os_family is neither Ubuntu nor Amazon Linux

Add the following task:

- name: show family
  debug:
    var: ansible_os_family

This will print out the actual value on the instances you need to use in your playbook.

On my Ubuntu machines, for example, the value of ansible_os_family is Debian. On my CentOS machines it is RedHat.

For a more detailed value of the Linux distribution you can use ansible_facts.distribution, which is set to Ubuntu on my Ubuntu machines and CentOS on my CentOS machines.

Related Topic