Issue
I'm using Ansible: 2.2.0.0
I have 3 machines:
Two vagrant boxes (one CentOS 7.x and one Ubuntu 14.04) and
3rd box is an EC2 Amazon Linux instance (Amazon Linux AMI release
2016.03).
On these boxes, I'm running the following command and getting valid output (as listed below):
CentOS:
[vagrant@myvagrant ~] $ ansible all -m setup -i "`hostname`," --connection=local -a "filter=ansible_distribution*"
myvagrant | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.2.1511"
},
"changed": false
}
Ubuntu:
vagrant@myubuntuvagrant:~$ ansible all -m setup -i "`hostname`," --connection=local -a "filter=ansible_distribution*"
myubuntuvagrant | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"ansible_distribution_major_version": "14",
"ansible_distribution_release": "trusty",
"ansible_distribution_version": "14.04"
},
"changed": false
}
vagrant@myubuntuvagrant:~$
Amazon EC2 instance/box:
$ ansible all -m setup -i "`hostname`," --connection=local -a "filter=ansible_distribution*"
ip-10-200-1-145 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Amazon",
"ansible_distribution_major_version": "NA",
"ansible_distribution_release": "NA",
"ansible_distribution_version": "2016.03"
},
"changed": false
}
In one of my Ansible playbook / templates/yum.repos.d.file.j2 file, I'm using {{ ansible_distribution_major_version }}
variable and using it's value in a .repo file for the baseurl
property's value for CentOS/Amazon EC2 instance only i.e. when: ansible_distribution == "CentOS" or ansible_distribution == "Amazon"
.
baseurl=https://packagecloud.io/company/packages/telegraf/el/6/$basearch
PS: I'm not looking for Ubuntu (as that part is working fine with using apt-get in my playbook for both setting the apt-get source list and installing the package).
My question:
Why ansible facter variable is not setting any valid valid for ansible_distribution_major_release
version for Amazon EC2 instance? What facter_*
/ansible_*
can I use which will work in all 3 OS types.
PS: When I used baseurl
's value with ../el/6/..
in it (inside the yum.repos.d/target-pacakge.amazon-os.repo file), yum install worked fine for installing the package on Amazon linux box (though, using ../el/7/..
in baseurl didn't work). See here for more details: https://packagecloud.io/docs#os_distro_version (under heading: Enterprise Linux (CentOS, RedHat, Amazon Linux))
Solution
If you use following, set_fact, you don't have to specifically handle ansible_distribution_major_version in tasks for all three OS types.
pre_tasks:
- set_fact: ansible_distribution_major_version=6
when: ansible_distribution == "Amazon" and ansible_distribution_major_version == "NA"
Answered By - Vikas Aggarwal