The right way to Configure Ansible Managed Nodes and Run ad-hoc Instructions

Within the earlier two articles of this Ansible Sequence, we’ve defined Core Elements of Ansible and Setting Up Ansible Management Node. On this half 3, we are going to reveal how one can configure Ansible managed nodes to run ad-hoc instructions on distant hosts.

Setup Passwordless SSH Authentication to Ansible Managed Nodes

As a recap on our final subject, managing distant hosts with Ansible requires establishing of Passwordless SSH authentication between the Ansible management node and the managed hosts. This entails the technology of a key pair (Public and Non-public SSH key pair) on the Ansible Management node and copying the Public key to the entire distant hosts. This will likely be a vital step going ahead and can make your work a lot simpler.

Configure Privilege Escalation on Managed Nodes

When logged in as an everyday consumer, it’s possible you’ll be required to carry out sure duties on managed nodes that require elevated privileges or root privileges. These duties embody package deal administration, including new customers & teams, and modifying system configurations to say simply however a couple of. To realize this, you’ll want to invoke sure directives within the playbook to run the duties as a privileged consumer on the distant hosts.

change into

Ansible permits you to ‘change into’ one other consumer on the managed node totally different from the one at the moment logged in. The change into:sure directive elevates your privileges and permits you to carry out duties that require root privileges comparable to putting in and updating packages and rebooting the system.

Contemplate a playbook httpd.yml that installs and begins Apache webserver as proven:

– identify: set up and begin Apache webserver
hosts: webservers

– identify: set up httpd
yum: identify=httpd state=newest
change into: sure
– identify: test httpd standing
service: identify=httpd state=began

The change into: sure the directive permits you to execute instructions as a root consumer on the distant host.


One other directive that you should utilize to change into one other consumer is the become_user. This lets you swap to a sudo consumer on the distant host upon logging in and never the consumer you log in as.

For instance, to run a command as tecmint consumer on the distant use the directive as proven.

– identify: Run a command because the apache consumer
command: somecommand
change into: sure
become_user: tecmint


This directive will override the default methodology set in ansible.cfg file which is normally set to sudo.


These are used at play or process degree, for example when you’ll want to swap to a consumer when the shell is ready to nologin.

For instance,

– identify: Run a command as no person
command: somecommand
change into: true
become_method: su
become_user: no person
become_flags: ‘-s /bin/sh’

Command-line Choices in Privilege Escalation

Let’s check out a few of the command-line choices that you should utilize to raise your privileges when working instructions:

  • –ask-become-pass, -Okay – This prompts you for the password of the sudo consumer on the distant system that you’re attempting to attach.

$ ansible-playbook myplaybook.yml –ask-become-pass

Ansible Become Pass

Ansible Grow to be Cross

  • –become, -b – This lets you run the duty as a root consumer with out prompting for a password.

$ ansible-playbook myplaybook.yml –become

  • –become-user=BECOME_USER – It permits you to run duties as one other consumer.

$ ansible-playbook myplaybook.yml –become-user=tecmint

Ansible Become User

Ansible Grow to be Person

Validate a Working Configuration utilizing Advert-Hoc Ansible Instructions

Typically, it’s possible you’ll need to carry out fast and easy duties on distant hosts or servers in Ansible with out essentially having to create a playbook. In that case, you’d require to run an ad-hoc command.

What’s an Adhoc Command?

An ansible ad-hoc command is a one-line command that helps you execute easy duties in a easy but environment friendly method with out the necessity of making playbooks. Such duties embody copying information between hosts, rebooting servers, including & eradicating customers and putting in a single package deal.

On this tutorial, we discover varied functions of Ansible Advert-Hoc instructions. We’re going to use the stock file under for an illustration.



Fundamental Utilization of Adhoc Instructions

Essentially the most fundamental utilization of Ansible-Adhoc instructions is pinging a bunch or a gaggle of hosts.

# ansible -m ping all

Within the above command, the -m parameter is the module choice. Ping is the adhoc command and the second parameter all represents all hosts within the stock file. The output of the command is proven under:

Ansible Ping All Hosts

Ansible Ping All Hosts

To ping, a selected group of hosts, exchange ‘all’ parameter with the group identify. Within the instance under, we’re testing connectivity with hosts underneath the webservers group.

# ansible -m ping webservers

Ansible Ping Group of Hosts

Ansible Ping Group of Hosts

Moreover, you should utilize the -a attribute to specify common Linux instructions in double citation marks. For instance, to test system uptime of distant methods, run:

# ansible -a “uptime” all

Ansible Check Uptime of Remote Host

Ansible Examine Uptime of Distant Host

To test disk utilization of distant hosts run.

