Questions tagged [jinja2]

18 questions
3
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
  • 79
  • 1
  • 3
  • 14
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
  • 8
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
  • 193
  • 1
  • 2
  • 7
2
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
  • 23
  • 1
  • 1
  • 3
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
  • 157
  • 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,191
  • 14
  • 44
  • 75
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
  • 537
  • 5
  • 18
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
  • 4
1
vote
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
  • 23
  • 1
  • 2
  • 8
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
0
votes
0 answers

Ansible- Define variables based on conditions with if elif else

I have multiple roles as follows : haproxy, java, nginx, tomcat ├── ansible-test-host.yml ├── hapoxy ├── java ├── myplaybook.yml ├── nginx └── tomcat Base playbook is : myplaybook.yml and looks like below: --- - hosts: test-local connection:…
SAGAR Nair
  • 133
  • 8
0
votes
1 answer

Ansible jinja2 template from JSON format provided as extra-vars

I have this jinja2 template: # {{ ansible_managed }} {% for vhost in nginx_vhosts %} {%- if vhost.name == item.name -%} # redirect www to non-www server { listen {{ nginx_port }}; listen [::]:{{ nginx_port }}; port_in_redirect off; …
Bogdan Stoica
  • 363
  • 2
  • 8
0
votes
2 answers

ansible - template json for environmental variable in docker container

I'm trying to assign a one-liner json string to an environmental variable in a docker container. This is what the json look like: {"ip_access": {"IP_whitelist": {"192.168.99.19/32": "grafana/status (Provider)"}}, "vhosts": {"prometheus1":…
Lethargos
  • 396
  • 1
  • 4
  • 16
0
votes
3 answers

templating file with ansible to get different variable parts (through its index) for different hosts

I'm trying to distribute certificates to its corresponding hosts (I'll just give the example for the private key task): - name: create certificate private key community.crypto.openssl_privatekey: path: "/root/client/{{ item }}.key" type:…
Lethargos
  • 396
  • 1
  • 4
  • 16
1
2