Ansible - 如何循环使用dict和模块的值

时间:2018-06-08 08:20:56

标签: networking ansible

我尝试使用Ansible(=> 2.5)创建一个配置网络接口的任务,例如:

- name: Set up network interfaces addr
  interfaces_file:
    dest: "/etc/network/interfaces.d/{{ item.device }}"
    iface: "{{ item.device }}"
    state: present
    option: address
    value: "{{ item.addr }}"
  with_items:
   - "{{ network }}"
  when: item.addr is defined
  notify: Restart interface

- name: Set up network interfaces netmask
  interfaces_file:
    dest: "/etc/network/interfaces.d/{{ item.device }}"
    iface: "{{ item.device }}"
    state: present
    option: netmask
    value: "{{ item.netmask }}"
  with_items:
   - "{{ network }}"
  when: item.netmask is defined
  notify: Restart interface

- name: Set up network interfaces dns
  interfaces_file:
    dest: "/etc/network/interfaces.d/{{ item.device }}"
    iface: "{{ item.device }}"
    state: present
    option: dns-nameservers
    value: "{{ item.dns }}"
  with_items:
   - "{{ network }}"
  when: item.dns is defined
  notify: Restart interface

- name: Set up network interfaces dns-search
  interfaces_file:
    dest: "/etc/network/interfaces.d/{{ item.device }}"
    iface: "{{ item.device }}"
    state: present
    option: dns-search
    value: "{{ item.dns_search }}"
  with_items:
   - "{{ network }}"
  when: item.dns_search is defined
  notify: Restart interface

这很有效。 但从我的角度来看,那不是那么干净.. 所以我试图使用2个循环......这显然不起作用。

- name: Set up network interfaces
  interfaces_file:
    dest: "/etc/network/interfaces.d/{{ item.iDunnoWhatToPutHere }}"
    iface: "{{ item.iDunnoWhatToPutHere }}"
    state: present
    option: {{ item.option }}
    value: "{{ item.value }}"
with_together:
  - "{{ network }}"
  - { option: address, value: item.0.addr }
  - { option: netmask, value: item.0.netmask }
  - { option: dns-nameservers, value: item.0.dns }
  when: item.dns_search is defined
  notify: Restart interface
  [...]

编辑:这很好但是很严格。我应该循环vars,它应循环每个选项及其任何选项的值。因为我也有桥接选项,例如" vlan_raw_device,bridge_ports,bridge_stp ..."。所以它应该盲目地循环选择和价值观。

Edit2:使用可变网络

network:
  - name: admin
    device: admin
    method: static
    address: X.X.X.X/X
    netmask: X.X.X.X
    up:
      net: X.X.X.X/X
      gateway: X.X.X.X/X
    down:
      net: X.X.X.X/X
      gateway: X.X.X.X/X

为什么我要尝试这一切?

  1. 因为如果必须更改,我需要更改所有值。
  2. 因为我想重启(ifup,ifdown)只有
  3. 的界面
  4. 因为我很惊讶我必须多次使用同一个模块。
  5. 你能帮助我找出如何使用它吗? 也许这不可能?

    谢谢大家!

1 个答案:

答案 0 :(得分:1)

这是一项有望满足您需求的任务。我已将interfaces_file替换为debug模块,只是为了打印您需要在interfaces_file模块中实际使用的变量。为了演示,我在network变量中添加了第二个接口:

playbook与变量和任务:

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    network:
    - name: admin
      device: admin
      method: static
      address: 10.10.10.22
      netmask: 255.255.255.0
      up:
        net: X.X.X.X/X
        gateway: X.X.X.X/X
      down:
        net: X.X.X.X/X
        gateway: X.X.X.X/X
    - name: admin22
      device: admin22
      method: static
      address: 20.20.20.22
      netmask: 255.255.255.192
      up:
        net: X.X.X.X/X
        gateway: X.X.X.X/X
      down:
        net: X.X.X.X/X
        gateway: X.X.X.X/X

  tasks:
  - name: process network config
    debug:
      msg: "dest: {{ item[0].name }}, option: {{ item[1].option }}, value: {{ item[0][item[1].value] }}"
    with_nested:
      - "{{ network }}"
      - [{ option: address, value: address }, { option: netmask, value: netmask }]

结果:

TASK [process network config] ******************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "msg": "dest: admin, option: address, value: 10.10.10.22"
}
ok: [localhost] => (item=None) => {
    "msg": "dest: admin, option: netmask, value: 255.255.255.0"
}
ok: [localhost] => (item=None) => {
    "msg": "dest: admin22, option: address, value: 20.20.20.22"
}
ok: [localhost] => (item=None) => {
    "msg": "dest: admin22, option: netmask, value: 255.255.255.192"
}

希望有所帮助