moonmoon
moonmoon

Reputation: 110

Specify different user name for each remote host

I am doing a deployment on 3 different remote hosts and they all have different users and passwords. I am trying to add those to my files so that they are not prompted during execution.

I have tried adding them to the group_vars folder.

 my_group:
 host1:
 ansible_ssh_user: user1
 ansible_ssh_pass: pass1

 host2:
 ansible_ssh_user: user2
 ansible_ssh_pass: pass2

 host3:
 ansible_ssh_user: user3
 ansible_ssh_pass: pass3

That returns a warning that there is a duplicate dict key and it uses only the value for the first declaration.

I saw a solution of passing it in the playbook:

hosts: host1:host2
user: abcd 
tasks: ...

hosts: host3
user:efgh
tasks:...

I find this solution unsuitable in my case as the tasks I need to execute are the same for all remote hosts. Also there are a lot of tasks and it will be inconvenient to copy paste them several times in one playbook. It doesn't let me add a pass as well.

How do I specify a username and password for each remote host?

Upvotes: 1

Views: 1743

Answers (1)

Alister Bulman
Alister Bulman

Reputation: 35149

You can also select the connection type and user on a per host basis:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan

from: Working with Inventory.

I've previously done something similar, but simpler for other options:

[all]
vagrant  ansible_host=127.0.0.1

Upvotes: 3

Related Questions