Testing a task result which uses a loop? - linux

So the first task is always skipped because RedHat 8 is detected which should trigger the fail module to run but this gets skipped also. Is works fine without the loop though.
- name: installing packages
hosts: ansible2
ignore_errors: true
vars_files: varsfile
tasks:
- name: install software based on OS distro and version
yum:
name: "{{ item.name }}"
state: latest
loop: "{{ packages }}"
when: >
( ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=') )
or
( ansible_distribution == "CentOS" and ansible_distribution_major_version | int >= 8 )
register: result
- fail:
msg: error {{ ansible_hostname }} does not meet minimum requirements
when: result is skipped

Regarding
So the first task is always skipped because RedHat 8 is detected which should trigger the fail module to run but this gets skipped also.
with a small test setup and a debug task for the variable name to debug
---
- hosts: test.example.com
become: false
gather_facts: true
tasks:
- name: Install software based on OS distro and version
debug:
msg: "{{ ansible_distribution }} {{ ansible_distribution_major_version }}"
register: result
when: ansible_distribution == "RedHat" and ansible_distribution_major_version is version('12', '>=')
with_items: ['pkg1', 'pkg2']
- name: Show result
debug:
var: result
- fail:
msg: "Error: {{ ansible_hostname }} does not meet minimum requirements"
when: result.results[0].skipped
resulting into an output of
TASK [Gathering Facts] ********************************
ok: [test.example.com]
TASK [Show result] ************************************
ok: [test.example.com] =>
result:
changed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
item: pkg1
skip_reason: Conditional result was False
skipped: true
...
TASK [fail] *******************************************
fatal: [test.example.com]: FAILED! => changed=false
msg: 'Error: test does not meet minimum requirements'
you can see that the loop will create a list.
- name: Show result
debug:
var: result.results | type_debug
TASK [Show result] ****************
ok: [test.example.com] =>
result.results | type_debug: list
Therefore you need to set the Conditional to when: result.results[0].skipped.
Regarding
Is works fine without the loop though.
it is recommended to simplify your use case with the following approach
- name: Install software based on OS distro and version
yum:
name: "{{ packages }}"
state: latest
according the yum module Notes
When used with a loop: each package will be processed individually, it is much more efficient to pass the list directly to the name option.
and as it is faster and consumes less resources. Furthermore the result set and conditionals are less complex.

Related

How can I pass a varialbe set by the set_fact module to the Jinja2 template?

I have a role to setup NATS cluster,, I've used host_vars to define which node is the master node like below:
is_master: true
Then in the setup-nats.yml task, I used the following to extract the master node's IP address based on the host_var I've set and then used it as a variable for the Jinja2 template, however, the variable doesn't get passed down to the template and I get the `variable 'master_ip' is undefined.
- name: Set master IP
set_fact:
set_master_ip: "{{ ansible_facts['default_ipv4']['address'] }}"
cacheable: yes
when: is_master
- name: debug
debug:
msg: "{{ set_master_ip }}"
run_once: true
- name: generate nats-server.conf for the slave nodes
template:
src: nats-server-slave.conf.j2
dest: /etc/nats-server.conf
owner: nats
group: nats
mode: 0644
when:
- is_master == false
vars:
master_ip: "{{ set_master_ip }}"
notify: nats-server
The variable is used like below in the Jinja2 template:
routes = [
nats-route://ruser:{{ nats_server_password }}#{{ master_ip }}:6222
]
}
Questions:
Is this approach according to the best practices?
What is the correct way of doing the above so the variable is passed down to the template?
Test Output:
I'm using Molecule to test my Ansible and even though in the debug task the IP address is visible, it doesn't get passed down to the template:
TASK [nats : Set master IP] ****************************************************
ok: [target1]
skipping: [target2]
skipping: [target3]
TASK [nats : debug] ************************************************************
ok: [target1] =>
msg: 10.0.2.15
TASK [nats : generate nats-server.conf for the slave nodes] ********************
skipping: [target1]
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: {{ set_master_ip }}: 'set_master_ip' is undefined
fatal: [target2]: FAILED! => changed=false
msg: 'AnsibleUndefinedVariable: {{ set_master_ip }}: ''set_master_ip'' is undefined'
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: {{ set_master_ip }}: 'set_master_ip' is undefined
fatal: [target3]: FAILED! => changed=false
msg: 'AnsibleUndefinedVariable: {{ set_master_ip }}: ''set_master_ip'' is undefined'
Any help is appreciated, thanks in advance.
UPDATE: I suspect the issue has something to do with the variable scope being in the host context but cannot find a way to fix it ( I might be wrong though).
Far from being best practice IMO but answering your direct question. Your problem is not passing the variable to your template but the fact it is not assigned to all hosts in your play loop (and hence is undefined on any non master node). The following (untested) addresses that issue keeping the same task structure.
- name: Set master IP for all nodes
ansible.builtin.set_fact:
master_ip: "{{ hostvars | dict2items | map(attribute='value'
| selectattr('is_master', 'defined') | selectattr('is_master')
| map(attribute='ansible_facts.default_ipv4.address') | first }}"
cacheable: yes
run_once: true
- name: Show calculated master IP (making sure it is assigned everywhere)
ansible.builtin.debug:
msg: "{{ master_ip }}"
- name: generate nats-server.conf for the slave nodes
ansible.builtin.template:
src: nats-server-slave.conf.j2
dest: /etc/nats-server.conf
owner: nats
group: nats
mode: 0644
when: not is_master | bool
notify: nats-server
Ideas for enhancement (non exhaustive):
Select your master based on a group membership in the inventory rather than on a host attribute. This makes gathering the ip easier (e.g. master_ip: "{{ hostvars[groups.master | first].ansible_facts.default_ipv4.address }}"
Set the ip as a play var or directly inside the inventory for the node group rather than in a set_fact task.

Ansible loop over a list to define a dynamic dictionary

I am trying to define a dictionary FACTS_VAR in which the key must contain the word SUB. Argument_value and the value is true but when I loop over the variable Argument
hosts: all
gather_facts: true
vars:
Argument:
- value1
- value2
tasks:
- name: DEFINING A variable
set_fact:
FACTS_VAR: {'SUB..item': true}
loop: "{{Argument}}"
- debug:
var: FACTS_VAR
I got this result so I don't know what is missing there. I am expecting to get a dictionary like this
FACTS_ENTRY:
SUB.value1: true
SUB.value2: true
TASK [debug] *****************************************************************************************************************
ok: [control] => {
"FACTS_ENTRY": {
"SUB..item": true
}
}
ok: [ansible4] => {
"FACTS_ENTRY": {
"SUB..item": true
}
Create the keys
facts_entry_keys: "{{ ['sub']|product(argument)|map('join','.')|list }}"
gives
facts_entry_keys:
- sub.value1
- sub.value2
Create the dictionary
facts_entry: "{{ dict(facts_entry_keys|product([true])) }}"
gives
facts_entry:
sub.value1: true
sub.value2: true
Example of a complete playbook
- hosts: localhost
vars:
argument:
- value1
- value2
facts_entry_keys: "{{ ['sub']|product(argument)|map('join','.')|list }}"
facts_entry: "{{ dict(facts_entry_keys|product([true])) }}"
tasks:
- debug:
var: facts_entry
From your current description I understand only that you probably like to define variables somehow dynamically.
The following example could give some guidance.
---
- hosts: test
become: false
gather_facts: false
vars:
ARG:
- val1
- val2
tasks:
- name: Dynamically define variable
set_fact:
FACTS_VAR: "{ 'SUB_{{ item }}': true }"
loop: "{{ ARG }}"
- debug:
var: FACTS_VAR
resulting into an output of
TASK [Dynamically define variable] **
ok: [test.example.com] => (item=val1)
ok: [test.example.com] => (item=val2)
TASK [debug] ************************
ok: [test.example.com] =>
FACTS_VAR:
SUB_val2: true
Please take note that according your current description there can be only one value as result.
Further Q&A
Build variable name at play level

Unable to fail terminate Ansible playbook upon string match condition

I wish to fail and terminate my ansible playbook when Number=TKT334
Below is my playbook:
---
- name: "Play 1"
hosts: localhost
any_errors_fatal: True
serial: 1
tags: always
tasks:
- name: Setting string variable.
set_fact:
inlinevar: '2020-06-10 20:22:16\tTKT334\tKIRAN\tDeployed\tPRODUCTION'
- name: Setting string variable.
set_fact:
environment: 'PRODUCTION'
- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and environment == "PRODUCTION")
- debug:
msg: This is FINAL inlinevar `{{ inlinevar }}`
rescue:
- name: Print custom conditional debug message
fail:
msg: >-
{{
command_result_app.stdout is search( Number ) |
ternary(
"This CR is already Deployed. Hence retry with a Unique CR Number.",
"The Dtabase is not reachable. Unable to connect To the Database."
)
}}
However, when I run the playbook I do not fail and continues to print the debug:
I was expecting it to fail but it shows ansible run as successful and does not terminate the playbook executions.
See the output below:
ansible-playbook test.yml -e Number=TKT334
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Play 1] *************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}] ***
changed: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "This is FINAL inlinevar `2020-06-10 20:22:16\\tTKT334\\t KIRAN\\tDeployed\\tPRODUCTION`"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Can you please suggest?
environment is a special option used to set environment variables for tasks, e.g.
- name: Do something
shell: echo "whatever"
environment:
http_proxy: http://my.server.com:8080
no_proxy: my.domain.com
In your case, environment is always empty after set_fact and environment == "PRODUCTION" is always false.
Rename your variable to something else, don't use environment (see this other question for more discussion), e.g.
[...]
- name: Setting string variable.
set_fact:
deploy_environment: 'PRODUCTION'
- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and deploy_environment == "PRODUCTION")
[...]

ansible yum check update parse output to have list of packages

I want to parse the output of yum check-update ansible equivalent to get only the list of package in human readable format.
My code so far:
- name: check for updates
hosts: localhost
gather_facts: true
tasks:
- name: check for updates (yum)
yum: list=updates update_cache=true
register: yumoutput
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
- debug: msg={{ yumoutput.stdout | from_json }}
but I get:
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ yumoutput.stdout | from_json }}): expected string or buffer"}
EDIT: the complete playbook:
---
- name: check for updates
hosts: localhost
gather_facts: true
tasks:
- name: check for updates (yum)
yum: list=updates update_cache=true
register: yumoutput
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
- debug: var=yumoutput
msg: "{{ yumoutput.results | map(attribute='name') | list }}
yum module does not register stdout key ― you can see it using debug: var=yumoutput.
You need to extract the package names from the list of dictionaries, for example:
debug:
msg: "{{ yumoutput.results | map(attribute='name') | list }}"
Some minor syntax errors in the complete playbook. This did it for me
---
- name: check for updates
hosts: localhost
gather_facts: true
tasks:
- name: check for updates (yum)
yum: list=updates update_cache=true
register: yumoutput
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
- debug:
msg: "{{ yumoutput.results | map(attribute='name') | list }}"
In addition to the above-mentioned solution. You can use call back plugin for ease of reading the output.
Below is one of the human-readable call-back plugin:
https://github.com/n0ts/ansible-human_log
More info about call back plugins:
http://docs.ansible.com/ansible/devel/plugins/callback.html

Why Ansible didn't see attribute in variable?

I have Ansible role "db" with simple task:
- name: Check repos
apt_repository: repo="{{ item.repo }}" state={{ item.state }}
with_items:
- "{{ apt_repos }}"
In /defaults/mail.yml:
apt_repos:
# Percona
- { state: present, repo: 'deb http://repo.percona.com/apt wheezy main', keyserver: 'keyserver.ubuntu.com', key: '1C4CBDCDCD2EFD2A', needkey: True }
- { state: present, repo: 'deb-src http://repo.percona.com/apt wheezy main', needkey: False }
When i try to run this ansible-playbook:
---
- hosts: test
roles:
- db
i see error:
fatal: [10.10.10.10] => One or more undefined variables: 'unicode object' has no attribute 'repo'
FATAL: all hosts have already failed -- aborting
But i have another role with same task and variable and it work perfectly. What's wrong?
You want to be doing this:
with_items: apt_repos
apt_repos is a list. By referencing it as - "{{ apt_repos }}" the extra - is turning it into a list of lists. You also don't need the quotes or braces in this case - those are pretty much just redundant in this type of situation.

Resources