Flatten a dict-list in a template

297 views
Skip to first unread message

Joachim Friberg

unread,
May 21, 2014, 5:59:14 AM5/21/14
to ansible...@googlegroups.com
So I'm trying to setup a shinken-enviroment and it has a similar syntax to Nagios.
In the contactgroups.cfg I need to put all my admins on one line.
Is there anyway to take entries from a list of dicts and flatten them to one line?
Below is my test-example
---
  - hosts: localhost
    remote_user: root
    vars:
      users:
       - name: Andrew
       - name: Doug
       - name: Paul
       - name: ipphone
    tasks:
     - debug: var=users
     - name: test
       template: src=../templates/test.j2
                 dest=/home/root/test.cfg

The template:
define contactgroup{
    contactgroup_name   Admins
    alias               Seamless_internal_admins
    members {{ users.name }}
}

result:
define contactgroup{
    contactgroup_name   JAPS
    alias               Seamless_internal_admins
    members Doug
}

Desired result:
define contactgroup{
    contactgroup_name   JAPS
    alias               Seamless_internal_admins
    members             Andrew,Doug,Paul,ipphone
}


Petros Moisiadis

unread,
May 21, 2014, 7:30:18 AM5/21/14
to ansible...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2ce4fa5b-80e9-4e33-915b-7f1361027eb2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Modify your template like this:

define contactgroup{
    contactgroup_name   Admins
    alias               Seamless_internal_admins
    members {{ users[0].name }}{% for user in users[1:] %},{{ user.name }}{% endfor %}
}

Joachim Friberg

unread,
May 21, 2014, 7:34:19 AM5/21/14
to ansible...@googlegroups.com
How do you know all of this! You are amazing! :)

It worked
Reply all
Reply to author
Forward
0 new messages