-1

我正在尝试重命名 Linux 上的以太网接口。每个名为 enp0s* 的接口都必须是 eth*。首先,我为重命名创建了 udev 规则。效果很好。其次,我必须为每个接口('etc/sysconfig/network-scripts/ifcfg-eth*')创建新的配置文件。我不知道,如何创建循环以在每个接口中放置参数。somne​​one 可以帮助我吗?

这是我的剧本的摘录:

- 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 %}
4

1 回答 1

0

只需修复您的语法:

- 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

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

于 2017-12-08T08:32:17.970 回答