need help with if condition in jinja template

31 views
Skip to first unread message

Kenady Inampudi

unread,
Feb 26, 2023, 10:32:26 AM2/26/23
to Ansible Project
I am trying to create a report of failed servers output.

Here is my playbook
---
- hosts: all
  gather_facts: no
  tasks:
    - name: "Runing mksysb backup"
      mksysb:
        name: "{{inventory_hostname}}.mksysb"
        storage_path: /backup
        exclude_files: yes
      register: mksys
      failed_when: '"mksysb: Backup Completed Successfully" not in mksys.msg'
    - name: Copy output to file
      copy:
       dest: /home/user1/mksysb_error_report.out
       content: |-
         {% for host in ansible_play_hosts_all %}
         {{ '###' }}{{ host }}{{ '###' }}
 {{ '------------------------------------' }}
         {{ hostvars[host]['mksys']['msg'] }}
         {% endfor %}
  run_once: True
  delegate_to: localhost

This gives me output of all the hosts, where i am looking to create the output of failed hosts 

i tried something like this which gives me nothing 

         {% for host in ansible_play_hosts_all %}
        {% if 'Completed Successfully' in hostvars[host]['mksys']['msg'] %}
         {{ '###' }}{{ host }}{{ '###' }}
 {{ '--------------------------' }}
         {{ hostvars[host]['mksys']['msg'] }}
 {% endif %}
         {% endfor %}

Could some help me out to put condition so i can get the output of just the failed hosts.

Dick Visser

unread,
Feb 26, 2023, 2:47:32 PM2/26/23
to ansible...@googlegroups.com
Clearly your when condition doesn't match. But in order to know why, we'd need to see the content of a failed backup, and maybe a sample success output as well. 
So post that/those.  

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e7637b0d-400c-4d95-aa99-edcd7c49f5ddn%40googlegroups.com.
--
Sent from Gmail Mobile

Kenady Inampudi

unread,
Feb 26, 2023, 5:33:19 PM2/26/23
to Ansible Project
Please find the task output below, usually all the jobs go to changed state true and the play successfully so i have passed a failed when condition to identify the failure 

----------------------------------------------------------------------------------
TASK [Runing mksysb backup] ****************************************************
changed: [server1]
fatal: [server2]: FAILED! => {"changed": true, "failed_when_result": true, "msg": "\nCreating information file (/image.data) for rootvg.\n\nCreating list of files to back up \n\nBacking up 153927 files..............................\n110142 of 153927 files backed up (71%)....\n\n0512-003 mksysb may not have been able to archive some files.\nThe messages displayed on the Standard Error contained additional\ninformation.\n"}

TASK [Copy output to file] *****************************************************
changed: [server1 -> localhost)]
------------------------------------------------------------

Failed output


{
  "changed": true,
  "msg": "\nCreating information file (/image.data) for rootvg.\n\nCreating list of files to back up \n\nBacking up 153927 files..............................\n110142 of 153927 files backed up (71%)....\n\n0512-003 mksysb may not have been able to archive some files.\nThe messages displayed on the Standard Error contained additional\ninformation.\n",
  "invocation": {
    "module_args": {
      "name": "server2.mksysb",
      "storage_path": "/backup",
      "exclude_files": true,
      "backup_crypt_files": true,
      "backup_dmapi_fs": true,
      "create_map_files": false,
      "exclude_wpar_files": false,
      "extended_attrs": true,
      "new_image_data": true,
      "software_packing": false,
      "use_snapshot": false
    }
  },
  "_ansible_no_log": null,
  "failed_when_result": true
}

===============================================================================
Sucessfull output

{
  "changed": true,
  "msg": "\nCreating information file (/image.data) for rootvg.\n\nCreating list of files to back up \n\nBacking up 88146 files................\n\n88146 of 88146 files backed up (100%)\n0512-038 mksysb: Backup Completed Successfully.\n",
  "invocation": {
    "module_args": {
      "name": "server1.mksysb",
      "storage_path": "/backup",
      "exclude_files": true,
      "backup_crypt_files": true,
      "backup_dmapi_fs": true,
      "create_map_files": false,
      "exclude_wpar_files": false,
      "extended_attrs": true,
      "new_image_data": true,
      "software_packing": false,
      "use_snapshot": false
    }
  },
  "_ansible_no_log": null,
  "failed_when_result": false
}

