How to use dictionnary with multiple elements ?

Hi,

I need help for use an dictionnary with multiple element, but i don’t find how to :confused:

Exemple :

vars/mains.yml :

users:
alice:
name: Alice Appleworth
telephone:

  • “123-456-7890”
  • “123-456-7891”

task/main.yml :

  • name: “Test”
    debug: msg=“User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})”
    with_dict: “{{users}}”

Output :

ok: [XXXXXXXXXXXXXX] => (item={‘value’: {u’name’: u’Alice Appleworth’, u’telephone’: [u’123-456-7890’, u’123-456-7891’]}, ‘key’: u’alice’}) => {
“item”: {
“key”: “alice”,
“value”: {
“name”: “Alice Appleworth”,
“telephone”: [
“123-456-7890”,
“123-456-7891”
]
}
},
“msg”: "User alice is Alice Appleworth ([u’123-456-7890’, u’123-456-7891’])"
}

I would have two messages with phone number,
Exemple :

“msg”: "User alice is Alice Appleworth (123-456-7890)"

“msg”: "User alice is Alice Appleworth (123-456-7891)"

Anybody have idea plz ? :confused:

You could use with_subelements, however your var structure doesn’t lend itself well to using with_subelements.

If you changed your var structure to look like the following (a list of users rather than a dict of users):

users:

  • username: alice
    name: Alice Appleworth
    telephone:
  • “123-456-7890”
  • “123-456-7891”

You could then use:

  • debug:
    msg: “User {{ item.0.username }} is {{ item.0.name }} ({{ item.1 }})”
    with_subelements:
  • “{{ users }}”
  • telephone

Hi Matt,

Thx for your help :slight_smile:

I have already try to use “with_subelement”, but when i try this i have this error all the time :
=> [WARNING]: Failure when attempting to use callback plugin (<ansible.plugins.callback.default.CallbackModule object at 0x2024d10>): not all arguments converted during string formatting

Idem, with your exemple :frowning:

idem with short subelement :

Exemple :

vars =>
project:

  • name: alice
    test:
  • test1
  • test2

task =>

  • debug:
    msg: “{{ item.0.name }}”
    with_subelements:
  • “{{ project }}”
  • test

output :

TASK [debug] **********************************************************
[WARNING]: Failure when attempting to use callback plugin (<ansible.plugins.callback.default.CallbackModule object at 0x2c20d10>): not all arguments converted during string formatting

I rage… :cry:

I have try on localhost ansible install with this playbook :

  • hosts: 127.0.0.1
    user: cvalentin
    vars:
    project:
  • name: alice
    test:
  • test1
  • test2
    tasks:
  • debug:
    msg: “{{ item.0.name }}”
    with_subelements:
  • “{{ project }}”
  • test

Output :

ok: [127.0.0.1] => (item=({‘name’: ‘alice’}, ‘test1’)) => {
“item”: [
{
“name”: “alice”
},
“test1”
],
“msg”: “alice”
}
ok: [127.0.0.1] => (item=({‘name’: ‘alice’}, ‘test2’)) => {
“item”: [
{
“name”: “alice”
},
“test2”
],
“msg”: “alice”
}

And on my remote host server :

[WARNING]: provided hosts list is empty, only localhost is available

PLAY [127.0.0.1] ***************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
[WARNING]: Failure when attempting to use callback plugin (<ansible.plugins.callback.default.CallbackModule object at 0x28c21d0>): not all arguments converted during string formatting

PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0

Ansible bug in my remote-host version ??
Remote Host => 2.1.0
Localhost => 1.5.4

:cry:

I have try with version 2.1.0 in localhost and work fine…

Python version ?
local => Python 2.7.6
remote => Python 2.7.3

I try to upgrade Python version…