Ansible 使用 setup 取得 Managed node 的 facts

2018-07-25 09:52 更新

使用 Playbooks 时,Ansible 会自动执行 setup module 以收集各个 Managed node 的 facts。对冻仁而言这个 facts 就如同系统变数般的存在,从 IP 位址、作业系统、CPU 等资讯应有尽有。

automate_with_ansible_practice-18.jpg

官方文件介紹如下:

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.

Ad-Hoc Commands

冻仁一般会先使用 Ad-Hoc Commands 呼叫 setup 看看有哪些用的上的资讯,这对撰写些较复杂的 Playbooks 时是很有用的。

底下冻仁将使用 chusiang/ansible-managed-node:ubuntu-14.04 这个 Docker image 作为 Managed node。

  1. 借由 less 快速搜寻所有的变数。1

    $ 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",
    :
    
  2. 搭配 filter 将发行版本 (distribution) 的资讯给过滤出来。

    $ 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
    }
    
  3. 取得套件管理员的种类资讯,此例取得的值为 apt

    $ ansible all -m setup -a "filter=ansible_pkg_mgr"
    server1 | SUCCESS => {
       "ansible_facts": {
           "ansible_pkg_mgr": "apt"
       },
       "changed": false
    }
    

撰写跨 Linux distribution 的 Playbooks

取得 ansible_pkg_mgr 后,我们可以 Playbooks 里加个判断式使用。

  1. 建立支持 Debian, Ubuntu, CentOS 安装 Vim 的 playbook。

    $ 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 :
    
  2. 执行 playbook,且当条件不符时,即会跳过该 task。。

    $ 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
    

后话

setup module 是每位 Ansibler 一定会接触到的!我们可以善加利用它,并与各种需求搭配使用,这样会让 Playbooks 更具弹性且易于维护。

相关连结

1. 在 less 里我们可使用 / + 关键字 + Enter 进行搜寻。 ↩


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号