如何创建Ansible剧本以获取远程主机的OS版本? [英] how to create Ansible playbook to obtain OS versions of the remote hosts?
问题描述
我是新来的ansible.我有一项要求,要求我为AWS托管的450多个Linux服务器安装OS版本. AWS不提供此功能-而是建议我们从木偶或厨师那里获取它.
I'm new to ansible. I have a requirement that requires me to pull OS version for of more than 450 linux severs hosted in AWS. AWS does not provide this feature - it rather suggests us to get it from puppet or chef.
我创建了一些无法运行的简单剧本
I created few simple playbooks which does not run
---
- hosts: testmachine
user: ec2-user
sudo: yes
tasks:
- name: Update all packages to latest
yum: name=* state=latest
task:
- name: obtain OS version
shell: Redhat-release
playbook应该输出带有主机名和操作系统版本的文本文件.任何对此的见解将受到高度赞赏.
playbook should output a text file with hostname and OS version. Any insight on this will be highly appreciated.
推荐答案
使用以下Jinja2表达式之一:
Use one of the following Jinja2 expressions:
{{ hostvars[inventory_hostname].ansible_distribution }}
{{ hostvars[inventory_hostname].ansible_distribution_major_version }}
{{ hostvars[inventory_hostname].ansible_distribution_version }}
其中:
-
hostvars
和ansible_...
内置并由Ansible自动收集 -
ansible_distribution
是Ansible正在处理的主机
hostvars
andansible_...
are built-in and automatically collected by Ansibleansible_distribution
is the host being processed by Ansible
例如,假设您正在对运行CentOS 7发行版的主机host.example.com
运行Ansible角色test_role
:
For example, assuming you are running the Ansible role test_role
against the host host.example.com
running a CentOS 7 distribution:
---
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution }}"
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution_major_version }}"
- debug:
msg: "{{ hostvars[inventory_hostname].ansible_distribution_version }}"
会给您:
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "CentOS"
}
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "7"
}
TASK [test_role : debug] *******************************************************
ok: [host.example.com] => {
"msg": "7.5.1804"
}
这篇关于如何创建Ansible剧本以获取远程主机的OS版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!