Command within an Ansible variable for Playbook

66 views
Skip to first unread message

Dominic S S

unread,
Oct 12, 2015, 12:50:48 PM10/12/15
to Ansible Project
Hi,

I am pretty new to ansible,  i  am trying to get ansible to send me an email from my defined hosts  (as in the inventory file

To make the playbook work i have had to hardcode the FROM field within the email module of ansible.

I have hardcoded the playbook file  such that the FROM field is the control server .

This works.. however the FROM  field gives the wrong impression that the email is being sent from the control server , ie ro...@server1.myserver1 but in other words its coming from each individual server .ie ro...@server2.myserver....... etc

I have 6 servers under ansible control for my lab purposes ie server 1  to server6



---
- hosts: random-servers
  tasks:
    - mail: >
        to="myemail...@gmail.com"
        from="ro...@server1.mylabserver.com"
        body="Just testing ansible "
        subject="Welcome Sir This is just a test  "
        attach="/etc/hosts"



What i want to achieve is to set the  enveriablevariable code such that it refelects the right server from which the email is being sent from

I tried $HOSTNAME  i tried  `hostname`

But its simply a case of not knowing how the set the environment variables right or using the wrong syntax

eseesntially the string  from="ro...@server1.mylabserver.com"  need  to be dynamically changed via environment settings but i am not sure how
I have tried various syntax combinations and tried to set vars.. but it wouldnt work any help would be appreciated.

I tried from="[root@$HOSTNAME]"   and "[root@`hostname`]"    and all sorts of other syntax  combinations but no joy.

I tried to use the vars:  and environment options . which i am not familiar with .but it always failed each time.

Maybe there might be an easy answer from you ansible gurus.I love ansible..ive picked up so much in a very short time


 
i am running ansible 1.9.3 on Centos 7 

Help please any help would be greatly appreciated

Regards
D

Brian Coca

unread,
Oct 23, 2015, 8:02:12 PM10/23/15
to Ansible Project
if the hostname envvar is set before you run ansible you can use:

from=root@{{lookup('env', 'HOSTNAME')}}

another option is to just execute `hostname`

from=root@{{lookup('pipe', 'hostname')}}

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages