18

我正在使用如下主机文件,

[qa-workstations]
10.39.19.190 ansible_user=test ansible_ssh_pass=test

我正在使用以下命令在主机中执行“whoami”命令

root@Svr:~/ansible# ansible all -a "whoami" -i /etc/ansible/host
10.39.19.190 | success | rc=0 >>
root

默认情况下,ansible 尝试使用我登录的用户名,即 root 而不是我在主机文件中指定的测试用户

当我尝试在 ansible cli 命令中传递用户名时它工作正常

root@Svr:~/ansible# ansible all -a "whoami" -i /etc/ansible/host -u test
10.39.19.190 | success | rc=0 >>
test

但是我不可能每次都在 CLI 中传递用户名,因为不同的主机使用不同的用户名。另外我没有为每个主机生成密钥对,因为主机经常变化

使用的版本:

 ansible 1.5.4 
 Ubuntu 14.04 LTS
4

2 回答 2

32

使用最新版本的 Ansible,您可以ansible_user在主机定义中使用该参数。

例如,在主机上mysql-host.mydomain,我需要连接的用户是mysql

[docker-hosts]
mysql-host.mydomain ansible_user=mysql

但由于您使用的是旧版本的 ansible,您可能需要ansible_ssh_user改用

http://docs.ansible.com/ansible/faq.html#how-do-i-handle-different-machines-needing-different-user-accounts-or-ports-to-log-in-with

于 2016-05-26T15:00:58.993 回答
11

在您的 playbook 目录中创建 ansible.cfg 或修改“全局” ansible.cfg

注意:仅处理 1 个配置文件。第一个在下面的列表中获胜。 http://docs.ansible.com/ansible/intro_configuration.html

  • ANSIBLE_CONFIG(环境变量)
  • ansible.cfg(在当前目录中)
  • .ansible.cfg(在主目录中)
  • /etc/ansible/ansible.cfg
[defaults]
remote_user=test
于 2015-12-17T14:02:03.183 回答