2

I created a file,

./group_vars/all

which has a simple line

---
ansible_user: centos

My inventory filemy /etc/ansible/inventory has

[centos]
10.1.38.15

However, when I use ansible -m ping I see that it's trying to connect with my regular user.

~/cp/ansible$ ansible -vvv centos -m ping
.... stuff
    "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
    "unreachable": true

You can see it's trying to connect with ecarroll@ and not centos@. Does Ansible -m work with ./group_vars/ if I'm using a global inventory?

0

1 Answer 1

2

As per the documentation (my emphasis):

You can also add group_vars/ and host_vars/ directories to your playbook directory. The ansible-playbook command looks for these directories in the current working directory by default. Other Ansible commands (for example, ansible, ansible-console, etc.) will only look for group_vars/ and host_vars/ in the inventory directory. If you want other commands to load group and host variables from a playbook directory, you must provide the --playbook-dir option on the command line. If you load inventory files from both the playbook directory and the inventory directory, variables in the playbook directory will override variables set in the inventory directory.

So you'll have to provide those vars to the command itself, you can do that with

ansible -m setup 10.1.38.15 -e ansible_user=centos

or,

ansible -u centos -m setup 10.1.38.15
9
  • can you confirm your group_vars/all is not in etc? ie, are you running this from an arbitrary directory? Commented Jan 2, 2020 at 20:05
  • Yes, it is from the current working directory from where I execute the ansible command. This was just intended as a POC.
    – anon
    Commented Jan 2, 2020 at 20:06
  • is your hosts file also local? Commented Jan 2, 2020 at 20:18
  • Yes. The hosts file is also local, as specified with -i hosts it also resides in the current working directory.
    – anon
    Commented Jan 2, 2020 at 20:20
  • 2
    Just did (no need to delete the file though as Ansible defaults there without the -i switch), and that indeed does not work. You must place the group_vars file in the inventory directory.
    – anon
    Commented Jan 2, 2020 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .