2

I am using Ansible 2.1.1.0 intalled by pip on OS X "El capitan". And I have tried with a very simple configuration:

#/etc/ansible/hosts
[webservers]
my.remote.server

Using the variable ansible_user to connect through ssh using the corresponding public key:

#/etc/ansible/group_vars/webservers
---
ansible_user: my_user

All seems to be fine when I try to ping the server ansible webservers -m ping:

my.remote.server | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Nonetheless when I try to run any command on the remote server it gets executed on my local machine, for example running ansible webservers -m shell -a "echo $HOSTNAME" should display my.remote.server, but displays this instead:

my.remote.server | SUCCESS | rc=0 >>
my.local.server

Any idea what could be causing the issue?

Ander
  • 5,093
  • 7
  • 41
  • 70

1 Answers1

6

That's because "echo $HOSTNAME" is being evaluated in your local shell before being passed to the remote server over SSH. Try this:

ansible webservers -a hostname

Or this:

ansible webservers -m shell -a 'echo $HOSTNAME'
mwp
  • 8,217
  • 20
  • 26