Skip to main content

Using Ansible Modules to Manage Services in Your Environment

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:15:00

Difficulty

Intermediate

Connecting to a single server to restart a service is tedious. Now imagine having to do it on a hundred servers. That kind of task is what tools like Ansible are great at. This lab will help reinforce some of the lesson on 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.

Using Ansible Modules to Manage Services in Your Environment

Introduction

Connecting to a single server to restart a service is tedious. Now imagine having to do it on a hundred servers. That kind of task is what tools like Ansible are great at. This lab will help reinforce some of the lesson on managing services.

The Scenario

There are reports of some servers not starting their required services when they rebooting after getting patches last night. Our task is to ensure all postgresql and httpd services are started and enabled throughout the environment. Web servers do not have PostgreSQL installed, while database and administrative servers do. Web servers only have httpd installed.

In addition, because the databases sometimes take a significant amount of time to start, we've been asked to make sure these restarts are asynchronous.

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.

Start httpd on the Clients in the webserver Group

We need to create services.yml and make a section for starting the httpd service. When we're done, it should look like this:

---
- name: Start httpd
  hosts: webservers
  become: yes

  tasks:
  - service:
     name: httpd
     state: started
     enabled: yes

Start PostgreSQL on Clients in the dbservers and adminservers Groups

While we're in there, let's add a section to start the database service. Because the services need to start asynchronously, we have to use the systemd module:

- name: Start postgresql
  hosts: dbservers:adminservers
  become: yes

  tasks:
  - systemd:
     name: postgresql
     state: started
     no_block: yes

Run the Playbook

The playbook is done, now let's see what happens when we run it. Execute ansible-playbook services.yml. We'll see output showing us that the appropriate services were in fact started on the correct servers.

If we run it again, we'll get all ok: messages, because the playbook is only starting services that aren't currently running.

Conclusion

We did it. Now, managing a fleet of many servers, as far as starting services goes, is a whole lot easier. Congratulations!