Skip to main content

Using Ansible to Get Clients to a Specific State

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:45:00

Difficulty

Intermediate

Often you'll find yourself wanting to get a group of servers set up from scratch rather than running individual configuration commands. Often it can seem like a daunting task to write a single playbook to handle a chain of tasks, switching host groups, calling multiple modules, and more.This lab will give you the opportunity to practice writing a playbook that will run all of that at once.

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 to Get Clients to a Specific State

Introduction

We often find ourselves wanting to get a group of servers set up from scratch in one fell swoop, rather than running individual configuration commands.

This lab will give some practice in writing a playbook that will get things up and running all at once.

The Scenario

Management has just told us they bought 300 servers, and we've been tasked with writing a playbook to get them ready to go as quickly as possible. They've already had the OS installed and configured to work with our Ansible environment.

We need to write a single playbook that will run and make sure that all the servers involved are correctly configured and in the desired state when it's finished running. We also need to ensure that all commands are idempotent.

  1. Install the linuxacademy-backup-software package throughout our environment. Due to the high potential for this repository server to be unavailable, ensure a failure doesn't stop the playbook from running.
  2. Install httpd on the webserver group.
  3. Start and enable the httpd service on the webserver group.
  4. Create a dba account on the dbserver group.
  5. Copy /root/DBAstuff.txt to the new user's home directory. Make sure that user (and the group with the same name) owns the file, and set permissions to 0600.
  6. Create index.html in /var/www/html on the web server.
  7. Ensure index.html on the web servers contains the line "Waiting for content."
  8. Install git on the webserver and dbserver group if it is not already.
  9. On each Red Hat server, create a file (/root/addresses) that contains all of the IPv4 addresses for it.

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.

Create the Initial Playbook

We're going to create one Ansible playbook that installs the packages we need on any number of new servers. Start off by creating the initial playbook file (we're using vim here, but use any text editor that's comfortable):

vim state.yml

Install the linuxacademy-backup-software Package throughout Our Environment

This section of our playbook should look like:

---
- name: Install backup software
  hosts: all
  become: yes

tasks:
 - name:  yum command
   yum:
    name: linuxacademy-backup-software
    state: present
   ignore_errors: yes

Install httpd on the webserver Group

The next section of the playbook should look like this:

- name: Install httpd
  hosts: webservers
  become: yes

  tasks:
   - name: httpd install
     yum:
      name: httpd
      state: present

Start and Enable the httpd Service on the webserver Group

Continuing from the last task, the next section should look like this:

   - name: Service management for httpd
     service:
      name: httpd
      state: started
      enabled: yes

Create a dba User Account on the dbserver Group

This portion of our playbook should look like this:

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

  tasks:
   - name: Add user
     user:
      name: dba
      state: present

Copy /root/DBAstuff.txt to the New User's Home Directory

Continuing along from the last task, this portion of our playbook should look like this:

   - name: Copy DB user data
     copy:
      src: /root/DBAstuff.txt
      dest: /home/dba/DBAstuff.txt
      owner: dba
      group: dba
      mode: 0600

Create index.html in /var/www/html on the Web Server

This portion of our playbook should look like this:

- name: Set up index.html on webservers
  hosts: webservers
  become: yes

  tasks:
   - name: Create and populate index.html
     lineinfile:
      path: /var/www/html/index.html
      line: Waiting for content.
      create: yes
      owner: apache
      group: apache
      mode: 0644

Install Git on the webserver and dbserver Groups If It Is Not Already Installed

This next portion of our playbook should look like this:

- name: Enable devs to easily populate content
  hosts: webservers:dbservers
  become: yes

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

Create Red Hat Server-Specific Files

This last portion of our playbook should look like this:

- name: Red Hat specific configuration
  hosts: all
  become: yes

  tasks:
   - name: Populate file with IP addresses
     lineinfile:
      path: /root/addresses
      line: "{{ ansible_facts['all_ipv4_addresses'] }}"
      create: yes
     when: ansible_facts['os_family'] == RedHat

Running the Playbook

To set our playbook in motion, run this:

ansible-playbook state.yml

Conclusion

Wow, that was quite a playbook. But taking the time to write it now saves us time in the long run. The playbook takes all of our servers (it could be ten servers, three hundred, or a thousand -- pick a number!) from blank slates to being in a state that we can use without us having to configure each one manually. That is a big deal. Congratulations!