0

so i have this playbook, which checking, if there are installed selected packages. nothing special there. Then i would like to send an output to my email, if everything went right etc.

---
 - name: Install basic apps
   hosts: some hosts from inventory
   become: yes
   tasks:
     - name: load global variables
       include_vars:
               file: /etc/ansible/vars/global.yml
               name: global

     - name: install MC
       apt:
               name: mc
               state: present
       register: mc

     - debug:
             var: mc

     - name: install HTOP
       apt:
               name: htop
               state: present
       register: htop

     - name: install TMUX
       apt:
               name: tmux
               state: present
       register: tmux

     - name: install VIM
       apt:
               name: vim
               state: present
       register: vim


     - name: send email
       mail:
             host: "{{ global.mail_server }}"
             port: "{{ global.mail_port }}"
             subject: basic apps instalation was run by {{ ansible_user_id }}
             subtype: html
             # body: targeted server:  {{ ansible_hostname }} 
# stav MC - {{ mc.failed }} {{ mc.changed }}
# stav HTOP - {{ htop }}
# stav TMUX - {{ tmux }}
# stav VIM - {{ vim }} body: "{{ lookup('template', '/etc/ansible/templates/mail.j2') }}" from: adress to: adress run_once: true

simple Jinja template:

{% for host in play_hosts %}
{{ host }}: 
Midnight Commander
- is there anything wrong? {{ mc.failed }}
- any changes made? {{ mc.changed}}
Htop
- is there anything wrong? {{ htop.failed }}
- any changes made? {{ mc.changed}}

{% endfor %}

this is an output in my email:

host1: 
Midnight Commander
- is there anything wrong? False
- any changes made? True
Htop
- is there anything wrong? False
- any changes made? True
________________________________________
host2: 
Midnight Commander
- is there anything wrong? False
- any changes made? True
Htop
- is there anything wrong? False
- any changes made? True

but changes were made only on host1. If i make change at host2, there will be False everywhere. That means, variables registred from apt module are filled only from first host. What am i doing wrong here?

0

I would be more sure about this, if I knew how exactly you are sending the emails. But I am assuming that you are doing it from host1.

The problem might be that you are not accessing the hostvars correctly in your email template.

This

{{ mc.failed }}

is effectively a shortcut for

{{ hostvars[inventory_hostname].mc.failed }}

It depends on which host you are running it from.

Try this in the template (using your host variable from above):

{{ hostvars[host].mc.failed }}

The docs on this topic can be found here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.