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

Join a list of key-value pairs in Ansible template

I need to convert the following yaml dictionary into comma-separated list of key=value pairs nodes: node1: 192.168.56.11 node2: 192.168.56.12 so it becomes node1=192.168.56.11,node2=192.168.56.12 In Python I would do it with a simple list…
badbishop
  • 1,281
  • 2
  • 18
  • 38
2
votes
2 answers

How to set different different arguments depending on when condition

I'd like to use a when condition so different paths are used depending on the host the playbook is limited. My playbook does this already but is very unclear as I have for every host of the inventory one entry doing the same. - name: copy yaml …
2
votes
1 answer

Ansible template loop

I'm trying to loop a dictionary and set a variable. This is what I have in my template file. {% for item in db_server() %} {% if item.name in fqdn.stdout and item.mysql == "mysql-5-5-28" %} {% set version = "mysql-5.5.28-linux2.6-x86_64"…
2
votes
1 answer

regex_replace by a var content in jinja template

I have a jinja template and i want to replace a string by the content of a variable exemple : ansible_hostname: 'host-to' item.suffixe: 'cool' the result will be : host-cool-to I did this : {{ ansible_hostname | regex_replace('-to', '-{{…
p305
  • 125
  • 1
  • 1
  • 14
2
votes
1 answer

How to get an env var inside a jinja2 template in ansible

So I have this bash script: #!/bin/bash echo -ne "Enter stack name: " read -r STACK echo -ne "Enter node type (Compute/Storage): " read -r NODE_TYPE export STACK export NODE_TYPE ansible-playbook -i inventory -l "$AC_STACK"…
Bogdan Stoica
  • 4,349
  • 2
  • 23
  • 38
2
votes
1 answer

Possible to use Jinja2 expressions in role vars file

I'm wondering if it's possible to use a Jinja2 {% if %} expression inside a vars file? So say I have: az: az1: foo az2: bar az3: foobar {% if az == az['az1'] %} floating_ip_pool = bar {% endif %} Basically, I'm trying to avoid having to set…
nascarj
  • 23
  • 2
2
votes
1 answer

Ansible task - how can I loop over all predefined vars to create linux groups in this format?

I have a predefined ansible variables, and I want to create linux groups from these variables. The below task create only the groups in the first array g1,g2,g3 How can I loop over all lines to create other groups? The predefined ansible…
2
votes
1 answer

Ansible: Unable to start the httpd service after installing its package

I am testing playbooks The following playbook could install apache and start the service: - hosts: apacheweb user: ansibleuser become: yes gather_facts: no tasks: - name: Install the apache web server yum: pkg=httpd state=latest …
vjwilson
  • 754
  • 2
  • 14
  • 30
2
votes
1 answer

How to use variable inside variable in ansible jinja template

I am trying to set value in j2 template in ansible. But the variable contains index inside it. For example: lb_url: {{ lbip_{{index }}_url }} But the above format doesn't work. What is the right format to support this?
2
votes
2 answers

Ansible | The conditional check 'item.stat.exists' failed

**Getting an error like :** "msg": "The conditional check 'item.stat.exists' failed. The error was: error while evaluating conditional (item.stat.exists): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stat'\n\nThe error…
2
votes
2 answers

Ansible: how to define "when" statement for the case, when some variable is not defined in any of ansible_play_hosts_all hosts?

i.e. I have a playbook, there some actions could be applied for some of hosts within ansible_play_hosts_all list, and I need to execute one task in the only case if none of hosts within ansible_play_hosts_all list have certain variable defined. I…
2
votes
1 answer

how to copy file from /root of controller node to remote host's /root via ansible playbook

I need to copy a file from /root of ansible control node to remote node's /root. I am able to copy that file if i keep it at any other location (say /tmp) of control node and able to copy it to /root of remote node (since i mentioned become in…
pratkul007
  • 89
  • 1
  • 6
2
votes
1 answer

Ansible templates and jinja {%block%}

I need to generate a single file on the remote host using multiple template files and Jinja's {% block block_name %} in my Ansible role For example, main.conf.j2: value1 = 123 value2 = 456 {% block test %} {% endblock %} value3 = 789 {% block…
Serg
  • 109
  • 2
  • 10
2
votes
0 answers

How to find a value index of a list in jinja

I'm using ansible-template (Jinja2), how can i get/find a value index of a list? For example: listvar = ['abc', 'def', 'hij'] # i want get the index of value 'hij', it should get 2 i know use for and set, is there a other simple way?
JJP
  • 825
  • 1
  • 7
  • 12
2
votes
1 answer

How can I build a string from a value and a list of values?

[First time questioner. I think that I've targeted this as an Ansible question. If not, gentle redirection is welcome!] Given: gid: 80 ports: [80, 443] where the number of ports may vary from 0 to many I'd like to produce a string like…
hartzell
  • 93
  • 6