How to specify host file via playbook?

Hi Samnang,

You can use ansible-playbook -i hosts abc.yml to specify your inventory file.

you can also have a look at https://github.com/ansible/ansible-examples for some examples.

I actually use a shebang on my playbooks

#!/usr/bin/env ansible-playbook -i ../hosts

All good answers -- also never thought of that, Brian!

FYI -- It's also configurable in /etc/ansible/ansible.cfg or ~/.ansible.cfg

And finally, you can set the ANSIBLE_HOSTS shell variable:

export ANSIBLE_HOSTS=/home/ansible/my-hosts
ansible-playbook site.yml

-Tim

I also use PROJECT_FOLDER/ansible.cfg

[defaults]
hostfile = ./etc/hosts
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks=5
timeout=10
transport=ssh
remote_port=112233
remote_user=xxxxxx

[ssh_connection]
ssh_args=-o PasswordAuthentication=no -o ControlMaster=auto -o
ControlPersist=60s -o ControlPath=/tmp/ansible-ssh-%h-%p-%r
scp_if_ssh=True

That’s awesome. Thanks.

More info: http://www.ansibleworks.com/docs/intro_configuration.html

It appears that look up from playbook’s directory is broken. The command above will
point to hosts file from the current directory and not from playbook’s dir. I filled an issue here:
https://github.com/ansible/ansible/issues/5115

We will look into docs but it works correctly.

– Michael

I prefer if it worked as described in docs - independent of current user position
in directory tree hierarchy. I execute ansible playbooks using relative paths, from
different dirs, and if my current dir will happen to contain ansible.cfg from another
project - this may lead to disaster. For me reading configuration pieces implicitly
from CWD is an anti-pattern.

Don’t do that then :slight_smile:

– Michael

To be honest, the main usercase is to avoid typing -i hosts every time for bootstrap
of config from github checkout. There is only one localhost and if I can just place
ansible.cfg in repository together with playbook - it will solve all my problems. =)

git clone myworkstation /tmp/a
ansible-playbook /tmp/a/playbook.yml