how to omit default message in debug module - debugging

- name:
debug:
with_items: "{{ result.stdout }}"
TASK [pvc_precheck : check_rpm_versionlock warning message] ********************
skipping: [ip]
{
"msg": "Hello world!"
}
Somehow came across this test case. Here, when I run this code in Ansible got output but default message is also printing "Hello world!"
How can I omit this default message, I don't want any custom message as well, just executing it is enough.

Q: "Don't want any custom message as well. Just execution is enough."
A: If 'just execution is enough' use meta: noop e.g. the task below just silently does nothing
- meta: noop
Notes
1. Module debug doesn't require arguments
It's not possible to make debug display nothing. It wouldn't make sense. Why do you want to run debug when you don't want to see anything? No parameter is mandatory. msg defaults to "Hello world!" and var defaults to 0 e.g.
- debug:
gives
msg: Hello world!
2. Condition
Use a condition if you want to skip debug (eliminate message) e.g.
- debug:
msg: "{{ result.stdout }}"
when: debug_enable|default(false)|bool
3. Undefined variable
If the variable might be undefined it's possible to either skip the task e.g.
- debug:
msg: "{{ result.stdout }}"
when: result.stdout is defined
, or display a default value e.g.
- debug:
msg: "{{ result.stdout|default('NOT DEFINED') }}"

Related

Ansible undefined variables in check mode

Consider this playbook:
---
- name: test
hosts: localhost
gather_facts: no
tasks:
- name: foo
shell: echo foo # skipped when running in check mode
register: result
- debug: msg="foo"
when: (result is defined)
and (result.stdout == 'foo')
I thought the is defined would result in short circuit evaluation as in other languages, but it doesn't.
If this is run in check mode, I get:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'
I know I can ignore the error with ignore_errors: "{{ansible_check_mode}}", but I want to learn how to fix this problem.
How do I rewrite the when clause to prevent referencing undefined variables?
Actually, if you debug the var without a condition, you will see it is defined. It simply does not contain a stdout key since the task was skipped. The correct ways to work arround this (non exhaustive list):
- debug: msg="{{ result.stdout | default('no value') }}
when: result.stdout | default('') == 'foo'
- debug: msg="foo"
when:
- result is not skipped
- result.stdout == 'foo'
Note that since your above shell example is not changing anything on the remote target you can also decide to play it even when running in check mode:
- name: foo
shell: echo foo
check_mode: false
changed_when: false
register: result
- debug: msg="foo"
when: result.stdout == 'foo'

Unable to use conditional(when statement and with_file) for ansible role

