【发布时间】: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