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.
On Monday, 27 February 2023 at 04:52:57 UTC+5:30 Todd Lewis wrote:

> 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.
>
> On Sunday, February 26, 2023 at 10:32:26 AM UTC-5 Kenady Inampudi wrote:
>
>> 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.
>>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6ce17eb0-cd5d-4672-a0dc-abf970259bc4n%40googlegroups.com.

Reply via email to