How to dynamically create an ansible list out of hostvars? - ansible

I have a few variables defined for every host. Like...
hosts:
- hostA:
vars:
self_ip: "192.168.1.10"
self_port: "8001"
- hostB:
vars:
self_ip: "192.168.1.11"
self_port: "8002"
Inside one of the roles, I want to define a variable, which is a combination of few host variables. For example...
all_endpoints: 192.168.1.10:8001,192.168.1.11:8002
How can I do this?
I tried using Jinja2 for loops like below:
rs_members:
"{% for host in groups['all_hosts'] %}
- {{hostvars[host]['self_ip']}}:{{hostvars[host]['self_port']}}
{% endfor %}"
This seems to be creating a string. Not a list.
Can someone tell me what is wrong? And is there a way to use ansible filters to achieve this?

- set_fact:
all_endpoints: "{{ hosts|json_query('[].vars.[self_ip, self_port]') }}"
- set_fact:
touples: "{{ touples|default([]) + [ item.0 + ':' + item.1 ] }}"
loop: "{{ all_endpoints }}"
- debug:
var: touples
gives
"touples": [
"192.168.1.10:8001",
"192.168.1.11:8002"
]

Related

Dynamic variable name in Jinja for loop [duplicate]

I have vars where I put something like this:
vars/main.yml
hello_port: 80
world_port: 81
in my ansbile file I load the vars with
vars_files:
- ./vars/main.yml
This is how I initialize m_name:
- name: set_fact
set_fact:
m_name:
- 'hello'
- 'world'
and after that I have task with iterate using with_items:
- debug:
msg: "{{ (item + '_port') }}"
with_items: "{{ m_name }}"
But I've got as output
hello_port
world_port
not their values.
OK I find that if I use debug var it is working. But If I want to put this expression "{{ (item + '_port') }}" for an example in shell task it does not evaluate it. Is there a way to evaluate the dynamically created variables name - to get the value?
https://docs.ansible.com/ansible/2.5/plugins/lookup/vars.html
- name: Show value of 'variablename'
debug: msg="{{ lookup('vars', 'variabl' + myvar)}}"
vars:
variablename: hello
myvar: ename
{{ hostvars[inventory_hostname][item + '_port'] }}
http://docs.ansible.com/ansible/latest/faq.html#how-do-i-access-a-variable-name-programmatically
I think you are searching for:
{{ vars[item ~ '_port'] }}
I guess best way is to use varnames_lookup
- name: List variables that start with qz_
debug: msg="{{ lookup('varnames', '^qz_.+')}}"
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/varnames_lookup.html

ansible variables usage - ansible_hostname or {{ ansible_hostname }}

I am new to ansible. What is the correct to call ansible variables? Here are the 3 playbooks, playbook 1 uses "{{ ansible_hostname }}", however, playbook 2 and 3 uses "ansible_hostname" directly. What are the differences? Thanks!
Playbook 1:
tasks:
- name: Jinja2 template
template:
src: template.j2
dest: "/tmp/{{ ansible_hostname }}_template.out"
trim_blocks: true
mode: 0644
Playbook 2:
tasks:
- name: Ansible Jinja2 if
debug:
msg: >
--== Ansible Jinja2 if statement ==--
{# If the hostname is ubuntu-c, include a message -#}
{% if ansible_hostname == "ubuntu-c" -%}
This is ubuntu-c
{% endif %}
Playbook 3:
tasks:
- name: Exploring register
command: hostname -s
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version | int >= 8
register: command_register
playbook 1 uses "{{ ansible_hostname }}", however, playbook 2 uses "ansible_hostname"
That's not entirely correct. Both playbooks use the variable name ansible_hostname inside a Jinja templating context.
In the first playbook, it's simple variable substitution, so we use the {{ ... }} markers.
In the second playbook, it's being used in a control expression, so we use the {% ... %} markers.
In the third playbook, you're looking at the clauses of a when expression. From the documentation:
The when clause is a raw Jinja2 expression without double curly braces...
You can read more about Jinja syntax here.

Ansible nested loops, how to set inner loop variable based on outer variable

I'm having problems with a nested loop on ansible.
I'm using ansible 2.5.2 with the following config files:
file hosts:
[group1]
host1
host2
host3
[group2]
hostA
file host_vars/host{N} (where N is the number for each host on group1):
variable:
- { line: "keyB" , file: "keyc"}
- { line: "key2" , file: "key3"}
I need to execute on hostA one task for each line in host_vars/host{N}.
in pseudo code, need something like this:
- name: modify file
for host in groups['group1']:
for item in host['variables']:
lineinfile: "path={{ host }}/{{ item.file }} line={{ item.line }}"
using jinja2 loops does not work:
- name: Modify files
lineinfile: "{% for linea in hostvars[item]['variables'] %}
path={{ item }}/{{ linea.file }}
line={{ linea.line }}
{% endfor %}"
loop: "{{ groups['group1'] }}"
normal nested loops does not work either because the inner loop deppends on the hostname:
- name: Modify files
lineinfile: "path={{ item[0] }}/{{ item[1]['file'] }} line={{ item[1]['line'] }}"
with_nested:
- "{{ groups['group1'] }}"
- "{{ hostvars[item[0]]['variables'] }}"
There is another way to cicle nested loops?
I solved my problem using loop_control,
I add a new file: inner.yml
- name: Modify files
lineinfile: "path={{ outer_item }}/{{ item.file }} line={{ item.line }}"
loop: "{{ hostvars[outer_item]['variables'] }}"
And defined my task file as follows
- include_tasks: inner.yml
loop: "{{ groups['group1'] }}"
loop_control:
loop_var: outer_item
which solves my problem of using two nested variables on loops.

Ansible: Access facts set by set_fact

I need to be able to set variables using tasks in Ansible. I use set_fact for this, but cannot seem to access the fact I set with this. What is wrong with the code below:
- name: kludge1
set_fact: fake_y = "{{ [] }}"
- name: Loop
debug:
msg: "{{ item }}"
with_items: "{{ fake_y }}"
You have spaces before and after =...
- name: kludge1
set_fact: fake_y="{{ [] }}"
Avoid var= shortcut syntax. Use original YAML syntax instead, it gives less errors:
- name: kludge1
set_fact:
fake_y: "{{ [] }}"

Iterate over a array in with_items loop

Based on this question
Ansible recursive checks in playbooks
I have another one.
We need to go through this structure
Zone spec https://gist.github.com/git001/9230f041aaa34d22ec82eb17d444550c
Now I can adress the hostnames via the array index but can I also iterate over the array "hosts"?
playbook
--
- hosts: all
gather_facts: no
vars_files:
- "../doc/application-zone-spec.yml"
roles:
- { role: ingress_add, customers: "{{ application_zone_spec }}" }
role
- name: Print ingress hostnames
debug: msg="{{ item.hosts.0.hostname }} {{ item.hosts.1.hostname }}"
with_items: "{{ customers.ingress }}"
We use.
ansible-playbook --version
ansible-playbook 2.1.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
Use with_subelements:
- name: Print ingress hostnames
debug: msg="{{ item.0.type }} {{ item.1.hostname }}"
with_subelements:
- "{{ customers.ingress }}"
- "hosts"
There is quite a bit of examples for different loops in the Loops section of the documentation.

Resources