Questions tagged [ansible-template]

Use this tag for questions regarding writing and processing Ansible templates, Jinja2 syntax within Ansible, custom filters.

Ansible templates are processed by the Jinja2 templating language. Beside the default Jinja2 filters Ansible extends functionality with a lot of custom filters.

Templates are rendered from Ansible tasks through the template module or through the template lookup plugin.

638 questions
18
votes
1 answer

Ansible Jinja2 string comparison

I am getting value of variable "env" in Jinja2 template file using a variable defined in group_vars like: env: "{{ defined_variable.split('-')[0] }}" env possible three values could be abc, def, xyz. On the basis of this value I want to use server…
Ruchir Bharadwaj
  • 1,132
  • 4
  • 15
  • 31
14
votes
1 answer

I want to include another Jinja2 template in an Ansible context in Jinja2

I have an Ansible playbook that sets a lot of variables. One the playbooks has this task: - name: create config file template: src: 'templates/main_config.j2' dest: "{{ tmp_dir }}/main_config.json" The template main_config.j2 writes…
saurabh daga
  • 205
  • 1
  • 2
  • 11
14
votes
1 answer

Printing a variable value indented in a YAML file using ansible

I'm generating a Behat config file using Ansible. This configuration file is a YAML file. I'm using a Jinja2 template like this: default: paths: features: '../all/tests/features' filters: tags: "~@api&&~@drush" extensions: …
sanzante
  • 844
  • 1
  • 11
  • 28
13
votes
2 answers

ansible regex_search with variable

How to find a match using regex in ansible playbook where variable appears in the regex_search argument? The following playbook doesn't find the match... when run using: ansible-playbook playbook.yml - hosts: localhost gather_facts: no tasks: …
rok
  • 9,403
  • 17
  • 70
  • 126
12
votes
3 answers

Restart service when service file changes when using Ansible

I am creating a systemd service using template module --- - name: Systemd service template: src: sonar.unit.j2 dest: /etc/systemd/system/sonarqube.service when: "ansible_service_mgr == 'systemd'" The contents of the sonarqube.service…
onknows
  • 6,151
  • 12
  • 65
  • 109
11
votes
1 answer

Get first "N" elements of a list in Jinja2 template in Ansible

Most of my locations have 4+ DNS sources, but a few have less. Each location gets their own dns4_ips list variable like this: dns4_ips: - dns_A - dns_B - dns_C - dns_C My resolv.conf template looks like this: domain example.com search…
dan_linder
  • 881
  • 1
  • 9
  • 30
11
votes
1 answer

Ansible/jinja2: Use filter result in if condition

is it possible to use the result of Jinja2 filters in conditions in ansible playbooks? I'm trying to get this working, but without success: {% if (item | ipv4) %}{{ item }}{% else %}{{ lookup('dig', item) }}{% endif %}} where item in my current…
Matthias Lohr
  • 1,696
  • 2
  • 20
  • 32
10
votes
2 answers

Print string when "item exists in a list" in Jinja2 template

I'm trying to populate nsswitch.conf with values that are determined from a list. The list is of string: openldap_nsswitch: - group - hosts - passwd - shadow If the string is in the list I want to output something in the template. passwd: …
Ken J
  • 4,312
  • 12
  • 50
  • 86
10
votes
1 answer

Ansible loop - how to match up template values with with_items?

I'm trying to create files that have values that match their with_items values. I have a var list like so: sites: - domain: google.com cname: blue - domain: facebook.com cname: green - domain: twitter.com cname: red I…
gkrizek
  • 978
  • 3
  • 10
  • 18
9
votes
3 answers

Ansible: Global template folder?

Couldn't find anything google'ing. There is group_vars/all/ for variables. Is there something similar for templates? I would like to use some templates across multiple roles.
kev
  • 8,928
  • 14
  • 61
  • 103
9
votes
7 answers

Ansible w/ Docker - Show current Container state

Im working on a little Ansible project in which I'm using Docker Containers. I'll keep my question short: I want to get the state of a running Dockercontainer! What I mean by that is, that i want to get the current state of the container, that…
Shai Shvili
  • 99
  • 1
  • 1
  • 6
8
votes
1 answer

AnsibleError: template error while templating string: expected token ':', got '}'

An error occurs when preparing the template. Who can tell you how to fix it? Variables, if necessary, can also be edited. vars: AllСountry: - "name1" - "name2" name1: - "region1a" - "region1b" …
Alex alex
  • 116
  • 1
  • 1
  • 7
7
votes
2 answers

Ansible: How to set docker container labels with dynamic key names?

I try to start docker containers with traefik labels. To create a traefik router for a container you have to put some labels like this. app_names: - tower01 - tower02 docker_labels: awx_web: traefik.enable: "true" …
7
votes
1 answer

ansible shell task erroring out without proper message

Team, I am trying to verify if sdd exists in mount command output. so when there is any, i am fine but when there is none, my task is simply failing instead of just telling me that no mounts exists. any hint how to tackle this? I don't want my task…
AhmFM
  • 1,552
  • 3
  • 23
  • 53
7
votes
3 answers

Is there a way to override a template defined into an ansible galaxy role?

I'm trying to set up a server with ansible and I'd like to use this galaxy role. It defines a template that I'd like to customize, but I don't know how. Right now I defined the role into requirements.yml and I installed it using: ansible-galaxy…
delphaber
  • 199
  • 1
  • 13
1
2
3
42 43