Ansible の変数を使用して cloud_init_nics の複数のインターフェースを構成する方法

Ansible の変数を使用して cloud_init_nics の複数のインターフェースを構成する方法

変数ファイルを使用して複数の cloud_init_nics を構成する方法について助けが必要です。たとえば、変数ファイルは次のとおりです: 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"

各 VM には、最低 1 個の NIC から N 個の NIC カードを搭載できます。

ここに私のプレイブックがあります(明らかに機能しませんでした)。2 番目の NIC を別のタスクとしてループするためです。

- 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}}"

ドキュメントによると、同じタスクで複数の「- nic_name」が必要なようです。

  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

各 VM の NIC の数が異なるため、ドキュメントの例は使用できません。

そこで質問なのですが、cloud_init_nics を複数回ループしながらも 1 つのタスクとして実行するにはどうすればよいでしょうか? それは可能ですか? そうでない場合、参考にすべきアイデアはありますか?

item.all.nic を変数として登録し、cloud_init_nics: {{ var }} とすることは可能ですか? ありがとうございます

答え1

翻訳するニック必要なものをリストアップする属性そして、ニックモジュール内のリスト

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

正しいデータを自分で作成することもできます。例えば

    - 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

与える

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

関連情報