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
1
vote
1 answer

ansible distribute elements from one list of dicts to different files

I have one list of dicts ( variable ): sites: - { site: "https://site1.com", project: "own", module: "2xx" } - { site: "https://site2.com", project: "external", module: "2xx" } - { site: "https://site3.net", project: "own", module: "ssl" } -…
Nikolai
  • 13
  • 4
1
vote
1 answer

Ansible: How to read file and push results to templates

I have task to read data from csv file and push result to templates and copy those templates to different servers. however, i am getting error while writing to template. below are details - main.yml - name: Print return information from the previous…
1
vote
1 answer

Duplicate object values while merging the JSON Objects arrays in Ansible

I am trying to merge 2 JSON file Objects. But 2 of the objects are getting appended with the same value in both the JSON file. Here Month & Year values are same in both JSON file like Month=1 & Year=2023, so its getting appended in the output JSON,…
Vinny
  • 302
  • 2
  • 11
1
vote
1 answer

How can I convert a list into int in Ansible?

Unable to convert list into integer. I have tried below code. - debug: msg: "{{ application_list }} then we get below output.. ok: [ebppdoxs10] => { "msg": [ "1674142504", "1674138844" ] } after using filter int -…
1
vote
1 answer

How do I query a complex JSON in Jinja2?

I'm parsing a complex JSON with a Jinja2 template in Ansible. The JSON has a lot of items and random arrays. For example, how could I query the value of name & id if the date < 2001 for all motors? (this should return: Premium diesel & 4). Please…
Costales
  • 2,799
  • 1
  • 17
  • 21
1
vote
0 answers

Slack Channel ID not working but channel name is working fine

Current channel name has been changed. So, every time when we face this then we need to change the name in template too. So, I am planning to provide channel ID instead of channel name but template fails with channel ID. What possibly could be the…
Raining
  • 11
  • 2
1
vote
2 answers

Access "other" hosts facts when running a playbook in serial

I have a Jinja template where I'm trying to extract the master and replica hosts IPs like this: {{ hostvars[master|default(ansible_hostname|lower)]['ansible_default_ipv4']['address'] }} 7800
1
vote
2 answers

Jenkins Ansible Tower deploy fails if passing multiple custom arguments within "extraVars" parameter

I have a situation where I need to pass the branch name and host name (corresponds to instance w/ connection properties) into an Ansible template that is run as part of the code deployment stage. However I get the following if I try to specify…
1
vote
1 answer

ansible print the stdout_lines in csv file in the exact format that playbook prints on the console

I have the following ansible playbook code which prints the some metrices of one remote server. Here I want to print the output in the csv file with the exact msg format shown in the output. How to print this in csv file. Ansible playbook: tasks: …
1
vote
1 answer

Ansible: Copy one server hostname to another using jinja template

I have two windows server, assume serverA and serverB. I need to take the hostname of serverA as a variable and replace it in Jinja template and copy it in serverB. I am not able to find any solution how to use dependency of servers in Ansible. Is…
Atul Abhi
  • 13
  • 2
1
vote
2 answers

Add new line to template for each item in a list

Given the following example list and Jinja template: List: list: - foo - bar Jinja template: {% for key in list %} results: - "{{ key }}" {% endfor %} I am able to produce the following output: results: - foo results: -…
TJ Zimmerman
  • 3,100
  • 25
  • 39
1
vote
1 answer

add list to previous list in same line

I have two lists: "list1": [ "xxxx" "yyyyy", "yyyyy", "yyyyy", ] "list2": [ "xxxx" "yyyyy", "yyyyy", "yyyyy", ] I do a loop on list2…
GenZ
  • 49
  • 4
1
vote
1 answer

merge two lists based on a condition

"ansible_facts": { "list1": [ "10", "2", "4", "5", "6", "7", "7", "8", "9" ], "list2": [ [ …
GenZ
  • 49
  • 4
1
vote
1 answer

Ansible: How to print all the inventory hostnames with serial number in order?

I am a novice to Ansible. I am trying to print all the inventory hosts with serial number (sl.no) using a Jinja template which would be transformed to a .csv file. I have this playbook to ping all the hosts and get the status. - hosts: '{{ host }}' …
1
vote
1 answer

Remove authorized_keys using Ansible for multiple keys and multiple users

I am fairly new to Ansible and has been assigned a task. I have a YAML file in which I have the following keys for multiple users. client: - key: ssh-rsa…