How to filter dictionary entries using Jinja

102 views
Skip to first unread message

Frank Thommen

unread,
Apr 26, 2017, 10:23:10 AM4/26/17
to ansible...@googlegroups.com
Dear all,

I'm at a loss as to how to filter entries fom a dictionry based von
specific host variables. In the concrete case we have a long list of
possible NFS mounts which should be applied according to the hosts' class.


Our hostlist would be like:

host1 class=cluster
host2 class=cluster
host3 class=server


our dictionary of possible mounts would e.g. be:

---
/mnt/1:
name: /mnt/1
src: server:/exports/1
fstype: nfs
state: mounted
class:
- cluster

/mnt/2:
name: /mnt/2
src: server:/exports/2
fstype: nfs
state: mounted
class:
- cluster
- server

/mnt/3:
name: /mnt/3
src: server:/exports/3
fstype: nfs
state: mounted
class:
- cluster
...


I.e I'd like all three mounts to be available on hosts of the class
"cluster" and /mnt/2 (but not the other two mounts) on hosts of the
class "server".

The current playbook (which applies all mounts to all hosts) is

---
- hosts: '{{ target }}'
gather_facts: no
serial: 5

tasks:
- include_vars:
file: ./mounts-dict.yml
name: fs

- name: "Add NFS mounts"
mount:
name: "{{ item.value.name }}"
fstype: "{{ item.value.fstype }}"
src: "{{ item.value.src }}"
state: "{{ item.value.state }}"
with_dict: "{{ fs }}"
...


The playbook is run with `ansible-playbook ./nfs-mounts.yml
--extra-vars="target=<hostname>".

What I'd need is a syntax, which checks for each mount in the
dictionary, if one of it's (possibly multiple) classes matches the
(single) class of the server and then applies the mount. I could not
find a usable Jinja-Syntax which I could use together with `when`. I'd
appreciate any hint or pointer to achieve this task.

Thanks
frank


Dick Davies

unread,
Apr 27, 2017, 3:18:05 AM4/27/17
to ansible list
If each mount was managed by a role, you could just apply those
roles to the (groups holding) the relevant servers.

You'll need to have multiple data structures, one per role, but that's
probably going to 'work with the grain' of Ansible better.
> --
> 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/a0fbe52c-a1bc-f33e-985e-216dac495630%40drosera.ch.
> For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Apr 27, 2017, 6:41:23 AM4/27/17
to ansible...@googlegroups.com
On 26.04.2017 16:20, Frank Thommen wrote:
> What I'd need is a syntax, which checks for each mount in the
> dictionary, if one of it's (possibly multiple) classes matches the
> (single) class of the server and then applies the mount. I could not
> find a usable Jinja-Syntax which I could use together with `when`.
> I'd appreciate any hint or pointer to achieve this task.

when: class in item.value.class

--
Kai Stian Olstad

Frank Thommen

unread,
Apr 28, 2017, 2:10:49 PM4/28/17
to ansible...@googlegroups.com
I haven't used roles yet. If a host can assume several roles, then this
could work. Otherwise I'll have to deal with redundant dictionaries,
which is exactly what I want to avoid.

Thanks for the hint

frank

Frank Thommen

unread,
Apr 28, 2017, 2:11:33 PM4/28/17
to ansible...@googlegroups.com
Wow, so simple? :-) Thanks a lot.
frank
Reply all
Reply to author
Forward
0 new messages