ansible playbook脚本获取系统版本信息
执行环境说明
脚本执行过程中实测系统为 redhat6、redhat7 suse11 suse12;
[root@ansible_host ~]# ansible --version
ansible 2.9.18
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible-2.9.18-py2.7.egg/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Sep 12 2018, 05:31:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
1、主机列表文件(1.txt)内容
[redhat]
192.168.254.7 ansible_ssh_user=xp ansible_ssh_pass=yourpasswd ansible_sudo_pass=yourpasswd ansible_python_interpreter=/usr/bin/python
192.168.254.76 ansible_ssh_user=xp ansible_ssh_pass=yourpasswd ansible_sudo_pass=yourpasswd ansible_python_interpreter=/usr/bin/python
[suse]
192.168.254.111 ansible_ssh_user=xp ansible_ssh_pass=yourpasswd ansible_sudo_pass=yourpasswd ansible_python_interpreter=/usr/bin/python
192.168.254.125 ansible_ssh_user=xp ansible_ssh_pass=yourpasswd ansible_sudo_pass=yourpasswd ansible_python_interpreter=/usr/bin/python
2、playbook(t.yml)内容
---
- hosts: all
gather_facts: yes
tasks:
- name: get_os
debug:
msg: "IP: {{ inventory_hostname }}; System version: {{ansible_distribution_file_variety}} {{ansible_distribution_version}}"
3、执行结果
[root@ansible_host ~]# ansible-playbook t.yml -i 1.txt
PLAY [all] *************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************
ok: [192.168.254.125]
ok: [192.168.254.76]
ok: [192.168.254.7]
ok: [192.168.254.111]
TASK [get_os] **********************************************************************************************************************************************
ok: [192.168.254.111] => {
"msg": "IP: 192.168.254.111; System version: SUSE 11.3"
}
ok: [192.168.254.125] => {
"msg": "IP: 192.168.254.125; System version: SUSE 12.2"
}
ok: [192.168.254.7] => {
"msg": "IP: 192.168.254.7; System version: RedHat 7.5"
}
ok: [192.168.254.76] => {
"msg": "IP: 192.168.254.76; System version: RedHat 6.5"
}
PLAY RECAP *************************************************************************************************************************************************
192.168.254.111 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.254.125 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.254.7 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.254.76 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0