为每个远程主机指定不同的用户名

时间:2019-10-11 15:14:20

标签: ansible

我正在3个不同的远程主机上进行部署,并且它们都具有不同的用户和密码。我试图将它们添加到我的文件中,以便在执行过程中不提示它们。

我尝试将它们添加到group_vars文件夹中。

 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

这将返回警告,提示有重复的dict键,并且仅使用第一个声明的值。

我看到了在剧本中传递它的解决方案:

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

hosts: host3
user:efgh
tasks:...

我发现此解决方案不适用于我的情况,因为我需要执行的任务对于所有远程主机都是相同的。另外,还有许多任务,将它们复制粘贴多次到一本剧本中会很不方便。它也不允许我添加通行证。

如何为每个远程主机指定用户名和密码?

1 个答案:

答案 0 :(得分:0)

您还可以基于每个主机选择连接类型和用户:

[targets]

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

来自:Working with Inventory

我以前做过类似的事情,但是在其他选择上比较简单:

[all]
vagrant  ansible_host=127.0.0.1