Registering multiple variables in a loop - ansible

I have a variable yaml file:
---
apps:
- client
- node
- gateway
- ic
- consul
and this task:
- name: register if apps exists
stat: path="/etc/init.d/{{ item }}"
with_items: apps
register: "{{ item.stat.exists }}exists"
I need to end up with a variable for each app with a value of true or false whether the file exists or not:
clientexists = true
nodeexists = true
gatewayexists = false
icexists = true
consulexists = false
For some reason, the item and exists concat is not working.
How can I achieve that??

Try this hope this will help you out. While looping in Stats.yml then msg field will contain your desired output.
Variables.yml Here we are defining variables
---
apps:
- client
- node
- gateway
- ic
- consul
Stats.yml
---
- hosts: localhost
name: Gathering facts
vars_files:
- /path/to/variables.yml
tasks:
- name: "Here we are printing variables"
debug:
msg: "{{apps}}"
- name: "Here we are gathering stats and registering it"
stat:
path: "/etc/init.d/{{item}}"
register: folder_stats
with_items:
- "{{apps}}"
- name: "Looping over registered variables, Its msg field will contain desired output"
debug:
msg: "{{item.invocation.module_args.path}}: {{item.stat.exists}}"
with_items:
- "{{folder_stats.results}}"
...
folder_stats will contain whole result, for individually referring to single - single result You can use it like this in you playbook.
folder_stats.results[0].stat.exists
folder_stats.results[1].stat.exists
folder_stats.results[2].stat.exists
folder_stats.results[3].stat.exists

Related

Ansible to print the variable value having another variable inside it

In a.yml file, I have stored data like below
---
Server:
"Node1" : ["Node1", "Owner1", "ID1"]
"Node2" : ["Node2", "Owner2", "ID2"]
Now, in xyz.yml playbook, I tried to debug a variable as below and I am passing the Node_Name in commandline (ansible-playbook xyz.yml -e "Node_Name=Node1")
---
- name: "Print Variable value"
hosts: all
gather_facts: no
vars:
Node_Name: Node
ID_Name: "{{ Server.{{ Node_Name }}[2] }}"
tasks:
- name: "Print the id"
debug:
msg:
- "The id is {{ ID_Name }}"
But this is failing with error - Template error while templating string :expected name or number
Can someone please help to fix this and let me know how can I get the ID printed as output. Here expected output is ID1
You never nest {{...}} markers.
Recall that to access a nested variable. the syntax Server.Node1 is exactly equivalent to Server["Node1"]. The second syntax allows us to make use of variables (and string interpolation) on the key, so we can write:
Server[Node_Name]
In other words:
- name: "Print Variable value"
hosts: all
gather_facts: no
vars:
Node_Name: Node
ID_Name: "{{ Server[Node_Name][2] }}"
tasks:
- name: "Print the id"
debug:
msg:
- "The id is {{ ID_Name }}"

Ansible - 'block' is not a valid attribute for a Play

I'm trying to figure out how to resolve the Error message: ERROR! 'block' is not a valid attribute for a Play
How do I achieve this? I'm trying to use the "block" condition, but it's not working. I'm not sure what to search anymore.
Code:
---
- name: Get diagnostic info block
block:
- name: Get top process diagnostic info
script: library/diag_top_processes.ps1
failed_when: false
changed_when: false
register: diag_top_proc_out
- name: Get memory and pagefile usage diagnostic info
script: library/diag_memory_and_pagefile.ps1
failed_when: false
changed_when: false
register: diag_mem_page_out
- name: Get CPU info
script: library/diag_cpu_info.ps1
failed_when: false
changed_when: false
register: diag_cpu_info_out
- name: Init diag message
set_fact:
diag_msg: "Diagnostic information:"
proc_info:
"{{ diag_top_proc_out.stdout|regex_replace('\r\n|\n|\r', '') }}"
cpu_info: "{{ diag_cpu_info_out.stdout_lines | join(' ') }}"
- name: Add top process info
set_fact:
diag_msg: "{{ [diag_msg, proc_info] | join(' ') }}"
- name: Add memory info
set_fact:
diag_msg:
"{{ [diag_msg, diag_mem_page_out.stdout_lines[0]] | join(' ') }}"
- name: Add CPU info
set_fact:
diag_msg:
"{{ [diag_msg, cpu_info] | join(' ') }}"
rescue:
- name: Diag info collection failed, setup variable
set_fact:
diag_failed: true
Error using 'block' for a Play:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'block' is not a valid attribute for a Play
The error appears to be in '/home/keith/Ansible/ansible-role-high-cpu-usage-win/tasks/get_diagnostic.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Get diagnostic info block
^ here
This file only correct if you do
- include_tasks path/to/get_diagnostic.yml
As lopez said, it cannot be called with ansible-playbook command line.
Rewrite play as:
---
- name: Play
tasks:
- name: Get diagnostic info block
block:
- name: Get top process diagnostic info
script: library/diag_top_processes.ps1
failed_when: false
changed_when: false
register: diag_top_proc_out
....
rescue:
- name: Another tasks
....

