HowTo: combine two lists in vars section/file

761 views
Skip to first unread message

Jard Leex

unread,
Mar 30, 2016, 9:46:22 AM3/30/16
to Ansible Project
Hello together,

i'd like to combine two lists directly in a vars section/file.
I've tried the combine filter but as i understood it, it is for dicts and not lists.
Also the union filter did not worked for me as i tried it
I assume that i did something wrong.

Can someone here make the example below fly?



---
- name: combine lists
  hosts
: localhost
  vars
:
   
- level1keyA:
       
- hostname: foo
          state
: present
          path
:
           
- left
       
- hostname: bar
          state
: present
          addonpath
:
           
- righthere
          path
:
           
- "{{ WHAT TO WRITE HERE? }}"
   
- level1keyB:
     
- right
     
- middle
     
- top
  tasks
:
   
- name: result of level1keyA
      debug
:
        msg
: "{{ level1keyA }}"

   
- name: result of level1keyB
      debug
:
        msg
: "{{ level1keyB }}"


It would be great if the task could be solved within the vars itself without editing the playbook using them.


Thank you very much!

Best
Jard
Message has been deleted

Jard Leex

unread,
Mar 30, 2016, 10:00:05 AM3/30/16
to Ansible Project
To make my request a bit more clear:


I'd like to have the entries of level1keyB in the path list of the hostname bar. The key 'addonpath' is just an guessed construct by me. Maybe this would also work.


- level1keyA:
       
- hostname: foo
          state
: present
          path
:
           
- left
       
- hostname: bar
          state
: present

          path
:
           
- righthere
           
- "{{ entries of level1keyB }}"

But how to do it in practise?

Thank you.

John Buxton

unread,
Mar 31, 2016, 7:07:31 PM3/31/16
to Ansible Project
In your second example, you could do this:

path: "{{ ['righthere'] | union(level1keyB) }}"


I'm not sure it's possible in your first example, because to put this in your "WHAT TO WRITE HERE" section you have to access "addonpath" from another part of the same level1keyA data structure, so you end up with a recursive loop error.

Jard Leex

unread,
Apr 1, 2016, 3:12:30 AM4/1/16
to Ansible Project
Hi John Buxton,

thanks for your reply.
Indeed that worked.

--
- name: combine lists
  hosts
: localhost
  vars
:

   
- level1keyA:
       
- hostname: foo
          state
: present
          path
:
           
- left
       
- hostname: bar
          state
: present
          path
:
           
-
righthere
           
- "{{ ['righthere'] |union (level1keyB) }}"

   
- level1keyB:
     
- right
     
- middle
     
- top
  tasks
:
   
- name: result of level1keyA
      debug
:
        msg
: "{{ level1keyA }}"

   
- name: result of level1keyB
      debug
:
        msg
: "{{ level1keyB }}"



:~/ansible ansible-playbook test.yml                                                                                                                                                                                    

PLAY
[combine lists] ***********************************************************

TASK
[setup] *******************************************************************
Friday 01 April 2016  08:47:45 +0200 (0:00:00.016)       0:00:00.017 **********
ok
: [localhost]

TASK
[result of level1keyA] ****************************************************
Friday 01 April 2016  08:47:46 +0200 (0:00:00.599)       0:00:00.616 **********
ok
: [localhost] => {
   
"msg": [
       
{
           
"hostname": "foo",
           
"path": [
               
"left"
           
],
           
"state": "present"
       
},
       
{
           
"hostname": "bar",
           
"path": [
               
"righthere",
               
[
                   
"righthere",
                   
"right",
                   
"middle",
                   
"top"
               
]
           
],
           
"state": "present"
       
}
   
]
}

TASK
[result of level1keyB] ****************************************************
Friday 01 April 2016  08:47:46 +0200 (0:00:00.030)       0:00:00.646 **********
ok
: [localhost] => {
   
"msg": [
       
"right",
       
"middle",
       
"top"
   
]
}

PLAY RECAP
*********************************************************************
localhost                  
: ok=3    changed=0    unreachable=0    failed=0


But would it be possible to do this also with multiple entries in path of level1keyA? Like:

---
- name: combine lists
  hosts
: localhost
  vars
:

   
- level1keyA:
       
- hostname: foo
          state
: present
          path
:
           
- left
       
- hostname: bar
          state
: present
          path
:
           
-
righthere
           
- peter
           
- paul
           
- mary
           
- "{{ WHAT NOW HERE? |union (level1keyB) }}"

   
- level1keyB:
     
- right
     
- middle
     
- top
  tasks
:
   
- name: result of level1keyA
      debug
:
        msg
: "{{ level1keyA }}"

   
- name: result of level1keyB
      debug
:
        msg
: "{{ level1keyB }}"

Listing all peter, paul, mary etc in front of the union wouldn't be very convenient either.

Thank you

Jard

Jard Leex

unread,
Jul 8, 2016, 4:48:22 AM7/8/16
to Ansible Project
Hi,

anyone else got an idea how to solve this?

Thanks in advance

Jard Leex

unread,
Aug 31, 2016, 2:59:42 AM8/31/16
to Ansible Project
Hi,

to calrify the question here comes a shorten code block

---
- hosts: localhost
  gather_facts: no
  vars:
    - simple:
      - right
      - middle
      - top
    - nested:
      - hostname: foo
        path:
          - left
      - hostname: bar
        path:
          - blue
          - green
          - yellow
          - "{{ simple }}"

  tasks:
    - name: content of nested
      debug:
        msg: "{{ nested }}"

This is the current output:
PLAY [localhost] ***************************************************************

TASK [content of nested] *******************************************************
Wednesday 31 August 2016  08:55:13 +0200 (0:00:00.025)       0:00:00.025 ****** 
ok: [localhost] => {
    "msg": [
        {
            "hostname": "foo", 
            "path": [
                "left"
            ]
        }, 
        {
            "hostname": "bar", 
            "path": [
                "blue", 
                "green", 
                "yellow", 
                [
                    "right", 
                    "middle", 
                    "top"
                ]
            ]
        }
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0 


What I'd like to have is (manipulated output)

PLAY [localhost] ***************************************************************

TASK [content of nested] *******************************************************
Wednesday 31 August 2016  08:55:13 +0200 (0:00:00.025)       0:00:00.025 ****** 
ok: [localhost] => {
    "msg": [
        {
            "hostname": "foo", 
            "path": [
                "left"
            ]
        }, 
        {
            "hostname": "bar", 
            "path": [
                "blue",
                "green",
                "yellow",
                "right",
                "middle",
                "top",
            ]
        }
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0 


Ideas?
Reply all
Reply to author
Forward
0 new messages