如何创建 Ansible playbook 以获取远程主机的操作系统版本? [英] how to create Ansible playbook to obtain OS versions of the remote hosts?
问题描述
我是 ansible 的新手.我有一个要求,要求我为 AWS 中托管的 450 多个 linux 服务器提取操作系统版本.AWS 不提供此功能 - 而是建议我们从 puppet 或 Chef 那里获取它.
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 playbook 以获取远程主机的操作系统版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!