【问题标题】:Ansible output to a fileAnsible 输出到文件
【发布时间】:2021-07-21 19:53:02
【问题描述】:

我想使用以下语法将每个 ansbile 主机的 mac 和 nics 获取到特定的 txt 文件:

nic:XXXXX,MAC:XXXXXX
nic:XXXXX,MAC:XXXXXX

正确的方法是什么?

此时我只能将其作为我创建的剧本的输出:

  - debug:
      msg: "nic:{{ (lookup('vars', concat)).device }},mac:{{ (lookup('vars', concat)).macaddress }}"
    vars:
      concat: "ansible_{{ item }}"
    loop: "{{ ansible_interfaces | difference(['lo']) }}"

剩下要做的就是使用上述语法将内容输出到文本文件。 我该怎么做?

谢谢。

【问题讨论】:

    标签: linux automation ansible


    【解决方案1】:

    这是现场编写的,未经测试:您可能需要为未定义的变量添加一些测试和默认值。我还从您的问题中猜到您在某些时候需要文件中的主机名。如果我错了,请删除它。简而言之:

    templates/nic_mac.txt.j2

    {% for h in group['all'] %}
    {% for i in (hostvars[h].ansible_interfaces | difference(['lo']) %}
    host:{{ hostvars[h].inventory_hostname }},nic:{{ hostvars[h]['ansible_'+i].device }},MAC:{{ hostvars[h]['ansible_'+i].macaddress }}
    {% endfor %}
    {% endfor %}
    

    playbook.yml

    ---
    - name: Make sure we have facts for all hosts
      hosts: all
      # This play will only trigger facts gathering, nothing else
    
    - name: Write nic and mac info to a file on controller
      hosts: localhost
      gather_facts: false
    
      tasks:
        - name: Write the file from template
          template:
            src: nic_mac.txt.j2
            dest: /tmp/nic_mac.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2021-06-25
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      相关资源
      最近更新 更多