How to iterate over nested list and split it into two groups using add_host?

20 views
Skip to first unread message

Danny Rehelis

unread,
Sep 9, 2019, 3:05:26 AM9/9/19
to Ansible Project
Hey,

Given the following playbook -

- hosts: localhost
 gather_facts: false
 vars:
   my_hosts:
     - ['host1', 'host2']
     - ['host3']

- name: Add host
add_host:
name: '{{ item }}'
groups: "group{{ index }}"
loop: "{{ my_hosts }}"
loop_control:
index_var: index

---

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

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        [
            "host1",
            "host2"
        ],
        [
            "host3"
        ]
    ]
}

TASK [Add host] *****************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=['host1', 'host2'])
changed: [localhost] => (item=['host3'])
ERROR! Unexpected Exception, this is probably a bug: unhashable type: 'list'

I'm having hard time iterating this var in order to add_host properly into X groups. I want this to be dynamic as much as possible.

Vladimir Botka

unread,
Sep 9, 2019, 3:25:53 AM9/9/19
to Danny Rehelis, ansible...@googlegroups.com
On Mon, 9 Sep 2019 00:05:26 -0700 (PDT)
Danny Rehelis <aut...@gmail.com> wrote:

> vars:
> my_hosts:
> - ['host1', 'host2']
> - ['host3']
> [...]
> loop: "{{ my_hosts }}"
> [...]
> ERROR! Unexpected Exception, this is probably a bug: unhashable type: 'list'
>
> I'm having hard time iterating this var in order to add_host properly into
> X groups. I want this to be dynamic as much as possible.

Flatten the lists.

loop: "{{ my_hosts|flatten }}"

Cheers,

-vlado

Danny Rehelis

unread,
Sep 9, 2019, 3:54:32 AM9/9/19
to Ansible Project
Thanks for the hint,

Flatting the list results with 

TASK [print] ********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "all": [
            "host1",
            "host2",
            "host3"
        ],
        "group0": [
            "host1"
        ],
        "group1": [
            "host2"
        ],
        "group2": [
            "host3"
        ],
        "ungrouped": []
    }
}

What i'm trying to achieve is -

TASK [print] ********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "all": [
            "host1",
            "host2",
            "host3"
        ],
        "group0": [
            "host1",
            "host2"
        ],
        "group1": [
            "host3"
        ],
        "ungrouped": []
    }
}

Vladimir Botka

unread,
Sep 9, 2019, 6:52:47 AM9/9/19
to Danny Rehelis, ansible...@googlegroups.com
On Mon, 9 Sep 2019 00:54:32 -0700 (PDT)
Danny Rehelis <aut...@gmail.com> wrote:

> > > vars:
> > > my_hosts:
> > > - ['host1', 'host2']
> > > - ['host3']
> > > [...]
> > > loop: "{{ my_hosts }}"
> > > [...]
> > > ERROR! Unexpected Exception, this is probably a bug: unhashable type:

> >
> > Flatten the lists.
> >
> > loop: "{{ my_hosts|flatten }}"
> >

> What i'm trying to achieve is -
> "msg": {
> "all": [
> "host1",
> "host2",
> "host3"
> ],
> "group0": [
> "host1",
> "host2"
> ],
> "group1": [
> "host3"
> ],
> "ungrouped": []

You've said "I want this to be dynamic as much as possible". Where does this
"dynamic" assignment of the hosts to the groups come from? Any structure? If
not it might be hard-coded i.e not dynamic at all. Make your choice.

Cheers,

-vlado

Vladimir Botka

unread,
Sep 9, 2019, 8:43:06 AM9/9/19
to Danny Rehelis, ansible...@googlegroups.com
If the hosts from 1st element of the list come to group0, from 2nd to
group1 and so on ... The play below

vars:
my_hosts:
- ['host1', 'host2']
- ['host3']
tasks:
- include_tasks: loop.yml
loop: '{{ my_hosts }}'
loop_control:
loop_var: outer_item
index_var: outer_idx
- debug:
var: groups

with this included task below gives the groups.

$ cat loop.yml
- add_host:
name: "{{ item }}"
groups: "{{ 'group' ~ outer_idx }}"
loop: "{{ outer_item }}"


Cheers,

-vlado
Reply all
Reply to author
Forward
0 new messages