Ansible Exit code fail - ansible

Guys I have code like:
- name: Running ccache for {{ SOMETHING }}
shell: "{{ item }}"
loop:
- command1
- command2
- command3
when: stat_result.stat.exists
failed_when: >
(rc != 0) or
(rc != 8)
I tried to get the following logic work:
"If command's exit code is not 0 or 8 , then success."
However only thing I get from this is:
[0;31mfatal: [someIPhere]: FAILED! => {"msg": "The conditional check '(rc != 0) or (rc != 8)' failed. The error was: error while evaluating conditional ((rc != 0) or (rc != 8)): 'rc' is undefined"}[0m
Any advices?

You need to register a variable to contain the results of the commands. This variable will contain values for each item in the loop, including rc.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables
- name: Running ccache for {{ TENANT }}
shell: "{{ item }}"
loop:
- touch /tmp/test
- touch /tmp/test
- touch /tmp/test
when: stat_result.stat.exists
register: command_result
failed_when: command_result.rc not in [ 0, 68 ]

Related

The condition in my playbook is not working

I have this simple playbook:
---
- name: JVM Status
shell: "ps -ef | grep {{item}} | grep -v grep | grep {{wasUser}} | wc -l"
loop: "{{jvmStatusList}}"
become: yes
become_user: "{{wasUser}}"
register: statusResult
- debug:
msg: "{{items.item}} is started"
loop: "{{statusResult.results}}"
when: item.stdout != 0
- debug:
msg: "{{item.item}} is stopped"
loop: "{{ statusResult.results }}"
when: item.stdout == 0
...
and when i run it, it doesnt take into account the condition.
I have verified what my item.stdout returns and 2 items are started and 1 is stopped but all three are returned in the first debug while the second one is being skipped by all.
Can anyone help?
thought maybe the problem was because my stdout result was not trimmed. so i tried different syntaxes including item.stdout | trim but no change. Also tried using item.stdout is 1 instead of != 0.

'dict object' has no attribute 'stdout' in Ansible

Could you please help me with identifying the error
'dict object' has no attribute 'stdout'
which I am getting while running an Ansible playbook. Please see the error below:
{"msg": "The conditional check '(result.stdout == 1) and (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat')' failed. The error was: error while evaluating conditional ((result.stdout == 1) and (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat')): 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/etc/ansible/playbooks/dev_patching_security_upgrade_version2.yml': line 47, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Reboot CentOS Server if Necessary\n ^ here\n"}
I am pasting the relevant part of the code below:
- name: check to see if we need a reboot in Centos
shell:
"needs-restarting; echo $?"
register: result
ignore_errors: yes
when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'
- name: display result
debug:
var: result.stdout
when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'
- name: Reboot CentOS Server if Necessary
command: shutdown -r now "Ansible Updates Triggered reboot"
become: true
async: 30
poll: 0
when: (result.stdout == 1) and (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat')
Thanks in advance
This is because the result set might not have a dictionary key stdout. To debug a result set you may use
- name: Show result
debug:
var: result
Since the shell module already provides the return code within the Return Values, reboot checks are simple as
- name: Check if reboot_required
shell:
cmd: "needs-restarting -r"
changed_when: false
failed_when: reboot_required.rc != 0 and reboot_required.rc != 1
check_mode: false
register: reboot_required
- name: Report reboot_required
debug:
msg: "{{ reboot_required.rc | bool }} "
changed_when: reboot_required.rc == 1
check_mode: false
and resulting into an output of in example
TASK [Report reboot_required] **************************************************
ok: [test1.example.com] => {
"msg": "True "
}
ok: [test2.example.com] => {
"msg": "False "
}
You could dense down the tasks to one by using one
needs-restarting -r || /usr/sbin/shutdown -r now "Ansible Updates Triggered reboot"
Further Q&A
How to determine if system needs a reboot?
Ansible - How to reboot the server based on condition?

Loop over list and evaluate element property in Ansible

I have a list with shell lines that I want to execute on inventory hosts so I can determine if the database is working. For the test purposes I have 1 server with PostgreSQL and 1 with MySQL.
This is my playbook so far:
- name: Check db statuses
shell: "{{ item }}"
loop:
- ps -fp $(pgrep -u postgres) | grep /usr/lib/postgresql
- ps -fp $(pgrep -u mysql) | grep mysqld
register: http
ignore_errors: yes
changed_when: item.failed == false
this is failing with:
{
"http": {
"failed": true,
"msg": "The conditional check 'item.failed == false' failed. The error was: error while evaluating conditional (item.failed == false): 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'failed'"
}
}
I want to assign only the item.failed==false result in the register variable (http) but ignore the failed ones.
You can't select what will be registered in a loop. Instead, you'll have to evaluate the registered results in the next task(s), e.g.
- hosts: localhost
tasks:
- command: "{{ item }}"
loop:
- /bin/true
- /bin/false
register: http
ignore_errors: true
- debug:
msg: "{{ item.item }} failed: {{ item.failed }}"
loop: "{{ http.results }}"
loop_control:
label: "{{ item.cmd }}"
gives
ok: [localhost] => (item=['/bin/true']) =>
msg: '/bin/true failed: False'
ok: [localhost] => (item=['/bin/false']) =>
msg: '/bin/false failed: True'

What attribute should be checked for a registered variable under two Ansible when conditions

How do we check for a registered variable if only one of the two conditions turns out to be true having the same registered variable?
Below is my playbook that executes only one of the two shell modules.
- name: Check file
shell: cat /tmp/front.txt
register: myresult
when: Layer == 'front'
- name: Check file
shell: cat /tmp/back.txt
register: myresult
when: Layer == 'back'
- debug:
msg: data was read from back.txt and print whatever
when: Layer == 'back' and myresult.rc != 0
- debug:
msg: data was read from front.txt and print whatever
when: Layer == 'front' and myresult.rc != 0
Run the above playbook as
ansible-playbook test.yml -e Layer="front"
I do get error that says myresult does not have an attribute rc. What is the best way to print debug one statements based on the condition met?
I tried myresult is changed but that too does not help. Can you please suggest.
Use ignore_errors: true and change the task order. Try as below.
- name: Check file
shell: cat /tmp/front.txt
register: myresult
when: Layer == 'front'
- debug:
msg: data was read from front.txt and print whatever
when: not myresult.rc
ignore_errors: true
- name: Check file
shell: cat /tmp/back.txt
register: myresult
when: Layer == 'back'
- debug:
msg: data was read from back.txt and print whatever
when: not myresult.rc
ignore_errors: true

template error while templating string: unexpected char u with ansible playbook?

I have a ansible playbook with below tasks and when I run it, I get an error:
tasks:
- shell: timeout 5 ps aux | grep app
register: result
ignore_errors: True
- name: Run your shell command
shell: "(ssh -o StrictHostKeyChecking=no abc.com 'ls -1 /var/lib/jenkins/workspace/copy/stuff/*' | parallel -j20 'scp -o StrictHostKeyChecking=no abc.com:{} /data/records/')"
when: result.rc != 124 && result.rc != 0
Error is:
{"msg": "The conditional check 'result.rc != 124 && result.rc != 0' failed. The error was: template error while templating string: unexpected char u'&' at 23. String: {% if result.rc != 124 && result.rc != 0 %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/var/lib/jenkins/workspace/proc/test.yml': line 10, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: copy files\n ^ here\n"}
Any idea what wrong I am doing here?
Correct syntax is
when:
- result.rc != 124
- result.rc != 0
or
when: (result.rc != 124) and (result.rc != 0)

Resources