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

Nag*_*nan 10 ansible ansible-playbook ansible-inventory

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

[qa-workstations]
10.39.19.190 ansible_user=test ansible_ssh_pass=test
Run Code Online (Sandbox Code Playgroud)

我使用下面的命令在主机中执行"whoami"命令

root@Svr:~/ansible# ansible all -a "whoami" -i /etc/ansible/host
10.39.19.190 | success | rc=0 >>
root
Run Code Online (Sandbox Code Playgroud)

默认情况下,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
Run Code Online (Sandbox Code Playgroud)

但是我不可能每次在CLI中传递用户名,因为不同的主机使用不同的用户名.此外,我没有为每个主机生成密钥对,因为主机经常不断更改

使用版本:

 ansible 1.5.4 
 Ubuntu 14.04 LTS
Run Code Online (Sandbox Code Playgroud)

Joh*_*ide 19

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

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

[docker-hosts]
mysql-host.mydomain ansible_user=mysql
Run Code Online (Sandbox Code Playgroud)

但是当你使用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


chr*_*ism 7

在您的剧本目录中创建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
Run Code Online (Sandbox Code Playgroud)

  • 尝试使用广告资源变量“ ansible_ssh_user” (3认同)