Reputation: 9036
A var file is defined as follows:
packages:
- python-pycurl
- name: tmux
files:
tmux.conf: /etc/tmux/conf
tmux2.conf: /etc/tmux/conf2
So a package is list of package names or dictionaries if extended customization is required. I would like to install all packages using single task no matter if package name is defined in a list or using dictionary. I hopped something like the following could be accomplished:
- name: Install Base Packages
apt: name={{ [item.name, item] | select('defined') }}
with_items: packages
I know that I can do it with 2 taks using when
stanza but that is not what I want as it is not DRY.
The second part also presents the problem since first package doesn't have files defined so this complains:
- name: Copy package customizations
copy: src={{item.key}} dest={{item.value}}
with_subelements:
- packages
- files
Is it possible at all to use this kind of setup or I have to be verbose even for those items that don't require it?
Upvotes: 0
Views: 660
Reputation: 5586
You are going to end up with some ugly Ansible code to do what you want, your best bet will be to either make your packages var uniform or split it into two vars, one a list of packages and a second that maps a list files to packages. The latter approach is not ideal since you will repeat the package name.
The first thing you want to do is possible, but it is not something I would recommend:
name={% if 'name' in item %}{{ item['name'] }}{% else %}{{ item }}{% endif %}
The second thing you want to do is probably not possible because with_subelements expects to see the element when it loops through the list.
Upvotes: 2