Amazon Linux machine - Ansible ansible_distribution* variables major release distribution
Asked Answered
A

2

7

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

Accountant answered 10/1, 2017 at 8:41 Comment(0)
P
7

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"
Pelt answered 12/1, 2017 at 9:6 Comment(2)
Yep, that's true but how would I know whether it's 6 or 7? If it's 7 then yum repo file baseurl won't work using ..\el\6\.. in it's path, right.Accountant
@ArunSangal you'd have to do a bit of further parsing and comparing. For Amazon Linux 2, related to EL7, the version is just "2". Original amazon linux had versions in the form of "YYYY.MM". I know 2018.03 corresponds to EL6. I can't speak to earlier versions, as I never used them before now.Freeholder
S
2

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
Spruill answered 22/11, 2018 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.