Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Linux machine - Ansible ansible_distribution* variables major release distribution

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

like image 265
AKS Avatar asked Jan 10 '17 08:01

AKS


Video Answer


2 Answers

Here's some relevant values from an Amazon Linux 2 docker image:

# docker images|grep amazon
amazonlinux                             2                   d656eea421ba        4 weeks ago         162MB
amazonlinux                             latest              d656eea421ba        4 weeks ago         162MB

# docker run --init --rm -it amazonlinux:2 cat /etc/os-release
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/"

After installing ansible within the docker image:
# ansible --version
ansible 2.7.2

# Relevant items from output of 'ansible -m setup'
ansible_distribution: Amazon
ansible_distribution_file_parsed: true
ansible_distribution_file_path: /etc/system-release
ansible_distribution_file_variety: Amazon
ansible_distribution_major_version: NA
ansible_distribution_release: NA
ansible_distribution_version: 2
ansible_os_family: RedHat
ansible_pkg_mgr: yum
ansible_service_mgr: sysvinit  # should this be 'systemd'?
ansible_system_vendor: NA
ansible_virtualization_role: guest
ansible_virtualization_type: docker

and on an actual EC2 Amazon Linux 2 instance:

[root@ip-xxx ~]# ansible --version
ansible 2.7.2

ansible_os_family: RedHat
ansible_pkg_mgr: yum
ansible_service_mgr: systemd
ansible_system: Linux
ansible_system_vendor: Xen
ansible_virtualization_role: guest
ansible_virtualization_type: xen
like image 21
Marc Tamsky Avatar answered Oct 19 '22 09:10

Marc Tamsky


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"
like image 67
Vikas Aggarwal Avatar answered Oct 19 '22 09:10

Vikas Aggarwal