--------------


Todd Lewis

unread,
Feb 26, 2023, 6:22:57 PM2/26/23
to Ansible Project
I tried running your code on my local machine. Of course I don't have "mksysb" so I had to substitute things I've got, but your logic with respect to "failed_when:" and "hostvars[host]['mksys']['msg']" appears to be okay.

The only thing that stuck out to me is that you have some leading tabs in your original post's yaml. Perhaps that's an artifact of posting, email, etc., but do check for tabs and retest if you find any. Either way let us know.

Kenady Inampudi

unread,
Feb 26, 2023, 9:11:03 PM2/26/23
to Ansible Project
Hi Todd,

my original playbook works just fine, the problem is with the second one where i want the report just for the failed ones, my first play book give me all the output due to special variable {% for host in ansible_play_hosts_all %}

I tried to use if condition to filter the result of the failed ones 

 {% for host in ansible_play_hosts_all %}
 {% if 'Completed Successfully' not in hostvars[host]['mksys']['msg'] %}

 {{ '###' }}{{ host }}{{ '###' }}
 {{ '--------------------------' }}
 {{ hostvars[host]['mksys']['msg'] }}
 {% endif %}
 {% endfor %}

This condition generates nothing., If am running the play on 100 nodes and 5 failed, i want the "hostvars[host]['mksys']['msg']" of the failed ones to a file.

Vladimir Botka

unread,
Feb 26, 2023, 10:42:51 PM2/26/23
to Kenady Inampudi, ansible...@googlegroups.com
On Sun, 26 Feb 2023 18:11:02 -0800 (PST)
Kenady Inampudi <ken...@cis-in.com> wrote:

> {% for host in ansible_play_hosts_all %}
> {% if 'Completed Successfully' not in hostvars[host]['mksys']['msg'] %}
>
> This condition generates nothing.

Your code works as expected. For testing, put the variable into the inventory

shell> cat hosts
all:
hosts:
host1:
mksys:
msg: 'Completed Successfully'
host2:
mksys:
msg: 'Completed Successfully'
host3:
mksys:
msg: 'Completed Not Successfully'

The playbook

shell> cat pb.yml
- hosts: all
tasks:
- debug:
msg: |
{% for host in ansible_play_hosts_all %}
{% if 'Completed Successfully' not in hostvars[host]['mksys']['msg'] %}
{{ host }} msg:{{hostvars[host]['mksys']['msg'] }}
{% endif %}
{% endfor %}
run_once: true

gives (abridged)

msg: |-
host3 msg:Completed Not Successfully


--
Vladimir Botka

Todd Lewis

unread,
Feb 27, 2023, 6:50:47 AM2/27/23
to Ansible Project
I understand the problem. Thanks for restating it concisely.

Your template is creating /home/user1/mksysb_error_report.out, but not on localhost.
It's creating it on each of the target hosts.
It's doing that because of the leading tabs I asked you about earlier. There's one in front of your "run_once: True" and another in front of your "delegate_to: localhost". So even though it may look correctly indented on your screen, your old Ansible sees it as being part of the body of your template, so it's running the copy task on each of the target hosts, creating the file there instead of once locally.

I could only get your originally posted playbook to run on my local "ansible [core 2.14.2]" if I removed the tabs and fixed the indentation. Otherwise it would error out immediately complaining about the tabs.

However, I still have access to an environment with "ansible 2.10.15", and there your code — with Vladimir Botka's clever hack of setting the necessary variables in the inventory — runs just fine, tabs and all, as described above.

Remove the tabs from the three lines that have them and run your test again. Also, look on the target hosts and see if /home/user1/mksysb_error_report.out exists. I expect it will.
Reply all
Reply to author
Forward
0 new messages