Bookmark this page

Guided Exercise: Managing Ansible Configuration Files

In this exercise, you will customize your Ansible environment by editing an Ansible configuration file.

Outcomes

You should be able to create a configuration file to configure your Ansible environment with persistent custom settings.

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

On workstation, run the lab deploy-manage start command. This script ensures that the managed host, servera, is reachable on the network.

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

Procedure 2.2. Instructions

  1. Create the /home/student/deploy-manage directory, which will contain the files for this exercise. Change to this newly created directory.

    [student@workstation ~]$ mkdir ~/deploy-manage
    [student@workstation ~]$ cd ~/deploy-manage
    [student@workstation deploy-manage]$
  2. In your /home/student/deploy-manage directory, use a text editor to start editing a new file, ansible.cfg.

    Create a [defaults] section in that file. In that section, add a line which uses the inventory directive to specify the ./inventory file as the default inventory.

    [defaults]
    inventory = ./inventory

    Save your work and exit the text editor.

  3. In the /home/student/deploy-manage directory, use a text editor to start editing the new static inventory file, inventory.

    The static inventory should contain four host groups:

    • [myself] should contain the host localhost.

    • [intranetweb] should contain the host servera.lab.example.com.

    • [internetweb] should contain the host serverb.lab.example.com.

    • [web] should contain the intranetweb and internetweb host groups.

    1. In /home/student/deploy-manage/inventory, create the myself host group by adding the following lines:

      [myself]
      localhost
    2. In /home/student/deploy-manage/inventory, create the intranetweb host group by adding the following lines:

      [intranetweb]
      servera.lab.example.com
    3. In /home/student/deploy-manage/inventory, create the internetweb host group by adding the following lines:

      [internetweb]
      serverb.lab.example.com
    4. In /home/student/deploy-manage/inventory, create the web host group by adding the following lines:

      [web:children]
      intranetweb
      internetweb
    5. Confirm that your final inventory file looks like the following:

      [myself]
      localhost
      
      [intranetweb]
      servera.lab.example.com
      
      [internetweb]
      serverb.lab.example.com
      
      [web:children]
      intranetweb
      internetweb

      Save your work and exit the text editor.

  4. Use the ansible command with the --list-hosts option to test the configuration of your inventory file's host groups. This does not actually connect to those hosts.

    [student@workstation deploy-manage]$ ansible myself --list-hosts
      hosts (1):
        localhost
    [student@workstation deploy-manage]$ ansible intranetweb --list-hosts
      hosts (1):
        servera.lab.example.com
    [student@workstation deploy-manage]$ ansible internetweb --list-hosts
      hosts (1):
        serverb.lab.example.com
    [student@workstation deploy-manage]$ ansible web --list-hosts
      hosts (2):
        servera.lab.example.com
        serverb.lab.example.com
    [student@workstation deploy-manage]$ ansible all --list-hosts
      hosts (3):
        localhost
        servera.lab.example.com
        serverb.lab.example.com
  5. Open the /home/student/deploy-manage/ansible.cfg file in a text editor. Add a [privilege_escalation] section to configure Ansible to automatically use the sudo command to switch from student to root when running tasks on the managed hosts. Ansible should also be configured to prompt you for the password that student uses for the sudo command.

    1. Create the [privilege_escalation] section in the /home/student/deploy-manage/ansible.cfg configuration file by adding the following entry:

      [privilege_escalation]
    2. Enable privilege escalation by setting the become directive to true.

      become = true
    3. Set the privilege escalation to use the sudo command by setting the become_method directive to sudo.

      become_method = sudo
    4. Set the privilege escalation user by setting the become_user directive to root.

      become_user = root
    5. Enable prompting for the privilege escalation password by setting the become_ask_pass directive to true.

      become_ask_pass = true
    6. Confirm that the complete ansible.cfg file looks like the following:

      [defaults]
      inventory = ./inventory
      
      [privilege_escalation]
      become = true
      become_method = sudo
      become_user = root
      become_ask_pass = true

      Save your work and exit the text editor.

  6. Run the ansible --list-hosts command again to verify that you are now prompted for the sudo password.

    When prompted for the sudo password, enter student, even though it is not used for this dry run.

    [student@workstation deploy-manage]$ ansible intranetweb --list-hosts
    BECOME password: student
      hosts (1):
        servera.lab.example.com

Finish

On workstation, run the lab deploy-manage finish script to clean up this exercise.

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

This concludes the guided exercise.

Revision: rh294-8.4-9cb53f0