How to run a command with root user in Ansible ?

In this post, we will see how to run a command with root user in ansible.

There are two ways to do this.

Lets have a look at both of the ways.

I. By getting root password prompt.
Tip: -b for root and -K will ask sudo pass

$ ansible -b -K -m shell -a "tail /var/log/messages" all

SUDO password:

r005-linux5.NGEL.ngelinux.com | SUCCESS | rc=0 >>

Oct  5 05:15:02 r005-linux5 sshd[19772]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key

 

OR,

 

II. By providing root password with command.

$ ansible -b -m shell -a "tail /var/log/messages" all --extra-vars "ansible_sudo_pass=Google@345”
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments