Skip to main content

Understand Core Components of Ansible – Modules

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:15:00

Difficulty

Intermediate

Using built-in modules is one of the ways to extract a lot Ansible's power. In this lab, we go through managing file ownership, copying information, installing software, and managing services.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Understand Core Components of Ansible - Modules

Introduction

Using built-in modules is one of the ways to extract a lot Ansible's power. In this lab, we go through managing file ownership, copying information, installing software, and managing services.

The Scenario

We're in the midst of a proof of concept project. Now we've been tasked with making sure that Ansible can create and manipulate files, as well as install software, on a target system. We need to install httpd and create an index.html file. The contents of that file will be server's facts. We also need to make sure all relevant services are started, and that the page is being served out. The index.html file should be owned by the apache user, have apache as the group owner, and have octal permissions of 0600.

Logging In

Use the credentials provided on the hands-on lab page to get into Server1 to begin with. Since we need root privileges, let's just run sudo -i right off and become root.

Collect Facts

If we're going to populate index.html with facts from the server, we need to collect them first:

ansible localhost -m setup --tree /tmp/facts

There will be an authenticity prompt, then the command will run. There should be a file in /tmp/facts now, called 127.0.0.1, that contains the facts.

Install Apache

Now, to serve out the facts we just collected, we've got to install Apache. But rather than using yum to do it, we're going to use Ansible:

ansible localhost -m yum -a "name=httpd state=present" --become

Remember, we need to use the --become flag so that Ansible knows to elevate and get root privileges.

Copy the Server's Facts into index.html

Using Ansible we can just copy the facts file into our new web server directory, and naming it index.html in the process:

ansible localhost -m copy -a "src=/tmp/facts/127.0.0.1 dest=/var/www/html/index.html mode=0600 owner=apache group=apache" --become

Again, we need to use --become because /var/www/html isn't writable unless we're the apache or root user, and we're trying to make the file so that it's owned by someone who isn't ansible.

Now let's check to make sure that we actually have an index.html file sitting in our web server's content directory:

ll /var/www/html/

It should be there.

Make Sure Apache is Running

We've installed Apache, but it probably isn't running by default. Check with this:

systemctl status httpd

We should see some output indicating that it is inactive (dead). Luckily, we can start and manage services with Ansible. Let's use it now to start Apache:

ansible localhost -m service -a "name=httpd state=started enabled=yes" --become

If we run systemctl status httpd again, we can see that it is now running, and should start at boot. We should be all set.

Prove It

Not so fast! We want to be sure that we are in fact serving out our facts. Test it with this curl command:

curl localhost

All of our facts should fly by on the screen.

Conclusion

Just as we set out to do, we managed to use Ansible modules to get facts, install Apache, and serve out a web page containing those facts. Congratulations!