Ansible Commonly Used Modules

45 minutes
  • 7 Learning Objectives

About this Hands-on Lab

Ansible has many built in modules, but some are used a lot more than others. In this lab we’re going to reinforce the usage of some of the more common ones, so that they’re easier for you to remember when the time comes.

*This course is not approved or sponsored by Red Hat.*

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Verify Connectivity in the Environment

On the command line run ansible -m ping all.

Install httpd

Your playbook, webservers.yml should look something like this:

---
# Common Modules Playbook
#

- name: Common Modules Playbook
  become: yes
  hosts: webservers

  tasks:
   - name: Install httpd on webservers
     yum:
      name: httpd
      state: present
Start and Enable the httpd Service

In this task all we have to do is add the following to the webservers.yml playbook:

   - name: Start the httpd service
     service:
      name: httpd
      state: started
      enabled: yes
Create a dba User Account

Your playbook, dbserver.yml, should look similar to this:

---

- name: DB server playbook
  become: yes
  hosts: dbservers

  tasks:
   - name: Add the 'dba' user to the dbservers
     user:
       name: dba
Copy the Required File

You can add to your dbserver.yml playbook something similar to the following:

   - name: Copy required DBA files
     copy:
      src: /root/DBAstuff.txt
      dest: /home/dba/
      owner: dba
      group: dba
      mode: 0600
Create /var/www/html/index.html

The final addition to your webserver.yml playbook should look similar to:

   - name: Create index.html
     file:
      path: /var/www/html/index.html
      state: touch
Clone the Ansible Git Repository into /opt on adminservers

Your admin.yml playbook will look something like this:

---

- name: Use git to clone the Ansible repo
  hosts: admins
  become: yes

  tasks:
   - name: Install git
     yum:
      name: git
      state: present

   - name: Use the git module
     git:
      repo: https://github.com/ansible/ansible.git
      dest: /opt

Additional Resources

Notice: Ansible is installed as the root user, so please work on all tasks after elevating to the root user.

Your Ansible environment is set up so let's take it for a real spin so you can show your co-workers this proof of concept is worth the time.

After you verify connectivity in your environment, write playbooks that do the following - 1 for webservers, 1 for dbservers, and 1 for admin servers. Rather than calling for server names individually, use the group names associated with them in /etc/ansible/hosts.

  1. Install httpd on the webserver group.
  2. Start the httpd service on the webserver group.
  3. Create a dba account on the dbserver group.
  4. Copy /root/DBAstuff.txt to the new user's home directory. Make sure he is the owner and group of the file, set permissions to 0600.
  5. Create index.html in /var/www/html on the webserver.
  6. Use the git module to check out https://github.com/ansible/ansible.git on the Admin server - put it in the /opt directory.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?