how to specify user name in host file of ansible
Asked Answered
P

2

27

I am using the host file as below,

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

I am using below command to execute "whoami" command in host

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

ansible by default trying to use user name in which I have logged in, i.e root instead of test user which I have specified in host file

It works fine when I try to pass the username in ansible cli command

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

But its not possible for me to pass username every time in CLI as different host uses different username. Also I don't have a key pair generated for each host, because host machine keeps changing often

version used:

 ansible 1.5.4 
 Ubuntu 14.04 LTS
Perpetuity answered 17/12, 2015 at 12:15 Comment(0)
R
16

Create an ansible.cfg in your playbook directory or modify the "global" ansible.cfg

Note: Only 1 configuration file is processed. First one wins in the below list. https://docs.ansible.com/ansible/latest/reference_appendices/config.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
Rabb answered 17/12, 2015 at 14:2 Comment(3)
This ansible.cfg is good to know, but the issue here is I need to provide different user name/password for different hostPerpetuity
Try inventory variable ansible_ssh_userRabb
Dead link to docs...Factorize
M
43

With recent versions of Ansible, you can use the ansible_user parameter in the host definition.

For example, on the host mysql-host.mydomain the user I need to connect with is mysql :

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

But as you are using an older version of ansible, you might need to use ansible_ssh_user instead

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

Molehill answered 26/5, 2016 at 15:0 Comment(0)
R
16

Create an ansible.cfg in your playbook directory or modify the "global" ansible.cfg

Note: Only 1 configuration file is processed. First one wins in the below list. https://docs.ansible.com/ansible/latest/reference_appendices/config.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
Rabb answered 17/12, 2015 at 14:2 Comment(3)
This ansible.cfg is good to know, but the issue here is I need to provide different user name/password for different hostPerpetuity
Try inventory variable ansible_ssh_userRabb
Dead link to docs...Factorize

© 2022 - 2024 — McMap. All rights reserved.