Questions tagged [jinja2]

23 questions
5
votes
2 answers

Iterating through nested list / dictionary using Jinja2 templates

I'm trying to dynamically configure multiple NFS servers in my system by generating their /etc/exports files using Ansible. I'm hoping to be able to do this with a jinja2 template. It's the jinja2 template that I can't figure out based on my…
dutsnekcirf
  • 249
  • 2
  • 4
  • 16
4
votes
1 answer

Ansible: Convert comma separated string with key:value pairs to dictionary

I'm launching a setup playbook for a docker-compose service (apache solr) via extra vars. Those vars are set while executing since this playbook should be flexible and multiple configurations exists on one server. My idea is, that the user can setup…
Josef Glatz
  • 153
  • 4
3
votes
2 answers

How to select network interface given ip address in ansible across Debian and FreeBSD?

I'm looking for an expression to fetch the interface name given an ip address assigned to that iface, across Linux and FreeBSD. This question is based on this answer: https://serverfault.com/a/948288/416946 This jinja2 expression will, on Debian,…
Casey
  • 143
  • 1
  • 8
3
votes
3 answers

Ansible check if variable exists in attribute list

I have a variable containing details of host machines on my network (called 'hostlist' - I believe you call this a dictionary, but I'm not sure of the terminology. The variable is defined in a file in group_vars/all, so it's available in all…
Mike Williams
  • 33
  • 1
  • 1
  • 3
3
votes
2 answers

How to pass a group value in inventory file to jinja2 template file in ansible

I have an inventory file named hospital.inventory and it contains following group with a single value. [dockerSwarmManager] hp2-3.mydomain.com Then I have a file name security.json.j2 inside the folder call templates. In there I want to refer above…
AnujAroshA
  • 213
  • 1
  • 3
  • 7
2
votes
1 answer

How to merge two files into single file using ansible in remote server

I need to merge two files without duplicate entries in it. is there any way i can achieve it through ansible modules. Ex i have two files /etc/hosts1 and /etc/hosts2. I need to have one /etc/hosts file with all entries present in both /etc/hosts1…
celcoprab
  • 43
  • 2
  • 3
  • 8
2
votes
1 answer

Use lookup/dynamic variable names in Ansible jinja2 template

The playbook below should generate a file with the content: a,b ssh_host_key ssh_rsa_host_key However, the way I construct the variable names results in either syntax/templating errors or 'variable name does not exists': --- - hosts: localhost …
Willem
  • 177
  • 4
  • 13
2
votes
2 answers

ansible/jinja2 reject all items from list of dicts with specific attribute

I have a list of dicts like this: list_of_dicts: - name: Item1 - name: Item2 type: special - name: Item3 - name: Item4 type: small As you can see, not all items have a type attribute. Context: If no type attribute is set, that means…
Zulakis
  • 4,153
  • 14
  • 48
  • 76
1
vote
1 answer

Access value of specified dictionary key within a list of dictionaries in Ansible

I am installing list of packages in ansible registering variable and outputting it with debug: community.general.homebrew: name: "{{ package }}" state: present register: package_install until: package_install is succeeded loop: -…
Drew
  • 123
  • 5
1
vote
1 answer

How can I loop over files in a directory and move only the unique files using ansible

I’m trying to loop over all files in a directory, these files are named differently but should have the same contents. Is there a way to loop through all these files using ansible and then move the unique files into a different directory. Thanks in…
Adam
  • 31
  • 1
  • 4
1
vote
1 answer

Loop through variables in state file and use the vars in the target template as well

In Ansible I can loop through a list of dicts/lists and then use the loop vars in the Ansible task, but also in the template I call on. I'm trying to do the same in Salt, but it seems like it's not possible. At least not in the way I'm used to. I…
AquaL1te
  • 13
  • 2
1
vote
1 answer

Ansible: filling a template that contains immutable interpolations

I need to make a playbook that will take a template file, interpolate some variables and put the resulting file to another directory. The first part of the problem is that the resulting file will be used as another Ansible playbook, so the source…
Volodymyr Melnyk
  • 577
  • 1
  • 5
  • 19
1
vote
1 answer

Access dict value in variable from the same list item in Ansible

I'm using Ansible to deploy some virtual machines and I have a list variable defining the VMs. I'm wondering if I can have one of the dict items use another as a variable within the same list element. For instance, say I have the following variable…
1
vote
1 answer

Ansible to update list of dictionary based on certain condition

I wanted to update the list with ansible which contains dictionary items based on certain conditions for example: list1: - {"name": "test1", "uid": 100, "gid": 250} - {"name": "test2", "uid": 101, "gid": 250} - {"name": "test3", "uid": 103,…
Sandeep
  • 13
  • 1
  • 4
1
vote
1 answer

When using an Ansible playbook, how do you store and use a password containing quotes or other metacharacters without them being interpereted?

My company uses Ansible (which I am a novice at) and we have a playbook and a related role for joining new Linux machines to AD. Our admin accounts have their passwords rolled three times a day, and we can't set them ourselves. The Ansible…
Linoob
  • 113
  • 3
1
2