Score:1

Access dict value in variable from the same list item in Ansible

tr flag

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

Score:0
br flag

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)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.