admirabilis
admirabilis

Reputation: 2430

Ansible uses wrong host settings

Considering the following hosts file:

[initial]
74.125.224.72 ansible_user=root

[default]
74.125.224.72 ansible_user=deploy ansible_port=2222

I have one playbook called initial.yml with hosts: initial and another called site.yml with hosts: default.

The problem is that if I run ansible-playbook initial.yml, it fails while trying to login as user deploy on port 2222. Shouldn't it just login as user root on port 22?

The contents of initial.yml (commenting out the roles makes no difference either):

---
- hosts: initial
  roles:
    - { role: common, tags: common }
    - { role: login, tags: login }

The contents of ansible.cfg:

[defaults]
inventory = ./hosts.ini
roles_path = ./.tmp
retry_files_enabled = False

Environment:

Files under /etc/ansible/ or /etc/ssh/ have not been modified.

EDIT: I have been able to workaround this issue by using a different hosts file for initial.yml, and specifying it with ansible-playbook --inventory-file=initial.ini.

Upvotes: 4

Views: 1944

Answers (2)

freginold
freginold

Reputation: 3956

It seems that with Ansible, defining the same host twice in the same file causes the second definition to override the first. From the Ansible docs:

Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, the last one loaded wins. If you define a variable twice in a play’s vars: section, the 2nd one wins.

You'll have to work around that somehow, by using multiple host files, specifying the host from the command line, or using your workaround that you mentioned. Another option that may work is to change hash_behavior:

the previous describes the default config hash_behavior=replace, switch to ‘merge’ to only partially overwrite.

Upvotes: 5

Atul Agrawal
Atul Agrawal

Reputation: 1520

Please try this:

ansible-playbook initial.yml -i <your host file> -l initial 

Here -i denotes your inventory file and -l for limiting playbook to a group.

Upvotes: 0

Related Questions