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

Ansible 为每个远程主机指定不同的用户名,ansible,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: user

我正在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:...
我发现这个解决方案不适合我的情况,因为我需要执行的任务对于所有远程主机都是相同的。还有很多任务,在一个剧本中复制粘贴几次会很不方便。它也不允许我添加通行证


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

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

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
发件人:

我以前也做过类似的事情,但其他选项更简单:

[all]
vagrant  ansible_host=127.0.0.1