1

i need some help on configure multiple cloud_init_nics using variable files. Here is my variable files for example: files/dict

vm:
  all:
    - name: rhel7
      hostname: rhel7
      dns: "8.8.8.8 8.8.4.4"
      nic:
        - nic_name: eth0
          ip: "10.10.10.10"
          netmask: "255.255.255.0"
          gateway: "10.10.10.1"
          bootproto: "static"
          onboot: "true"
        - nic_name: "eth1"
          ip: "10.10.10.11"
          netmask: "255.255.255.0"
          bootproto: "static"
          onboot: "true"

    - name: rhel8
      hostname: rhel8
      dns: "8.8.8.8 8.8.4.4"
      nic:
        - ip: "10.10.10.12"
          netmask: "255.255.255.0"
          gateway: "10.10.10.1"
          nic_name: "ens7"
          bootproto: "static"
          onboot: "true"

Each vm could have from minimum 1 nic to N number of nic card.

Here is my playbook(obviously didn't work), because it will loop the second nic as a different tasks.

- name: include vards
  include_vars: files/dict

- name: readvar
  ovirt_vm:
    cloud_init_nics:
    - nic_name: "{{item.nic|json_query('[*].nic_name') }}"
      nic_boot_protocol: "{{item.nic|json_query('[*].ip') }}"
    cloud_init_persist: no
    wait: false
  with_items: "{{vm.all}}"

From the documentation , it seems like it need multiple "- nic_name" under the same task

  cloud_init_nics:
    - nic_name: eth0
      nic_boot_protocol: dhcp
    - nic_name: eth1
      nic_boot_protocol: static
      nic_ip_address: 10.34.60.86
      nic_netmask: 255.255.252.0

Because each vm has different number of nic , so I cant use the example from the documentation.

So my question would be: how do i loop the cloud_init_nics multiple times but still running as 1 task? Is that possible? if not is there any idea that i should look up to?

is it possible to register item.all.nic as a variable, then cloud_init_nics: {{ var }} ? Thanks

sloweriang
  • 21
  • 1
  • 6

1 Answers1

0

Translate the nic lists to keep the required attributes and use the nic lists in the module

- name: readvar
  ovirt_vm:
    cloud_init_nics: "{{ item.nic }}"
    cloud_init_persist: no
    wait: false
  loop: "{{ vm.all }}"

You can create the correct data on your own. For example

    - set_fact:
        vm2: "{{ vm2|default([]) + [item|combine({'nic': nic|from_yaml})] }}"
      loop: "{{ vm.all }}"
      vars:
        nic: |
          {% for i in item.nic %}
            - nic_boot_protocol: {{ i.bootproto|default(omit) }}
              nic_boot_protocol_v6: {{ i.nic_boot_protocol_v6|default(omit) }}
              nic_gateway: {{ i.gateway|default(omit) }}
              nic_gateway_v6: {{ i.nic_gateway_v6|default(omit) }}
              nic_ip_address: {{ i.ip|default(omit) }}
              nic_ip_address_v6: {{ i.nic_ip_address_v6|default(omit) }}
              nic_name: {{ i.nic_name|default(omit) }}
              nic_netmask: {{ i.netmask|default(omit) }}
              nic_netmask_v6: {{ i.nic_netmask_v6|default(omit) }}
          {% endfor %}
    - debug:
        var: vm2

gives

vm2:
  - dns: 8.8.8.8 8.8.4.4
    hostname: rhel7
    name: rhel7
    nic:
    - nic_boot_protocol: static
      nic_gateway: 10.10.10.1
      nic_ip_address: 10.10.10.10
      nic_name: eth0
      nic_netmask: 255.255.255.0
    - nic_boot_protocol: static
      nic_ip_address: 10.10.10.11
      nic_name: eth1
      nic_netmask: 255.255.255.0
  - dns: 8.8.8.8 8.8.4.4
    hostname: rhel8
    name: rhel8
    nic:
    - nic_boot_protocol: static
      nic_gateway: 10.10.10.1
      nic_ip_address: 10.10.10.12
      nic_name: ens7
      nic_netmask: 255.255.255.0
Vladimir Botka
  • 3,791
  • 6
  • 17