Questions tagged [jinja2]

27 questions
7
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,505
6
votes
1 answer

Why does ansible reevaluate a variable on each access

I need to generate some unique id in ansible. The generation works but accessing the variable results in some "not expected" behaviour. The playbook is very simple: --- - hosts: localhost vars: - var1: "{{ 99999999 | random }}" tasks: -…
5
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…
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…
4
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…
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
3
votes
2 answers

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…
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…
2
votes
1 answer

Ansible compare two value from two dictionnaries

I have a simple problem, i have two dictionaries, one being static with some data, and an other being the result of the stat command stored in a variable (with register). The goal is simply to compare the checksum of some files on a host, with the…
math
  • 121
2
votes
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
  • 41
2
votes
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
  • 23
  • 1
  • 4
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
  • 307
1
vote
1 answer

Ansible-map fuction

employees: sarav: city: Coimbatore email: sarav@gritfy.com mobile: '985643210' tasks: - name: gathering_complete_data ansible.builtin.debug: msg={{ ['sarav'] | map('extract', employees , 'email' )…
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
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…
1
2