Using Ansible to Get Clients to a Specific State

Get Started
45 minutes
  • 9 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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

Additional Resources

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

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 the server.
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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?