Bookmark this page

Guided Exercise: Managing Ansible Configuration Files

In this exercise, you edit Ansible configuration files to customize your Ansible environment.

Outcomes

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

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

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

Procedure 2.2. Instructions

  1. Change into the /home/student/playbook-manage directory.

    [student@workstation ~]$ cd ~/playbook-manage
    [student@workstation playbook-manage]$
  2. Configure automation content navigator.

    1. Create the /home/student/playbook-manage/ansible-navigator.yml file. Configure automation content navigator to use the execution environment image utility.lab.example.com/ee-supported-rhel8:latest and to only pull the image if it is missing. Also configure automation content navigator to disable playbook artifacts. The file should consist of the following content:

      ---
      ansible-navigator:
        execution-environment:
          image: utility.lab.example.com/ee-supported-rhel8:latest
          pull:
            policy: missing
        playbook-artifact:
          enable: false
    2. Run the ansible-navigator images command to list the available execution environment images.

      [student@workstation playbook-manage]$ ansible-navigator images
      ----------------------------------------------------------------------------------
      Execution environment image and pull policy overview
      ----------------------------------------------------------------------------------
      Execution environment image name:     utility.lab.example.com/ee-supported-rhel8:latest
      Execution environment image tag:      latest
      Execution environment pull arguments: None
      Execution environment pull policy:    missing
      Execution environment pull needed:    True
      ----------------------------------------------------------------------------------
      Updating the execution environment
      ...output omitted...
      Running the command: podman pull utility.lab.example.com/ee-supported-rhel8:latest
      Trying to pull utility.lab.example.com/ee-supported-rhel8:latest...
      ...output omitted...
    3. After automation content navigator pulls the execution environment image you should see it in the list:

        Image                 Tag     Execution environment    Created      Size
      0│ee-supported-rhel8    latest  True                     3 weeks ago  1.34 GB
      
      
      ^b/PgUp page up ^f/PgDn page down ↑↓ scroll esc back [0-9] goto :help help

      Press Esc to exit the image list.

  3. In your /home/student/playbook-manage directory, start editing a new file named ansible.cfg.

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

    [defaults]
    inventory = ./inventory

    Save your work and exit the text editor.

  4. In the /home/student/playbook-manage directory, start editing the new static inventory file, inventory.

    The static inventory should contain four host groups:

    • [myself] should contain the workstation host.

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

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

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

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

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

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

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

      [web:children]
      intranetweb
      internetweb
    5. The final inventory file should consist of the following content:

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

      Save your work and exit the text editor.

  5. Use the ansible-navigator command to run the provided playbooks and test the configuration of your inventory file's host groups.

    The ansible-navigator run command runs an Ansible Playbook, formatted as a YAML file, that contains automation instructions to be run on managed hosts. The following ansible-navigator commands use the configuration files that you edited in preceding steps.

    Each of the following playbooks runs the ansible.builtin.ping module on a host or group of hosts to determine if they are ready to be used as managed hosts by Ansible. These tests also validate whether your inventory file is correct.

    1. Run the /home/student/playbook-manage/ping-myself.yml playbook to verify that the workstation machine is in the myself inventory group.

      [student@workstation playbook-manage]$ ansible-navigator run \
      > -m stdout ping-myself.yml
      
      PLAY [Validate inventory hosts] **********************************************
      
      TASK [Ping workstation] ******************************************************
      ok: [workstation]
      
      PLAY RECAP *******************************************************************
      workstation                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    2. Run the /home/student/playbook-manage/ping-intranetweb.yml playbook to verify that the servera.lab.example.com machine is in the intranetweb inventory group.

      [student@workstation playbook-manage]$ ansible-navigator run \
      > -m stdout ping-intranetweb.yml
      
      PLAY [Validate inventory hosts] **********************************************
      
      TASK [Ping intranetweb] ******************************************************
      ok: [servera.lab.example.com]
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    3. Run the /home/student/playbook-manage/ping-internetweb.yml playbook to verify that the serverb.lab.example.com machine is in the internetweb inventory group.

      [student@workstation playbook-manage]$ ansible-navigator run \
      > -m stdout ping-internetweb.yml
      
      PLAY [Validate inventory hosts] **********************************************
      
      TASK [Ping internetweb] ******************************************************
      ok: [serverb.lab.example.com]
      
      PLAY RECAP *******************************************************************
      serverb.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    4. Run the /home/student/playbook-manage/ping-web.yml playbook to verify that the servera.lab.example.com and serverb.lab.example.com machines are in the web inventory group.

      [student@workstation playbook-manage]$ ansible-navigator run \
      > -m stdout ping-web.yml
      
      PLAY [Validate inventory hosts] **********************************************
      
      TASK [Ping web] **************************************************************
      ok: [servera.lab.example.com]
      ok: [serverb.lab.example.com]
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
      serverb.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    5. Run the /home/student/playbook-manage/ping-all.yml playbook to verify that the workstation, servera.lab.example.com, and serverb.lab.example.com machines are all in the inventory file.

      [student@workstation playbook-manage]$ ansible-navigator run \
      > -m stdout ping-all.yml
      
      PLAY [Validate inventory hosts] **********************************************
      
      TASK [Ping all] **************************************************************
      ok: [serverb.lab.example.com]
      ok: [servera.lab.example.com]
      ok: [workstation]
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
      serverb.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
      workstation                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  6. Open the /home/student/playbook-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/playbook-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. The complete ansible.cfg file should consist of the following content:

      [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.

  7. Use the ansible-navigator command to run the /home/student/playbook-manage/ping-intranetweb.yml playbook again to verify that you are now prompted for the sudo password.

    When prompted for the sudo password, enter student.

    [student@workstation playbook-manage]$ ansible-navigator run \
    > -m stdout ping-intranetweb.yml
    BECOME password: student
    
    PLAY [Validate inventory hosts] **********************************************
    
    TASK [Ping intranetweb] ******************************************************
    ok: [servera.lab.example.com]
    
    PLAY RECAP *******************************************************************
    servera.lab.example.com    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

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

This concludes the section.

Revision: rh294-9.0-c95c7de