Ansible 循环变量

标签 ansible ansible-playbook

我正在使用ansible更新新添加的网卡的配置文件
为此,我在单独的 yml 文件中定义了一些变量

/tmp/ip.yml

#first interface
interface1: eth1
bootproto1: static
ipaddress1: 192.168.211.249
netmask1: 255.255.255.0
gateway: 192.168.211.2
DNS1: 192.168.211.2

#second interface
interface2: eth2
bootproto2: static
ipaddress2: 10.0.0.100
netmask2: 255.0.0.0

剧本
- include_vars: /tmp/ip.yml

- name: configuring interface 
  lineinfile:
    state=present
    create=yes
    dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}}
    regexp="{{ item.regexp }}"
    line="{{ item.line }}"
  with_items:
     - { regexp: '^BOOTPROTO=.*', line: 'BOOTPROTO={{interface1}}' }
     - { regexp: '^IPADDR=.*', line: 'IPADDR={{ipaddress1}' }
     - { regexp: '^NETMASK=.*', line: 'NETMASK={{netmask1}}' }
     - { regexp: '^GATEWAY=.*', line: 'GATEWAY={{gateway}}' }
     - { regexp: '^PEERDNS=.*', line: 'PEERDNS=no' }
     - { regexp: '^DNS1=.*', line: 'DNS1={{DNS1}}' }
     - { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' }
when: bootproto1 == 'static'

- name: configuring for DHCP
  lineinfile:
   state=present
   create=yes
   dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}}
   regexp="{{ item.regexp }}"
   line="{{ item.line }}"
  with_items:
    - { regexp: '^BOOTPROTO=.*',line: 'BOOTPROTO={{bootproto1}}' }
    - {regexp: '^PEERDNS=.*',line: 'PEERDNS=yes' }
    - { regexp: '^ONBOOT=.*', line: 'ONBOOT={{onboot}}' }
when: bootproto1 == 'dhcp'

类似地重复第二个界面。

即使这种方法适用于 2 个 NIC,这也很难管理,即对于每个添加的新 NIC,我都需要修改 playbook 并更新 中的相应变量/tmp/ip.yml .

有没有办法将变量添加到/tmp/ip.yml 并且可能使用一些分隔符将其解析为剧本,而无需每次修改剧本以插入新的 NIC。

最佳答案

这里有很多话要说。
一、尽量避免lineinfile像瘟疫一样。这确实是最后的解决方案。 lineinfile使得编写一致且幂等的剧本变得困难。

现在,由于您正在尝试填充 RH 样式的界面文件,因此很容易做到。

组织你的变量

首先要做的是为您的变量建立一个适当的结构。你会想要循环你的接口(interface),所以你必须让东西'可循环'。有interface1 , interface2 ... interfaceN如您所述,不可扩展。

这是一个建议:

interfaces_ipv4:
  - name: eth0
    bootproto: static
    ipaddress: 192.168.211.249
    netmask: 255.255.255.0
    gateway: 192.168.211.2
    dns: 192.168.211.2
  - name: eth2
    bootproto: static
    ipaddress: 10.0.0.100
    netmask: 255.0.0.0

写你的模板

现在您有了数据,您需要一个模板来创建您的操作系统配置文件。
BOOTPROTO={{item.bootproto}}
IPADDR={{item.ipaddress}}
NETMASK={{item.netmask}}
{% if item.gateway is defined %}
GATEWAY={{item.gateway}}
{% endif %}
PEERDNS=no
DNS1={{item.dns}}
ONBOOT={{item.onboot|default('no')}}

我包括了两个变体:您可以在未设置时跳过输出行({% if ... %} 构造)或提供默认值(例如 {{item.onboot|default('no')}})。

您的里程可能会有所不同,具体取决于您是要使用默认值还是要跳过 if 构造。

创建任务

最后,这是一个将为每个接口(interface)创建接口(interface)配置文件的任务:
- name: Push template
  template: 
    src=/path/to/the/above/template.j2
    dest=/etc/sysconfig/network-scripts/ifcfg-{{item.name}}.cfg
  with_items:
    - "{{ interfaces_ipv4 }}"

这应该做这一切。

当然,使用此任务的最佳方式是将其添加到某个“网络”角色,并从剧本中调用它。

祝你好运。

关于Ansible 循环变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606121/

相关文章:

Ansible:我们可以异步运行包含剧本吗?

shell - 使用 ansible 打印文件的内容

Ansible:理解复合条件 when 语句

windows - 对 Windows 的 ansible 支持在哪里?

ansible - 如何从包含 YAML 的字符串中读取 Ansible 变量?

loops - 一遍多个Ansible任务

ansible-playbook - Ansible 将 --extra-vars 用于条件包含

Ansible 将变量传递给角色

Ansible Yum 模块挂起交易错误

ansible - 如何为动态 list 中的服务器定义 SSH 私钥