0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Running Ad-hoc Commands

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, we'll focus on executing ad-hoc commands in Ansible. Ad-hoc commands are a quick and easy way to perform tasks without writing a full playbook. Specifically, we'll learn how to use an ad-hoc command to delete the file we created in our previous lesson. By the end of this lesson, you'll understand how to use Ansible for simple, direct task execution across your managed nodes.

What are Ad-hoc Commands?

Ad-hoc commands in Ansible allow you to execute simple tasks at the command line without needing to write a playbook. They are great for tasks you need to perform quickly, but they are not idempotent like a playbook, so they should be used judiciously.

Using ansible-doc for Module Information

Before we run our ad-hoc command, let's first understand how to use the ansible-doc command to get information about the file module. The file module is versatile and can be used for various file operations, including creating, deleting, or modifying files and directories.

To view the documentation for the file module, run:

ansible-doc file

Pro tip: You can search the file by pressing '/' and then typing in a search query. Pressing 'n' will show the next occurrence of your search pattern.

This command will display detailed information about the file module, including how to specify the path to a file and set its state. Look for the state option and notice how setting it to absent will remove a file.

Deleting the File with an Ad-hoc Command

Now, let's use an ad-hoc command to delete the file /tmp/ansible_created_this_file.txt that we created earlier. The command is as follows:

ansible all -m file -a "path=/tmp/ansible_created_this_file.txt state=absent"

Here's what this command does:

When I execute the command, I see the following output:

Ansible Ad-hoc Command Output
Ansible Ad-hoc Command Output

Here is a breakdown of the output we see:

Verifying File Removal

After executing the ad-hoc command, it's good practice to verify that the command did what we expected and the file has indeed been removed on all hosts.

First, check on the controller node:

ls /tmp

This will list the contents of the /tmp directory on the controller. Ensure that ansible_created_this_file.txt is no longer listed.

Next, verify on the managed nodes. Assuming you have two managed nodes named managed-node-1 and managed-node-2, run:

ssh managed-node-1 -t "ls /tmp"
ssh managed-node-2 -t "ls /tmp"

These commands will SSH into each managed node and list the contents of the /tmp directory, allowing you to confirm that the file has been removed on both nodes.

Verifying the /tmp directory no longer contains the file we attempted to delete
Verifying the /tmp directory no longer contains the file we attempted to delete

As you can see, the files have been deleted all hosts in our Ansible inventory!

Important: Ad-hoc commands are NOT idempotent

Remember, while ad-hoc commands are powerful, they lack the idempotency and organization of playbooks, making them more suited for one-off or immediate tasks.

Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.

Conclusion

Ad-hoc commands in Ansible provide a quick and efficient way to perform tasks without the need for a full playbook. They are particularly useful for straightforward tasks like file manipulation.

Understanding how to use ansible-doc is also an important skill. It allows you to explore the capabilities and options of various Ansible modules directly from the command line, aiding in both ad-hoc commands and playbook development.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments