Merge 2 groups in Ansible - ansible

I have 2 host groups in my inventory in ansible as follows:
[loadbalancer-add]
172.23.130.97
172.23.130.98
[loadbalancer-remove]
172.23.130.99
172.23.130.100
I would like to merge these groups to pass them to a loadbalancer API so I can add a server, then remove a server. So I need to merge the groups to create a group as follows:
[loadbalancer]
172.23.130.97
172.23.130.99
172.23.130.98
172.23.130.100
I have the following task but it is not producing the correct output
- name: Merge Dictionaries
gather_facts: false
hosts: localhost
become: true
no_log: false
tasks:
- add_host:
name: "{{ item }}"
ansible_ssh_port: 2020
action: remove
group: loadbalancer
with_items:
- "{{ groups['loadbalancer-remove'] }}"
- "{{ groups['loadbalancer-add'] }}"
delegate_to: localhost
This produces
[loadbalancer]
172.23.130.99
172.23.130.100
172.23.130.97
172.23.130.98
Is it possible to get the output that I require?
Thanks

May be this is what you need:
[loadbalancer-add]
172.23.130.97
172.23.130.98
[loadbalancer-remove]
172.23.130.99
172.23.130.100
[loadbalancer:children]
loadbalancer-remove
loadbalancer-add
So now you could reference as one group
groups['loadbalancer']*
.

I did manage to find a way of doing this. It may not be the best way but here is my solution anyway:
- name: Merge Dictionaries
gather_facts: false
hosts: localhost
no_log: false
vars:
merged_lb_hosts: |
{% if (groups['loadbalancer-add'] | length) >= (groups['loadbalancer-remove'] | length) %}
{% for i in range(0, groups['loadbalancer-add'] | length) -%}
{{ groups['loadbalancer-add'][i] | default('') }}:add|{{ groups['loadbalancer-remove'][i] | default('') }}:remove|
{%- endfor %}
{% else %}
{% for i in range(0, groups['loadbalancer-remove'] | length) -%}
{{ groups['loadbalancer-add'][i] | default('') }}:add|{{ groups['loadbalancer-remove'][i] | default('') }}:remove|
{%- endfor %}
{% endif %}
tasks:
- debug: msg="{{ merged_lb_hosts }}"
- add_host:
name: "{{ item.split(':')[0] }}"
action: "{{ item.split(':')[1] }}"
ansible_ssh_port: 2020
group: loadbalancer
with_items:
- "{{ (merged_lb_hosts | trim()).split('|') }}"
when: "{{item.split(':')[0] != ''}}"
This will take the add and remove groups and merge them into a var called merged_lb_hosts
The add_host task will split this var in it with_items section to create an iterable list.
It feels very hacky but it does the job that I was after

Related

Ansible: how to get the calculated value of key "content" displayed into the playbook output

I am a beginner to ansible.
How can I get the content of the csv file printed in my shell?
I tryed to register the calculated value of content key and to display it via
- ansible.builtin.debug:
msg: "{{ csv_content }}"
in another task, but I cannot see it into my playbook output.
vars:
current_date: "{{ '%Y-%m-%d' | strftime }}"
tasks:
- name: Dump results to /tmp/myfile.csv
copy:
dest: /tmp/mycsv_{{ '%Y-%m-%d' | strftime }}.csv
content: |
{% for host in hosts_list %}
{% ---things--- %}
{% set idm=host.inventory_hostname.split('_')[0].split('-')[1] %}
{% set idm_padded = '%03d' % idm|int %}
{% ---things--- %}
{{ [idm_padded, --things-- ] | map('trim') | join(';') }}
{% --things--- %}
{% endfor %}
vars:
hosts_list: "{{ ansible_play_hosts | map('extract', hostvars) | list }}"
register: csv_content
run_once: yes
- ansible.builtin.debug:
msg: "{{ csv_content }}"
Rather than trying to get the content from the copy task, reverse your logic: render the content into a variable first, then use that variable as the argument to the content key in the copy task:
- hosts: localhost
gather_facts: false
tasks:
- set_fact:
csv_content: |
{% for host in hosts_list %}
{% set idm=2 %}
{% set idm_padded = '%03d' % idm|int %}
{{ [idm_padded, "things" ] | map('trim') | join(';') }}
{% endfor %}
vars:
hosts_list:
- foo
- bar
- baz
run_once: true
- debug:
msg: "{{ csv_content }}"
- name: Dump results to /tmp/myfile.csv
copy:
dest: mycsv_{{ '%Y-%m-%d' | strftime }}.csv
content: "{{ csv_content }}"
run_once: true

