How to reference multiple sub elements using ansible? - loops

I've got an ansible playbook with the following vars structure:
TESTS:
- name: test1
hosts: ['host_one', 'host_two', 'host_three']
services: ['service1, 'service2, 'service3']
- name: test2
hosts: ['host_four', 'host_five', 'host_six']
services: ['service4, 'service5, 'service6']
This is the kind of task I want to do, but of course with_subelements only allows one subkey. I've been trying to use with_nested but struggling quite a lot.
- name: check services on each host
systemd:
name: "{{item.1}}"
state: started
delegate_to: "{{item.2}}"
with_subelements:
- "{{TESTS}}"
- services
- hosts
I want each service to be checked on each of the corresponding hosts.
eg.
test1:
service1 on host_one,host_two,host_three
service2 on host_one,host_two,host_three
service3 on host_one,host_two,host_three
test2:
service4 on host_four,host_five,host_six
service5 on host_four,host_five,host_six
service6 on host_four,host_five,host_six

when to tranform data is complex i prefer to use a custom plugin:
create a file my_filter.py in the folder filter_plugins (same level than your playbook) and give customfilter as name:
my_filter.py:
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'customfilter': self.customfilter
}
def customfilter(self, obj):
result = []
for rec in obj:
for ser in rec["services"]:
for host in rec["hosts"]:
result.append({ "name":rec["name"], "service":ser, "host":host })
#print(result)
return result
playbook to use the custom filter:
- name: "make this working"
hosts: localhost
vars:
TESTS:
- name: test1
hosts: ['host_one', 'host_two', 'host_three']
services: ['service1', 'service2', 'service3']
- name: test2
hosts: ['host_four', 'host_five', 'host_six']
services: ['service4', 'service5', 'service6']
tasks:
- name: Debug
debug:
msg: "{{ item }}"
loop: "{{ TESTS | customfilter }}"
for your playbook:
- name: check services on each host: {{ item.name }}
systemd:
name: "{{item.service}}"
state: started
delegate_to: "{{item.host}}"
loop: "{{ TESTS | customfilter }}"

Related

How iterate module output from a loop Ansible and capture particular value only