Playbook is getting hanged for some time and resuming itself after that

When i used WHEN condition for block which contains a lot of tasks the playbook is getting stuck before entering into that block and staying in the hung state for a longtime.
i have added ANSIBLE_SSH_PIPELINING=True in ansible.cfg, but it's not working.
- block:
- block:
- name: Interface Status Details
include: interface_details.yml
register: result
- name: Interface Result Global
set_fact:
LowerCaseResult: "{{ result.stdout }}
- debug: var=LowerCaseResult
- block:
- name: Calling Dependency Playbook
include: one_test.yml
register: result
- debug: var=result
when: "'is up' in LowerCaseResult"
- block:
- name: Calling Dependency Playbook
include: two_test.yml
register: cmd_result
- debug: var= cmd_result
when: "'is up' not in LowerCaseResult"
when: true
Debug is not printing the playbook is getting stuck for a long time no error message is produced.
(ansible 2.7.9)
ansible-lint does not complain about nested block (interesting!) See nested blocks lose conditions on parent blocks #12395 (ansible locked and limited conversation to collaborators on Apr 25) This might cause problems until resolved.
Problems in the code
1) Nothing is registered by include use include_tasks
- name: Interface Status Details
include_tasks: interface_details.yml
register: result
2) The closing quotation is missing; If there is no stdout in this result the task will fail
- name: Interface Result Global
set_fact:
LowerCaseResult: "{{ result.stdout }}
correct, set default to 'is NOT up'
- name: Interface Result Global
set_fact:
LowerCaseResult: "{{ result.stdout|default('is NOT up') }}"
3) Extra space after var=
- debug: var= cmd_result
correct
- debug: var=cmd_result
The play below might be working
- hosts: localhost
tasks:
- block:
- block:
- name: Interface Status Details
include_tasks: interface_details.yml
register: result
- debug: var=result
- name: Interface Result Global
set_fact:
LowerCaseResult: "{{ result.stdout|default('is NOT up') }}"
- debug: var=LowerCaseResult
- block:
- name: Calling Dependency Playbook
include_tasks: one_test.yml
register: result
- debug: var=result
when: "'is up' in LowerCaseResult"
- block:
- name: Calling Dependency Playbook
include_tasks: two_test.yml
register: cmd_result
- debug: var=cmd_result
when: "'is up' not in LowerCaseResult"
when: true

How to pass results back from include_tasks

Started to learn ansible yesterday, so I believe I may risk XY problem here, but still…
The main yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
register: res
- name: print
debug: msg="{{res.results}}"
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: print
debug:
msg: "IP: {{ec2.instances.0.public_ip_address}}"
So that's that IP that I'd like to have on the top level. Haven't found anything right away about return values of the include block…
Well, I've found some way that suits me, maybe it's even canonical?
main.yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
vars:
ec_results: {}
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
register: res
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: update
set_fact:
ec_results: "{{ ec_results|combine({ item.key: ec2.instances.0.public_ip_address }) }}"
Thanks for you answer - this helped me to find an answer to my issue when my task was called in a loop - my solution was just to use lists so for your example above, the solution would be:
The main yml:
- hosts: localhost
vars_files:
[ "users.yml" ]
vars:
ec_results: []
tasks:
- name: manage instances
#include_tasks: create_instance.yml
include_tasks: inhabit_instance.yml
with_dict: "{{users}}"
- name: print
debug: msg="{{ec_results}}"
inhabit_instance.yml:
- name: Get instance info for {{ item.key }}
ec2_instance_facts:
profile: henryaws
filters:
"tag:name": "{{item.key}}"
instance-state-name: running
register: ec2
- name: Add IP to results
set_fact:
ec_results: "{{ ec_results + [ec2.instances.0.public_ip_address] }}"
In my code, include_tasks was in a loop so then ec_results contained a list of the results from each iteration of the loop

Saving state of services in Linux services in Ansible

I am trying to save whether services are in running state or stopped. I applied the following login.
---
- hosts: all
vars:
myName: Nikunj
result: ""
tasks:
- name: Initialize empty Started and Stopped list of strings
set_fact:
started: []
stopped: []
- name: Saving context of the VM
shell: service tomcat status
ignore_errors: true
register: result
when: ' "running" in "{{result.stdout}}" '
set_fact:
started : "{{ started }} + ['tomcat']"
- debug:
msg: "{{started}}"
shell command here is giving me eroor.
---
- hosts: all
vars:
myName: Nikunj
result: ""
tasks:
- name: Initialize empty Started and Stopped list of strings
set_fact:
started: []
stopped: []
- name: Saving context of the VM
shell: service tomcat status
ignore_errors: true
register: result
- name: setting fact
set_fact:
started : "{{ started }} + ['tomcat']"
when: ' "running" in "{{result.stdout}}" '
- debug:
msg: "{{started}}"
This will work. set_fact is an individual module.

Resources