I want to run ansible role with following condition.
roles:
- role: /home/ansible/myrole_1
when: "'myrole_1' not in item"
with_file:
- "/var/log/play_status.log"
In /var/log/play_status.log, I have entry for myrole_1. Like:-
cat "/var/log/play_status.log".
myrole_1.
myrole_2
But during execution, I am getting error:-
fatal: [localhost]: FAILED! =>
msg: |-
The conditional check ''myrole_1' not in item' failed. The error was: error while evaluating conditional ('myrole_1' not in item): Unable to look up a name or access an attribute in template string ({% if 'myrole_1' not in item %} True {% else %} False {% endif %}).
Make sure your variable name does not contain invalid characters like '-': argument of type 'AnsibleUndefined' is not iterable
If I am using the same condition for task, then it is working fine.
debug:
msg: "I am running..."
when: "'myrole_1' in item"
with_file: /var/log/play_status.log
Is there any limitation for role OR I am doing anything wrong?
Can anyone help me on this?
It looks like when the syntax in your example is eventually using import_roles behind the scenes.
With import_roles (see https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#id9) the role is always executed and each task inside the role is evaluated against the condition.
Let's assume that I have the following tasks in my_role1:
- debug:
msg: "inside role"
- debug:
var: item
with_file:
- "/var/log/play_status.log"
When we call the role like your example does, this is what actually evaluated:
- debug:
msg: "inside role"
when: "'myrole_1' not in item"
- debug:
var: item
with_file:
- "/var/log/play_status.log"
when: "'myrole_1' not in item"
item is not defined in the first debug task, hence the error.
to make the playbook execute the tasks inside the role only when the condition is met, do this:
tasks:
- include_role: /home/ansible/myrole_1
when: "'myrole_1' not in item"
with_file:
- "/var/log/play_status.log"

register with loop not giving expected output - Ansible

I am writing a playbook that gives me the status of list of processes in loop but the output is not coming a desired
I am using ansible 2.7.1
---
- hosts: test_group
gather_facts: false
tasks:
- name: checking status
shell: /etc/init.d/{{ item }} status
register: output
loop:
- gdac
- scac
- name : print status
debug:
msg: "{{ item.stdout }}"
loop: "{{ output.results }}"
expecting output like (which given me the stdout or stdout_lines from the registered variable.
"msg":"Poller is Running\nSpooler is Running"
"msg": scac.db1: 3 of 3 running ( 7067 7060 7040 )\nayld.db1: 1 of 1 running ( 7114 )\nscac.db2: 3 of 3 running ( 7227 7216 7203 )\nayld.db2: 0 of 1 running
but am getting error
fatal: [test01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined
``
In your second task, you declared loop as an option of the debug module.
It should be an option of the task, not of the module. Since there is no loop declaration for the task, item is undefined.
You just have to fix your indentation:
- name : print status
debug:
msg: "{{ item.stdout }}"
loop: "{{ output.results }}"

Ansible check if variables are set

I want to automate the installation process of our software for our client. Therefore I wrote an Ansible playbook which has a task which should check if all the mandatory variables are set:
- name: Check environment variables.
hosts: all
vars_files:
- required_vars.yml
tasks:
- fail: msg="Variable '{{ item }}' is not defined"
when: item not in hostvars[inventory_hostname]
with_items:
- required_vars
The required_vars.yml looks like this:
required_vars:
- APPHOME: /home/foo/bar
- TMPDIR: /home/foo/bar/tmp
When I execute the playbook via ansible-playbook -i inventory/dev.yml playbook.yml I get the following error:
TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************ok: [localhost]
TASK [fail] ************************************************************************************************************************************************************************************************************************************failed:
[localhost] (item=required_vars) => {"changed": false, "failed": true, "item": "required_vars", "msg": "Variable 'required_vars' is not defined"}
It is obvious that I am doing something wrong, but I cannot point to the error. Can you help me please?
Edit: the accepted answer helped me out. Thank you.
But I have two more questions:
The executed task says:
TASK [fail]
skipping: [some_ip] => (item=/root)
skipping: [some_ip] => (item=TMPDIR: /home/foo/bar/tmp)
It is getting skipped because all variables are set, correct?
I think I figured out how to print the correct message, if the variable is not set:
- name: Check environment variables.
hosts: all
vars_files:
- required_vars.yml
tasks:
- fail:
msg: "Variable '{{ item }}' is not defined"
with_items: "{{ required_vars }}"
when: item is undefined
Correct? Or is there a better solution?
Two problems here:
You want to iterate over the value of required_vars variable value, so you need to provide it as an argument to with_items: "{{ required_vars }}":
with_items: "{{ required_vars }}"
Currently you are providing a list of a single element with a statically defined string required_vars.
You need to change the data type of the elements in your required_vars list to strings:
required_vars:
- "APPHOME: /home/foo/bar"
- "TMPDIR: /home/foo/bar/tmp"
Currently (because of : followed by space) you defined dictionaries, so for example in the first iteration item will have a value of { "APPHOME": "/home/foo/bar" }, which will then always fail on the when condition.
Bonus problem:
you defined a message in the form "Variable '{{ item }}' is not defined";
Ansible reports Variable 'required_vars' is not defined;
the above is not an error, as you think ("I get the following error"), but a correct result of the fail module with the message you defined yourself.
Since you have only one value for 'with_items' I think it should look like this:
with_items: "{{ required_vars }}"
On one line and with the brackets and quotation marks. Once you have more then one item, you can use the list like you did:
with_items:
- "{{ one }}"
- "{{ two }}"

Ansible: how to register variable in uri module?

This is my code or checking wheter the website is valid and running:
- hosts: master
become: true
tasks:
- name: "Checking server availibility"
uri:
url: http://www.google.pl
timeout: 5
register: command_result
ignore_errors: yes
- debug: msg= "{{ command_result }}"
So command_result variable always returns this output regardless of the website availability:
ok: [Centos.v1] => {
"changed": false,
"msg": "Hello world!"
}
I would like to know if the first task was succesful or not but i can't do that when the output of registered variable is always the same.
How to setup my variable properly to read the return code?
and also
Is there some other way to check if an error occured in the previous task?
This has nothing to do with registering.
Remove the space character after msg=, otherwise you pass an empty string to the debug module and it prints Hello World! as a result (its default behaviour).
Better yet, use var parameter and YAML syntax:
- debug:
var: command_result

Resources