How to iterate module output from a loop in ansible and capture particular value to be redirected to a file. Example: 'amazon-ssm-agent.service']['state']": "running" should be pushed to a file locally.
[ansibleadm#node1 ~]$ cat myloops3.yaml
---
- name: collect service status remotely
hosts: remote
become: yes
roles:
- role: myServices
myServiceName:
- amazon-ssm-agent.service
- cloud-init-local.service
[ansibleadm#node1 ~]$ cat roles/myServices/tasks/main.yml
---
# tasks file for myServices
- name: collect systemd info
service_facts:
- name: cross verify service is runnng or not
debug:
var: ansible_facts.services['{{ item }}']['state']
loop: "{{ myServiceName }}"
[ansibleadm#node1 ~]$
## Outputs ##
TASK [myServices : cross verify service is runnng or not]
*****************************************************************
ok: [3.109.201.79] => (item=amazon-ssm-agent.service) => {
"ansible_facts.services['amazon-ssm-agent.service']['state']": "running",
"ansible_loop_var": "item",
"item": "amazon-ssm-agent.service"
}
ok: [3.109.201.79] => (item=cloud-init-local.service) => {
"ansible_facts.services['cloud-init-local.service']['state']": "stopped",
"ansible_loop_var": "item",
"item": "cloud-init-local.service"
}
Say you want to output those services status into a file, you may use something like this:
- name: collect systemd info
service_facts:
- name: cross verify service is runnng or not
copy:
content: |
{% for s in myServiceName %}{{ s }}={{ ansible_facts.services[s]['state'] }}
{% endfor %}
dest: /tmp/test.txt
Would give you:
$> cat /tmp/test.txt
amazon-ssm-agent.service=running
cloud-init-local.service=running
Or, if you want one file per service:
- name: cross verify service is runnng or not
loop: "{{ myServiceName }}"
copy:
content: |
{{ ansible_facts.services[item]['state'] }}
dest: "/tmp/{{ item }}.txt"
Which gives:
$> cat /tmp/amazon-ssm-agent.service.txt
running
If you need the quotation and parenthesis from the example (I balanced the beginning)
Example: "['amazon-ssm-agent.service']['state']": "running"
the Jinja below should create it. For example, given myServiceName: [ssh, xen]
- service_facts:
- copy:
content: |
{% for s in myServiceName %}
"['{{ s }}']['state']": "{{ ansible_facts.services[s]['state'] }}"
{% endfor %}
dest: /tmp/test.txt
creates the file
shell> cat /tmp/test.txt
"['ssh']['state']": "running"
"['xen']['state']": "running"

Ansible cannot invoke variable for hostname in playbook

I need to add new users to multiple Ubuntu servers. Unfortunately, the password and username are not consistent. Every machine has its own username and the password cannot be the same. For example, host-1 will have a user account host-1_username with password host-1_password and host-2 will have a user account host-2_username with password host-2_password, and so on.
I would like to do that by Ansible. I have a list.yaml file:
---
list:
- hostname: host-1
username: host-1_username
password: host-1_password
- hostname: host-2
username: host-2_username
password: host-2_password
- hostname: host-3
username: host-3_username
password: host-3_password
Here is my Ansible playbook:
- name: Crate new user
vars_files:
- list.yml
hosts: "{{ item.hostname }}"
remote_user: root
become: true
tasks:
- name: Create new user
ansible.builtin.user:
name: "{{ item.username }}"
groups: sudo
password: "{{ item.password | password_hash('sha512') }}"
shell: /bin/bash
- name: Modify sshd_config
ansible.builtin.lineinfile:
dest: /etc/ssh/sshd_config
line: 'AllowUsers {{ item.username }}'
loop: "{{ list }}"
But looks like Ansible cannot invoke the variable to add into hosts column:
ERROR! couldn't resolve module/action 'hosts'. This often indicates a misspelling, missing collection, or incorrect module path.
I am very new to Ansible, any help is appreciated!
Given the data
shell> cat list.yml
users_list:
- hostname: host-1
username: host-1_username
password: host-1_password
- hostname: host-2
username: host-2_username
password: host-2_password
- hostname: host-3
username: host-3_username
password: host-3_password
Create an inventory file, e.g.
shell> cat hosts
host-1
host-2
host-3
Convert the data to dictionaries, e.g.
- hosts: all
gather_facts: false
vars_files:
- list.yml
tasks:
- set_fact:
users_dict: "{{ users_list|items2dict(key_name='hostname', value_name='username') }}"
psswd_dict: "{{ users_list|items2dict(key_name='hostname', value_name='password') }}"
run_once: true
gives
users_dict:
host-1: host-1_username
host-2: host-2_username
host-3: host-3_username
and
psswd_dict:
host-1: host-1_password
host-2: host-2_password
host-3: host-3_password
Use the dictionaries to select the hosts' specific users and passwords, e.g.
- debug:
msg: "Create user: {{ users_dict[inventory_hostname] }}
password: {{ psswd_dict[inventory_hostname] }}"
gives
TASK [debug] ***************************************************************
ok: [host-1] =>
msg: 'Create user: host-1_username password: host-1_password'
ok: [host-2] =>
msg: 'Create user: host-2_username password: host-2_password'
ok: [host-3] =>
msg: 'Create user: host-3_username password: host-3_password'
You can omit the inventory file and create a playbook completely driven by the data. Create dynamic group my_group in the first play and use it in the second one. The playbook below gives the same results
- name: Create dynamic group of the hosts from users_list
hosts: localhost
gather_facts: false
vars_files:
- list.yml
tasks:
- add_host:
name: "{{ item.hostname }}"
groups: my_group
loop: "{{ users_list }}"
- name: Create users
hosts: my_group
gather_facts: false
vars_files:
- list.yml
tasks:
- set_fact:
users_dict: "{{ users_list|items2dict(key_name='hostname', value_name='username') }}"
psswd_dict: "{{ users_list|items2dict(key_name='hostname', value_name='password') }}"
run_once: true
- debug:
var: users_dict
run_once: true
- debug:
var: psswd_dict
run_once: true
- debug:
msg: "Create user: {{ users_dict[inventory_hostname] }}
password: {{ psswd_dict[inventory_hostname] }}"

concatenate variables in ansible to pass them as helm flags for deployments

The goal is to pass variables from ansible to helm --set key=value. The following output structure for ansible is available
apps:
- name: proxy
properties:
- key: proxy.externalIP
value: 192.168.178.1
- key: proxy.service.Type
value: LoadBalancer
- name: proxylived
properties:
- key: proxylived.externalIP
value: 192.168.178.1
- key: proxylived.port
value: 31443
The ansible role should execute the following commands
$ helm install proxy . --set proxy.externalIP=192.168.178.1 --set proxy.service.Type=LoadBalancer
$ helm install proxylived . --set proxy.externalIP=192.168.178.1 --set proxylived.port=31443
My problem is, I don't know how to iterate over the objects. I tried the following:
main.yml
---
- name: deploy applications
include_tasks: apps.yml
loop: "{{ apps }}"
loop_control:
loop_var: app
apps.yml
---
- name: deploy application {{ app.name }}
ansible.builtin.command:
argv:
- /usr/bin/helm
- install
- {{ app.name }}
- {{ how to pass here a list of the key value attributes? }}
In a nutshell and not thoroughly tested:
apps.yml
---
- name: create the list of values to set
set_fact:
kvs: "{{ kvs | default([]) + ['--set', item.key ~ '=' ~ item.value] }}"
loop: "{{ app.properties }}"
- name: deploy application {{ app.name }}
vars:
base_cmd:
- "/usr/bin/helm"
- "install"
- "{{ app.name }}"
- "."
ansible.builtin.command:
argv: "{{ base_cmd + kvs }}"

Iterate a list within a dictionary in ansible

I have a variable structured like this. I have successfully used this with with_dict with a single key in the accessible_from
vars:
mysql_dbs:
db1:
user: db1_user
pass: "password"
accessible_from: localhost
db2:
user: db2_user
pass: "password2"
accessible_from: '%'
This is applied using the mysql_db ansible module, like this:
- name: Configure mysql users
mysql_user: name={{ item.value.user }} password={{ item.value.pass }} host={{ item.value.accessible_from | default('localhost')}} priv={{ item.key }}.*:ALL state=present
with_dict: "{{ mysql_dbs }}"
I would like accessible_from to have the ability to be a list. It doesn't matter if it has to be a list, but a single key/value pair is not enough :) So for example:
vars:
mysql_dbs:
db1:
user: db1_user
pass: "password"
accessible_from:
- server1
- server2
- localhost
db2:
user: db2_user
pass: "password"
accessible_from:
- '%'
So - the aim is to create all the DBs and users in one play. I've tried playing around with with_subelements, without success. Is it actually possible to do this? Or is it necessary to restructure the data, or rewrite the play? I'll do that if I have to, but I was wondering if there was another way round it.
First: You may refactor your mysql_dbs into list (because in with_subelements you can't refer items' keys), like:
mysql_dbs:
- name: db1
user: db1_user
pass: "password"
accessible_from:
- server1
- server2
- localhost
- name: db2
user: db2_user
pass: "password2"
accessible_from:
- '%'
And user with_subelements:
- mysql_user: name={{ item[0].user }} password={{ item[0].pass }} host={{ item[1] }} priv={{ item[0].name }}.*:ALL state=present
with_subelements:
- "{{ mysql_dbs }}"
- accessible_from
But this will fail if accessible_from is undefined for any db. You may use skip_missing, but this will skip entire db. So you can't omit accessible_from in this case.
Second: You may use helper set_fact to form a list with key and value, also defaulting accessible_from to localhost. This will work without refactoring your data:
- set_fact:
db_name: "{{ item.key }}"
db_params: "{{ dict(accessible_from=['localhost']) | combine(item.value) }}"
with_dict: "{{ mysql_dbs }}"
register: mysql_dbs_fact
loop_control:
label: "{{ item.key }}"
- debug:
msg: "mysql_user: name={{ item[0].db_params.user }} password={{ item[0].db_params.pass }} host={{ item[1] }} priv={{ item[0].db_name }}.*:ALL state=present"
with_subelements:
- "{{ mysql_dbs_fact.results | map(attribute='ansible_facts') | list }}"
- db_params.accessible_from
loop_control:
label: "{{ item[0].db_name }}->{{ item[1] }}"
Try this:
vars:
mysql_dbs:
db1:
user: db1_user
pass: "password"
accessible_from:
- acc_from: server1
- acc_from: server2
- acc_from: localhost
db2:
user: db2_user
pass: "password"
accessible_from:
- acc_from: '%'
tasks:
- name: Configure mysql users
debug: msg="{{ item.0.user }} password={{ item.0.pass }} host={{ item.1.acc_from }} priv={{ item.0 }}.*:ALL state=present"
with_subelements:
- "{{ mysql_dbs }}"
- accessible_from

Ansible looping over sub-lists

I have a yaml for the creation of a user.
users:
username:
uid: 12345
gid: 6789
secggroups:
- group1
- group3
gecos: user_for_xyz
home: /home/username
I also have a file with just the usernames called users_list. The playbook to create users is as follows:
---
\- name: create users
user: name="{{ item }}" uid={{ users[item]['uid'] }} group={{ users[item][gid] }} comment="{{ users[item]['gecos'] }}" home={{ users[item]['home' }} expires=0
with_items:
\- users_list
How can I loop through the groups to be added to user?
Your playbook is on the right track.
Try this for the users variable:
users:
- username: someusername
uid: 12345
gid: 6789
groups:
- group1
- group3
gecos: "Some user"
home: /home/someusername
- username: someusername
... etc ...
And this for the user play
- name: User creation
user:
name:"{{item.username}}"
groups: "{{item.groups | join(',')}}"
comment: "{{item.name}}"
uid: "{{item.uid}}"
with_items: users
Note that I modified your syntax to not use inline YAML.
Also you may find this users role helpful.
You can solve this using subelements like this:
- name: User creation
user:
name:"{{item.0.username}}"
groups: "{{item.1 }}"
with_subelements:
- "{{ users }}"
- groups
here is a sample to debug, like a directory tree:
vars:
test:
- name: Testing subelements loop
dir: dir0
subdir:
- subdir0
- subdir1
- subdir2
tasks:
- name: Subelements loop sample
debug:
msg: "{{ item.0.dir }}/{{ item.1 }}"
with_subelements:
- "{{ test }}"
- subdir
you can find more here: http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops

Resources