Ansible 如何创建动态文件?

Ansible 如何创建动态文件?,ansible,ansible-template,Ansible,Ansible Template,我正在尝试在Linux上重命名以太网接口。每个名为enp0s*的接口必须为eth*。首先,我创建用于重命名的udev规则。那很好。其次,我必须为每个接口创建新的配置文件(“etc/sysconfig/networkscripts/ifcfg eth*”)。我不知道如何创建循环以在每个接口中放置参数。Somnone能帮我吗 这是我剧本的摘录: - name: Get new interface-names of the new created udev-rules become: yes

我正在尝试在Linux上重命名以太网接口。每个名为enp0s*的接口必须为eth*。首先,我创建用于重命名的udev规则。那很好。其次,我必须为每个接口创建新的配置文件(“etc/sysconfig/networkscripts/ifcfg eth*”)。我不知道如何创建循环以在每个接口中放置参数。Somnone能帮我吗

这是我剧本的摘录:

- name: Get new interface-names of the new created udev-rules
  become: yes
  shell: cat /etc/udev/rules.d/70-persisten-ipoib.rules.cfg | egrep -i 'eth.'
  register: car

- name: Create new-ifcfg-eth* files
  template:
  with_items: "{{ car.stdout_lines }}"
  register: cat
  template: src= roles/configure_network/templates/create_ifcfg.cfg.j2    dest=/etc/sysconfig/network-scripts/ifcfg-{{ item }}

# Template: roles/configure_network/templates/create_ifcfg.cfg.j2

{% for interface in cat.results %}
NAME="eth{{ item.name }}
TYPE=Ethernet
BOOTPROTO={{item.bootproto|default('dhcp')}}
IPV4_FAILURE_FATAL=no
IPV6INIT=no
{% if item.ipaddress is defined %}
IPADDR={{item.ipaddress}}
{% endif %}
{% if item.netmask is defined %}
NETMASK={{item.netmask}}
{% endif %}
{% if item.gateway is defined %}
GATEWAY={{item.gateway}}
{% endif %}
PEERDNS=no
{% if item.dns is defined %}
DNS1={{item.dns}}
{% endif %}
ONBOOT={{item.onboot|default('yes')}}
{% endfor %}
只需修改您的语法:

- name: Create new-ifcfg-eth* files
  template:
    src: create_ifcfg.cfg.j2
    dest: /etc/sysconfig/network-scripts/ifcfg-{{ item }}
  with_items: "{{ car.stdout_lines }}"
  register: cat

删除double
template
call,使用相对路径(无需定义角色自己模板的完整路径),使用YAML语法而不是
key=value
(其中包含空格,这是不允许的)。

这并不是回答你的问题,但还有其他方法可以实现你想要的-看,我不喜欢这样,如何禁用此功能。