Bookmark this page

Lab: Deploying Ansible

In this review, you will install Ansible on workstation, use it as a control node, and configure it for connections to the managed hosts servera and serverb. Use ad hoc commands to perform actions on managed hosts.

Outcomes

You should be able to:

  • Install Ansible.

  • Use ad hoc commands to perform actions on managed hosts.

Log in to workstation as student using student as the password.

On workstation, run the lab review-deploy start command. This script ensures that the managed hosts, servera and serverb, are reachable on the network. The script creates a lab subdirectory named review-deploy in the student's home directory.

[student@workstation ~]$ lab review-deploy start

Procedure 10.1. Instructions

  1. Install Ansible on workstation so that it can serve the control node.

    [student@workstation ~]$ sudo yum install ansible
    [sudo] password for student:
    Loaded plugins: langpacks, search-disabled-repos
    Resolving Dependencies
    --> Running transaction check
    ...output omitted...
    Is this ok [y/d/N]: y
    ...output omitted...
  2. On the control node, create an inventory file, /home/student/review-deploy/inventory, containing a group called dev. This group should consist of the managed hosts servera.lab.example.com and serverb.lab.example.com.

    1. Change directory into the Ansible project directory, /home/student/review-deploy, created by the setup script.

      [student@workstation ~]$ cd ~/review-deploy
    2. Create the inventory file with the following content.

      [dev]
      servera.lab.example.com
      serverb.lab.example.com
  3. Create the Ansible configuration file in /home/student/review-deploy/ansible.cfg. The configuration file should reference the /home/student/review-deploy/inventory inventory file.

    Add the following entries to configure the inventory file ./inventory as the inventory source. Save the changes and exit the text editor.

    [defaults]
    inventory=./inventory
  4. Execute an ad hoc command using privilege escalation to modify the contents of the /etc/motd file on servera and serverb to contain the string Managed by Ansible\n. Use devops as the remote user.

    [student@workstation review-deploy]$ ansible dev -m copy \
    > -a 'content="Managed by Ansible\n" dest=/etc/motd' -b -u devops
    servera.lab.example.com | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "4458b979ede3c332f8f2128385df4ba305e58c27",
        "dest": "/etc/motd",
        "gid": 0,
        "group": "root",
        "md5sum": "65a4290ee5559756ad04e558b0e0c4e3",
        "mode": "0644",
        "owner": "root",
        "secontext": "unconfined_u:object_r:etc_t:s0",
        "size": 19,
        "src": "/home/devops/.ansible/tmp/...output omitted...",
        "state": "file",
        "uid": 0
    }
    serverb.lab.example.com | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "4458b979ede3c332f8f2128385df4ba305e58c27",
        "dest": "/etc/motd",
        "gid": 0,
        "group": "root",
        "md5sum": "65a4290ee5559756ad04e558b0e0c4e3",
        "mode": "0644",
        "owner": "root",
        "secontext": "system_u:object_r:etc_t:s0",
        "size": 19,
        "src": "/home/devops/.ansible/tmp/...output omitted...",
        "state": "file",
        "uid": 0
    }
  5. Execute an ad hoc command to verify that the contents of the /etc/motd file on servera and serverb are identical.

    [student@workstation review-deploy]$ ansible dev -m command -a "cat /etc/motd"
    servera.lab.example.com | CHANGED | rc=0 >>
    Managed by Ansible
    
    serverb.lab.example.com | CHANGED | rc=0 >>
    Managed by Ansible

Evaluation

On workstation, run the lab review-deploy grade command to confirm success on this exercise. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab review-deploy grade

Finish

On workstation, run the lab review-deploy finish command to clean up this exercise.

[student@workstation ~]$ lab review-deploy finish

This concludes the lab.

Revision: rh294-8.4-9cb53f0