Is there any way to loop through Ansible list of dictionary registered variable in combination with Jinja2?

In my inventory I have 3 servers in a group. I want to be able to increase that size in the future so I can add more nodes into the network and generate the template with Jinja2.
- name: Gathering API results
shell:
cmd: "curl {{ groups['nodes'][node_index] }}/whatever/api/result "
loop: "{{ groups['nodes'] }}"
loop_control:
index_var: node_index
register: api_value
If I run some debug tasks hardcoding which list I want to use everyhing works fine
- debug: "msg={{ api_value.results.0.stdout }}"
- debug: "msg={{ api_value.results.1.stdout }}"
- debug: "msg={{ api_value.results.2.stdout }}"
output:
ok: [server-1] => {
"msg": "random-value-a"
ok: [server-2] => {
"msg": "random-value-b"
ok: [server-3] => {
"msg": "random-value-c"
The problem is when I try to increase the list number in Jinja template. I tried several for loops combination, nested for loops and many other things but nothing seems to be working.
For example I want my Jinja template look similar like this:
{% for vm in groups['nodes'] %}
NODE_{{ loop.index }}={{ api_value.results.{loop.index}.stdout }}
{% endfor %}
This way I want to achieve this output:
NODE_0=random-value-a
NODE_1=random-value-b
NODE_2=random-value-c
Is there any other way to workaround this? Or maybe is something I could do better in the "Gathering API results" task?
Given the inventory
shell> cat hosts
[nodes]
server-1
server-2
server-3
Either run it in the loop at a single host, e.g.
- hosts: localhost
gather_facts: false
vars:
whatever_api_result:
server-1: random-value-a
server-2: random-value-b
server-3: random-value-c
tasks:
- command: "echo {{ whatever_api_result[item] }}"
register: api_value
loop: "{{ groups.nodes }}"
- debug:
msg: "{{ api_value.results|json_query('[].[item, stdout]') }}"
gives
msg:
- - server-1
- random-value-a
- - server-2
- random-value-b
- - server-3
- random-value-c
Then, in the Jinja template, fix the index variable
- debug:
msg: |-
{% for vm in groups.nodes %}
NODE_{{ loop.index0 }}={{ api_value.results[loop.index0].stdout }}
{% endfor %}
gives what you want
msg: |-
NODE_0=random-value-a
NODE_1=random-value-b
NODE_2=random-value-c
Optionally, iterate api_value.results. This gives the same result
- debug:
msg: |-
{% for v in api_value.results %}
NODE_{{ loop.index0 }}={{ v.stdout }}
{% endfor %}
Or run it in the group, e.g.
- hosts: nodes
gather_facts: false
vars:
whatever_api_result:
server-1: random-value-a
server-2: random-value-b
server-3: random-value-c
tasks:
- command: "echo {{ whatever_api_result[inventory_hostname] }}"
register: api_value
delegate_to: localhost
- debug:
msg: "{{ api_value.stdout }}"
(delegate to localhost for testing)
gives
ok: [server-1] =>
msg: random-value-a
ok: [server-2] =>
msg: random-value-b
ok: [server-3] =>
msg: random-value-c
Then, in the Jinja template, use hostvars
- debug:
msg: |-
{% for vm in groups.nodes %}
NODE_{{ loop.index0 }}={{ hostvars[vm].api_value.stdout }}
{% endfor %}
run_once: true
gives also what you want
msg: |-
NODE_0=random-value-a
NODE_1=random-value-b
NODE_2=random-value-c
Optionally, iterate hostvars. This gives the same result
- debug:
msg: |-
{% for k,v in hostvars.items() %}
NODE_{{ loop.index0 }}={{ v.api_value.stdout }}
{% endfor %}
run_once: true

Ansible and passing vars to the following include_role from a previous include_role

I'm trying to wrap all the roles of Kubespray in block/rescue blocks so I had to move from the usual roles includes like this:
- hosts: kube-master[0]
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
roles:
- { role: kubespray-defaults}
- { role: kubernetes-apps/rotate_tokens, tags: rotate_tokens, when: "secret_changed|default(false)" }
- { role: win_nodes/kubernetes_patch, tags: ["master", "win_nodes"]}
to this:
- hosts: kube-master[0]
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
vars:
roles:
- name: "kubespray-defaults"
- name: kubernetes-apps/rotate_tokens
tags: rotate_tokens
when: "secret_changed|default(false)"
- name: win_nodes/kubernetes_patch
tags: ["master", "win_nodes"]
- name: "ems-notification"
msg: kubespray-defaults, kubernetes-apps/rotate_tokens and win_nodes/kubernetes_patch completed
tasks:
- include_tasks: roles/a4-roles/tasks/main.yml
loop: "{{ roles }}"
with a4-roles/tasks/main.yml being:
- name: a4-roles
when: item.when | default(omit)
block:
- include_role:
name: "{{ item.name }}"
apply:
tags: >-
{%- if item.tags is defined -%}
"{{ item.tags }}"
{%- else -%}
""
{%- endif -%}
rescue:
- include_role:
name: "ems-notification"
vars:
msg: an error has occurred
host: "{{ inventory_hostname }}"
result: "{{ ansible_failed_result.msg | trim | default(omit) }}"
role: "{{ item.name }}"
error: "true"
The problem is that the kubespray-defaults as well as other roles are setting some vars and defaults that are being used by the subsequent roles in the roles block.
When using include_role those vars and defaults are just lost. Is there any way to retain them and pass them on to the next role?
I found a way to solve this just by using the public directive. Setting it to true shares the defaults and vars with all the following roles.
All I had to do was change a4-roles/tasks/main.yml and make it like this:
- name: a4-roles
when: item.when | default(omit)
block:
- include_role:
name: "{{ item.name }}"
public: true
apply:
tags: >-
{%- if item.tags is defined -%}
"{{ item.tags }}"
{%- else -%}
""
{%- endif -%}
rescue:
- include_role:
name: "ems-notification"
vars:
msg: an error has occurred
host: "{{ inventory_hostname }}"
result: "{{ ansible_failed_result.msg | trim | default(omit) }}"
role: "{{ item.name }}"
error: "true"
This is still polluting the global stack as those defaults and vars are being shared with all the roles and not just those that are processed by my loop.
I don't think there is a better solution at the time I'm writing this.

Ansible: Loop over dict and filetree

How to loop over dict and filetree? I want to recursively template files with .j2 suffix (key) to destination location (value), also basename should be renamed (remove .j2 suffix). Its a perfect use case. Unfortunatelly ansible is not good with complex data structures.
Input:
vars:
applications:
application1:
svcpaths:
localfolder/bardir1: remotefolder/bardir1
localfolder/bardir2: remotefolder/bardir2
localfolder/bardir3: remotefolder/bardir3
application2:
svcpaths:
localfolder/bardir5: remotefolder/bardir5
localfolder/bardir6: remotefolder/bardir6
My try:
- name: Files to template
template:
src: "{{ item.src }}"
dest: "{{ item.destination }}/{{ item.name | regex_replace('.j2','') }}"
loop: |
[
{% for c in applications %}
{% if applications[c]['svcpaths'] is defined and applications[c]['svcpaths'] |list|length >0 %}
{% for o,m in applications[c]['svcpaths'].items() %}
{% for i in lookup('filetree', o ) %}
{% if i.state == 'file' and i.path | regex_search('.\.j2') %}
{
"name": "{{ i.path }}",
"src": "{{ i.src }}",
"destination": "{{ m }}"
},
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
]
I know that using jinja in plays is not good and I want to avoid it, if its possible. Also input datastructure should not be changed.
Thannks
If I understand what you're trying to do, I think there is a reasonably simple solution. If you write a task file like this called template_files.yml:
---
- name: render templates to dest_dir
loop: "{{ query('filetree', src_dir) }}"
# we need this to avoid conflicts with the "item" variable in
# the calling playbook.
loop_control:
loop_var: template
when: template.src.endswith('.j2')
template:
src: "{{ template.src }}"
dest: "{{ dest_dir }}/{{ (template.src|basename)[:-3] }}"
Then you can write a playbook like this:
---
- hosts: localhost
gather_facts: false
vars:
applications:
application1:
svcpaths:
localfolder/bardir1: /tmp/remotefolder/bardir1
localfolder/bardir2: /tmp/remotefolder/bardir2
localfolder/bardir3: /tmp/remotefolder/bardir3
application2:
svcpaths:
localfolder/bardir5: /tmp/remotefolder/bardir5
localfolder/bardir6: /tmp/remotefolder/bardir6
tasks:
# generate a list of {key: src, value: destination}
# dictionaries from your data structure.
- set_fact:
templates: "{{ templates|default([]) + item|dict2items }}"
loop: "{{ applications|json_query('*.svcpaths')}}"
# show what the generated variable looks like
- debug:
var: templates
# template all the things
- include_tasks: template_files.yml
loop: "{{ templates }}"
vars:
src_dir: "{{ item.key }}"
dest_dir: "{{ item.value }}"
Given that I have a set of local files that look like this:
localfolder/bardir1/example.txt.j2
localfolder/bardir2/example.txt.j2
localfolder/bardir3/example.txt.j2
localfolder/bardir5/example.txt.j2
localfolder/bardir6/example.txt.j2
Running the playbook results in:
/tmp/remotefolder/bardir6/example.txt
/tmp/remotefolder/bardir5/example.txt
/tmp/remotefolder/bardir3/example.txt
/tmp/remotefolder/bardir2/example.txt
/tmp/remotefolder/bardir1/example.txt
I think that's probably easier to read and understand than the Jinja-template based solution you're using.

How to get all the node list IP addresses?

In my case, there are four nodes which runs ansible. I want to get each and every node ip address. Therefore i tried these.
In my playbook.yml
- name: Ansible
hosts: all
gather_facts: true
vars:
ansible_ec2_local_ipv4: "{{ ansible_default_ipv4.address }}"
roles:
- role: "ansible-mongo/roles/mongo"
- role: "ansible-mongo/roles/replication"
In my main.yml
- name: ensure file exists
copy:
content: ""
dest: /tmp/myconfig.cfg
force: no
group: "{{ mongodb_group }}"
owner: "{{ mongodb_user }}"
mode: 0555
- name: Create List of nodes to be added into Cluster
set_fact: nodelist={%for host in groups['all']%}"{{hostvars[host].ansible_eth0.ipv4.address}}"{% if not loop.last %},{% endif %}{% endfor %}
- debug: msg=[{{nodelist}}]
- name: Set Cluster node list in config file
lineinfile:
path: "/tmp/myconfig.cfg"
line: "hosts: [{{ nodelist }}]"
But as a result when i tried to view /tmp/myconfig.cfg file. I only get one IP.
cat /tmp/myconfig.cfg
hosts: ["10.1.49.149"]
Any idea on this?
Your set_fact loop is overwriting the value of 'nodelist' on each pass, effectively meaning you only ever end up with the last element in the loop. Try this:
- set_fact:
nodelist: "{{ ( nodelist | default([]) ) + [ hostvars[item].ansible_eth0.ipv4.address ] }}"
loop: "{{ groups['all'] }}"
- debug:
var: nodelist | join(',')
(nodelist | default([])) outputs the current value of 'nodelist' or an empty list if it is not set (first pass)
+ [] merges the existing list with a new list, containing a single element - the IP of the host
So 'nodelist' ultimately ends up containing a list of IP's. You can then use | join(',') to turn that into a CSV.

Resources