5

我正在使用Ansible:2.2.0.0

我有3台机器:

两个 vagrant 盒子(一个 CentOS 7.x 和一个 Ubuntu 14.04)和
第三个盒子是 EC2 Amazon Linux 实例(Amazon Linux AMI 版本 2016.03)。

在这些框中,我正在运行以下命令并获得有效输出(如下所示):

中央操作系统

[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 实例/盒子

$ 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
}

在我的 Ansible playbook / templates/yum.repos.d.file.j2 文件之一中,我使用{{ ansible_distribution_major_version }}变量并在 .repo 文件中使用它的baseurl值作为 CentOS/Amazon EC2 实例的属性值when: ansible_distribution == "CentOS" or ansible_distribution == "Amazon".

baseurl=https://packagecloud.io/company/packages/telegraf/el/6/$basearch

PS:我不是在寻找 Ubuntu(因为在我的剧本中使用 apt-get 来设置 apt-get 源列表和安装软件包时,这部分工作正常)。



我的问题:为什么 ansible facter 变量没有为 Amazon EC2 实例的版本
设置任何有效的有效版本?ansible_distribution_major_release我可以使用什么facter_*/ansible_*可以在所有 3 种操作系统类型中使用哪个。

PS:当我在其中使用baseurl's 值../el/6/..时(在 yum.repos.d/target-pacakge.amazon-os.repo 文件中), yum install 可以很好地在 Amazon linux 机器上安装软件包(不过,../el/7/..在 baseurl中使用没用)。有关更多详细信息,请参见此处https ://packagecloud.io/docs#os_distro_version (在标题下:Enterprise Linux(CentOS、RedHat、Amazon Linux)

4

2 回答 2

6

如果您使用以下set_fact,则不必在所有三种操作系统类型的任务中专门处理ansible_distribution_major_version

pre_tasks:
- set_fact: ansible_distribution_major_version=6
  when: ansible_distribution == "Amazon" and ansible_distribution_major_version == "NA"
于 2017-01-12T09:06:30.983 回答
2

以下是来自 Amazon Linux 2 docker 映像的一些相关值:

# 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

在实际的 EC2 Amazon Linux 2 实例上:

[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
于 2018-11-22T01:01:40.500 回答