在 Cumulus Linux 上收集 Ansible Facts

本文概述了在实验室环境中使用 Ansible 收集有关 Cumulus Linux 交换机的信息(Ansible 称之为 facts)的过程,其中 Ansible 在与交换机位于同一网络的物理服务器或虚拟机上运行。

要求

如何从 CLI 收集 Facts

  1. 确保主机名在 DNS 中。Ping Cumulus Linux 交换机的 DNS 主机名

     user at server in ~
     $ ping sw1
     PING sw1 (192.168.100.11) 56(84) bytes of data.
     64 bytes from sw1 (192.168.100.11): icmp_req=1 ttl=64 time=0.197 ms
     64 bytes from sw1 (192.168.100.11): icmp_req=2 ttl=64 time=0.163 ms
     ^C
     --- sw1 ping statistics ---
     2 packets transmitted, 2 received, 0% packet loss, time 1000ms
     rtt min/avg/max/mdev = 0.163/0.180/0.197/0.017 ms
     user at server in ~
     $
    
  2. 使用 Ansible setup 命令,其中 sw1 是您的 switch1 的 DNS 名称,其中 -m 表示您选择运行的模块,--ask-pass 提示您输入密码(大多数自动化环境使用 SSH 密钥进行身份验证而不是密码),-vvvv 为您提供所有调试信息(不是必需的,但确实有助于您进行故障排除),而 -u root 使用户成为 root 而不是您在运行 Ansible 的主机设备上的用户名。

     ansible sw1 -m setup --ask-pass -vvvv -u root
    
  3. Ansible 使用提供的用户(在本例中为 root)和提供的密码连接到交换机。这应该与用户通过 SSH 连接到交换机的确切方式相同。如果能够连接,Ansible 将运行 setup 模块并收集有关 Cumulus 交换机的 facts。

收集哪些 Facts?

Ansible 收集许多 facts。本文使用运行 Cumulus Linux 2.0.1 的 DNI et-7448bf 型号作为示例设置。此处突出显示的一些 facts 对于编写 playbook 很重要。您可以在下方找到 setup 命令的完整结果。

Cumulus Linux 的版本。

  • setup 命令返回什么?

     "ansible_lsb": {
    "description": "2.0.1-fffbbda-201403232243-final",
     "id": "\"Cumulus Networks\"",
     "major_release": "2",
     "release": "2.0.1"
     },
    
  • 一个利用变量的示例

    - name: install image file (installs to opposite slot)
       command: /usr/cumulus/bin/cl-img-install -f http://192.168.100.1/{{ image }}
       when: ansible_lsb.release not in image
    

playbook 中的上述任务可以使用 setup 命令中收集的任何 fact(除非有目的地关闭,否则在任何 playbook 上自动运行)。此示例利用 setup 中获得的 ansible_lsb.release 变量,并将其与示例 playbook 的特定变量 {{image}} 进行比较。此 playbook 将为此任务升级交换机,使用 cl-img-install 命令。创建条件语句,使用 “when:” 可以防止您浪费时间进行安装,除非运行此 playbook 的用户给出的镜像与交换机上当前运行的镜像 {{ansible_lsb.release}} 不同。

主机名

  • setup 命令返回什么?

     "ansible_hostname": "sw1",
    
  • 一个利用变量的示例

    - name: configure MOTD with version # for user
       lineinfile: dest=/etc/motd regexp='^sw.*bin$' line='{{ ansible_hostname }} - running version {{ image }}' backrefs=yes
       register: result
    

此示例利用主机名以及内部变量 {{image}},Ansible setup 收集 facts 时不会返回该变量。上述任务使用当前主机名和交换机正在运行的镜像更新 Cumulus Linux 交换机上的 MOTD。一个示例是:sw1 - running version CumulusLinux-2.0.1-powerpc.bin

管理 MAC 地址

  • setup 命令返回什么?

      "ansible_eth0": { 
        "active": true, 
        "device": "eth0", 
        "ipv4": {
          "address": "192.168.100.11",
          "netmask": "255.255.255.0",
          "network": "192.168.100.0" 
        },
         "ipv6": [ 
          {
          "address": "fe80::4638:39ff:fe00:3410", 
          "prefix": "64", 
          "scope": "link"
         }
         ],
         "macaddress": "44:38:39:00:34:10",
         "mtu": 1500,
         "promisc": false,
         "type": "ether"
       },
    
  • 一个利用变量的示例

    - name: writing report to /var/log/mac-script.log
       shell: "echo {{ansible_hostname}}: Script Completed Successfully at {{ansible_date_time.time}} - Version {{ansible_lsb.release}} MAC for eth0 is {{ansible_etho.macaddress}} >> /tmp/upgrade-script.log"
       delegate_to: 127.0.0.1
    

此任务的示例输出为 "sw1: Script Completed Successfully at 18:53:24 - Version 2.0.1 MAC for eth0 is 44:38:39:00:34:10"。上面的脚本将该文本输出到它创建的名为 upgrade-script.log 的文件中,该文件位于 /tmp/ 目录中。

Ansible Facts 示例

ansible_facts