如何在ansible的主机文件中指定用户名

how to specify user name in host file of ansible

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

[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

在您的 playbook 目录中创建一个 ansible.cfg 或修改 "global" ansible.cfg

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

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • .ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg
[defaults]
remote_user=test

对于最新版本的 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