0

Can anyone help me in inventory variable in Ansible?

I have a server where root login via ssh is not permitted and I used to login on server with a normal user (MYUSER) and then su - to become root. I have ssh key-based authentication with user (MYUSER). Now, I want to execute something like below.

ansible --private-key /PATH/TO/PRIVATE/KEY.KEY -i INVENTORY -m shell -a "whoami" all

But I am getting output as MYUSER.

I want to login on server with my ssh keys in user MYUSER and, then execute some commands using shell module on server via root user :NOTE: I have root password to provide in inventory. Below is the inventory that I created. (^ ^) I know it is wrong.

test-web.example.com ansible_ssh_host=X.X.X.X ansible_ssh_user=MYUSER ansible_become_method=su ansible_become_user=root ansible_become_pass=Xjdt@mdaj12

Execution:

[root@server ansi]# ansible --private-key /root/MASTER/audit/key.key -i test -m shell -a "whoami" all
test-web.example.com | SUCCESS | rc=0 >>
MYUSER 

How can I make this work?

1

Ansible will not become a privileged user until you explicitly ask to.

There is -b switch in ansible CLIs for that. Try:

ansible all -b --private-key /root/MASTER/audit/key.key -i test -m shell -a "whoami"
  • Ok thanks.... I will try... So what I did in my host file.... is it correct or not ? – Suyog Nov 26 '16 at 14:46
1

Like Konstanting said the -b or --become switch is correct.

in playbooks you can also add in every task or globally

become: yes

and optionally additionally if you don't want to become root

become: yes
become_user: user2 

Your hostfile line looks ok

however i guess if you don't need to specify ansible_become_method=su (i think ansible will either use sudo or su by default only if you want it to force using su you need to specify it)

and also not ansible_ssh_host if the inventory name is a valid domain that is bound to the IP: test-web.example.com

also you probably dont need to specify:

ansible_become_user=root

only if you don't want the default become behaviour to be switching to the default root but to another manually created user from yourself.

therefore my lines look a bit shorter, more like this if the hostname is a real domain address!:

www.domain.com ansible_user=nonrootuser ansible_become_pass=ROOTPASSWD

btw: ansible_user is the same as ansible_ssh_user just shorter ;) the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.