# ansible -a “df -Th” all

Ansible Check Disk Usage of Remote Hosts

Ansible Examine Disk Utilization of Distant Hosts

There are tons of upon tons of of modules that you should utilize with Adhoc command. To view all the listing of modules with their descriptions, run the command under.

# ansible-doc -l

To view detailed details about a selected module, run the command.

# ansible-doc module_name

For instance, to seek for extra particulars concerning the yum module run:

# ansible-doc yum

Ansible Check Yum Module

Ansible Examine Yum Module

Managing Packages / Companies with Ansible

Ansible adhoc instructions can be utilized for the set up and elimination of packages utilizing yum and apt package deal managers.

To put in Apache net server on the CentOS 7 host underneath webservers group within the stock file run the command:

# ansible webservers -m yum -a “identify=httpd state=current”

Ansible Install Apache on Remote Hosts

Ansible Set up Apache on Distant Hosts

To confirm the set up of the Apache net server, log in to the distant shopper and run.

# rpm -qa | grep httpd

Confirm Apache Installation

Affirm Apache Set up

To uninstall Apache, easy change the state from current to absent.

# ansible webservers -m yum -a “identify=httpd state=absent”

Ansible Remove Apache

Ansible Take away Apache

Once more, to verify the elimination of httpd run.

# rpm -qa | grep httpd

Confirm Removal of Apache

Affirm Removing of Apache

As noticed, Apache net server packages have been purged.

Creating Customers and Teams Utilizing Ansible

When creating customers, the ‘consumer‘ module is useful. To create a brand new consumer james with password redhat on the shopper system database_server, problem the command.

# ansible database_server -m consumer -a “identify=james password=redhat”

Ansible Create User on Remote Hosts

Ansible Create Person on Distant Hosts

To substantiate the creation of the brand new consumer, run the command:

# ansible database_servers -a “id james”

Ansible Confirm User Creation

Ansible Affirm Person Creation

To take away the consumer, run the command:

# ansible database_servers -m consumer -a “identify=james state=absent”

Ansible Remove User

Ansible Take away Person

Privilege Escalation

In case you are working Ansible as an everyday consumer, Ansible offers privilege escalation in distant hosts utilizing the –become choice to amass root privileges and -k to immediate for the password.

For instance, to run the Ansible adhoc command ‘netstat -pnltu‘ with the privileged choice –-become and choice -Okay to immediate for the basis consumer’s password to run the command.

$ ansible webservers -m shell -a ‘netstat -pnltu’ –become -Okay

Ansible Privilege Escalation

Ansible Privilege Escalation

To change into one other consumer aside from root, use the –become-user attribute.

For instance to run ‘df -Th‘ as tecmint consumer on the distant hosts and immediate for the password run:

$ ansible all -m shell -a ‘df -Th’ –become-user tecmint -Okay

Ansible Become Another User

Ansible Grow to be One other Person

Gathering Info about Host Programs

Info check with detailed details about a system. This consists of details about the IP deal with, system structure, reminiscence, and CPU to say a couple of.

To retrieve details about distant hosts, run the command:

$ ansible all -m setup

Ansible Gather System Facts

Ansible Collect System Info

File Switch / Copy Recordsdata

Ansible makes use of the module copy to securely copy information from the Ansible management to a number of distant hosts.

Beneath is an instance of a duplicate operation:

# ansible webservers -m copy -a “src=/var/log/safe dest=/tmp/”

Ansible Copy Files to Remote Host

Ansible Copy Recordsdata to Distant Host

The command copies the /var/log/safe file within the Ansible Management node to distant hosts within the webservers group within the /tmp vacation spot.

You should utilize the file module to alter permissions and file possession.

# ansible webservers -m file -a “dest=/tmp/safe mode=600″

Ansible Change File Permissions

Ansible Change File Permissions

Moreover, you’ll be able to append the proprietor and group arguments as proven:

# ansible webservers -m file -a “dest=/tmp/safe mode=600 proprietor=tecmint group=tecmint”

Ansible Append User and Group Attributes

Ansible Append Person and Group Attributes

You can too create directories, in an analogous method to mkdir -p as proven.

$ ansible webservers -m file -a “dest=/path/to/listing mode=755 proprietor=tecmint group=tecmint state=listing”

For instance,

$ ansible webservers -m file -a “dest=/dwelling/tecmint/information mode=755 proprietor=tecmint group=tecmint state=listing”

Ansible Create a Directory

Ansible Create a Listing


On this article, we make clear how one can configure managed nodes to run Ansible ad-hoc instructions to handle distant hosts. We do hope you discovered it helpful. Give it a shot and tell us the way it went.


Germany Devoted Server

Leave a Reply