【问题标题】:Ansible with items using multiple variables使用多个变量的项目 Ansible
【发布时间】:2020-06-25 05:44:46
【问题描述】:

只是寻求一点帮助。

基本上,我正在尝试为我的计算机具有的每个网络接口创建一个文件,文件名是接口名称和 mac 地址的组合。
例如

对于这些接口
eth0 与 mac 123-456-789
eth1 与 mac 987-654-321
eth2 与 mac 456-123-789

我希望创建 3 个使用名称的文件:
eth0_123-456-789
eth1_987-654-321
eth2_456-123-789

我目前有这个剧本

- hosts: all

  tasks:
    - name: Create files
      block:
        - name: This gets me a list of all eth* interfaces
          shell: ls /sys/class/net/ | grep eth
          register: eth_interface

        - name: This gets the mac address for each of those eth* interfaces
          command: cat /sys/class/net/{{ item }}/address
          with_items: "{{ eth_interface.stdout_lines }}"
          register: mac_address

        - name: Add mac address to its relevant file
          command: touch /tmp/"{{ item.mac }}"_"{{ item.eth }}"
          with_items: 
             - { eth: "{{ eth_interface.stdout_lines }}", mac: "{{ mac_address | json_query('results[*].stdout') }}" }

我希望为每个接口获得 3 个单独的文件,但我得到了 1 个名称如下的单个文件: [u'123-456-789', u'987-654-321', u'456-123-789'][u'eth0', u'eth1', u'eth2']

如果我只使用一个变量(如 eth 或 mac)似乎可以正常工作,但是当我使用 with_items 循环同时使用这两个变量时会中断。

有人可以帮忙吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    下面的剧本

    shell> cat pb.yml
    - hosts: localhost
      vars:
        ifc: [eth0, eth1, eth2]
        mac: [AAA, BBB, CCC]
      tasks:
        - file:
            state: touch
            path: "{{ 'tmp/' ~ item.0 ~ '_' ~ item.1 }}"
          loop: "{{ ifc|zip(mac)|list }}"
    

    创造

    shell> tree tmp
    tmp
    ├── eth0_AAA
    ├── eth1_BBB
    └── eth2_CCC
    

    【讨论】:

    • 哇,成功了。我已经为此挠头两天了,应该早点寻求帮助。非常感谢您的帮助!!!
    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 2015-01-02
    • 2022-07-19
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    相关资源
    最近更新 更多