1

I'm using Ansible to deploy some virtual machines and I have a list variable defining the VMs. I'm wondering if I can have one of the dict items use another as a variable within the same list element. For instance, say I have the following variable defined:

nodes:
  - name: vm1
    aliases:
      - vm1
      - vm1.local
  - name: vm2
    aliases:
      - vm2
      - vm2.local

Could I, instead, do something like this?

nodes:
  - name: vm1
    aliases:
      - "{{ name }}"
      - "{{ name }}.local"
  - name: vm2
    aliases:
      - "{{ name }}"
      - "{{ name }}.local"

I tried that and I tried using "{{ nodes.0.name }}".

1 Answers1

0

No. You can't. You'll have to concatenate the strings in run-time. For example

  - debug:
      msg: "vm:{{ item.0 }} alias:{{ item.0 }}{{ item.1 }}"
    with_subelements:
      - "{{ nodes }}"
      - aliases
    vars:
      nodes:
        - name: vm1
          aliases:
            - ""
            - ".local"
        - name: vm2
          aliases:
            - ""
            - ".local"

(not tested)

Vladimir Botka
  • 3,791
  • 6
  • 17