13. 怎麼使用 setup 取得 Managed node 的 facts?
This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host. Ansible provides many facts about the system, automatically.
Copy $ ansible all -m setup | less
server1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.19.0.2"
],
"ansible_all_ipv6_addresses": [
"fe80::42:acff:fe13:2"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "03/14/2014",
"ansible_bios_version": "1.00",
"ansible_cmdline": {
"com.docker.database": "com.docker.driver.amd64-linux",
"com.docker.driver": "com.docker.driver.amd64-linux,",
"console": "ttyS0",
"earlyprintk": "serial",
"mobyplatform": "mac",
"ntp": "gateway"
},
"ansible_date_time": {
"date": "2016-12-13",
:
Copy $ ansible all -m setup -a "filter=ansible_distribution*"
server1 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"ansible_distribution_major_version": "14",
"ansible_distribution_release": "trusty",
"ansible_distribution_version": "14.04"
},
"changed": false
}
Copy $ ansible all -m setup -a "filter=ansible_pkg_mgr"
server1 | SUCCESS => {
"ansible_facts": {
"ansible_pkg_mgr": "apt"
},
"changed": false
}
撰寫跨 Linux distribution 的 Playbooks
Copy $ vim setup_vim.yml
---
- name: Setup the vim
hosts: all
become: true
tasks:
# Debian, Ubuntu.
- name: install apt packages
apt: name=vim state=present
when: ansible_pkg_mgr == "apt"
# CentOS.
- name: install yum packages
yum: name=vim-minimal state=present
when: ansible_pkg_mgr == "yum"
# vim:ft=ansible :
Copy $ ansible-playbook setup_vim.yml
PLAY [Setup the vim] **************************************************
TASK [setup] **********************************************************
ok: [server1]
TASK [install apt packages] *******************************************
changed: [server1]
TASK [install yum packages] *******************************************
skipping: [server1]
PLAY RECAP ************************************************************
server1 : ok=2 changed=1 unreachable=0 failed=0