Bookmark this page

Guided Exercise: Managing Software and Subscriptions

In this exercise you will configure a new Yum repository and install packages from it on your managed hosts.

Outcomes

You should be able to:

  • Configure a yum repository using the yum_repository module.

  • Manage RPM GPG keys using the rpm_key module.

  • Obtain information about the installed packages on a host using the package_facts module.

On workstation, run the lab start script to confirm that the environment is ready for the lab to begin. The script creates the working directory, called system-software, and populates it with an Ansible configuration file, a host inventory, and lab files.

[student@workstation ~]$ lab system-software start

Procedure 9.1. Instructions

Your organization requires that all hosts have the example-motd package installed. This package is provided by an internal Yum repository maintained by your organization to host internally developed software packages.

You are tasked with writing a playbook to ensure that the example-motd package is installed on the remote host. The playbook must ensure the configuration of the internal Yum repository.

The repository is located at http://materials.example.com/yum/repository. All RPM packages are signed with an organizational GPG key pair. The GPG public key is available at http://materials.example.com/yum/repository/RPM-GPG-KEY-example.

  1. As the student user on workstation, change to the /home/student/system-software working directory.

    [student@workstation ~]$ cd ~/system-software
    [student@workstation system-software]$
  2. Begin writing the repo_playbook.yml playbook. Define a single play in the playbook that targets all hosts. Add a vars clause that defines a single variable custom_pkg with a value of example-motd. Add the tasks clause to to the playbook.

    The playbook now contains:

    ---
    - name: Repository Configuration
      hosts: all
      vars:
        custom_pkg: example-motd
      tasks:
  3. Add two tasks to the playbook.

    Use the package_facts module in the first task to gather information about installed packages on the remote host. This task populates the ansible_facts.packages fact.

    Use the debug module in the second task to print the installed version of the package referenced by the custom_pkg variable. Only execute this task if the custom package is found in the ansible_facts.packages fact.

    Execute the repo_playbook.yml playbook.

    1. Add the first task to the playbook. Configure the manager keyword of the package_facts module with a value of auto. The first task contains the following:

          - name: Gather Package Facts
            package_facts:
              manager: auto
    2. Add a second task to the playbook that uses the debug module to display the value of the ansible_facts.packages[custom_pkg] variable. Add a when clause to the task to check if the value of the custom_pkg variable is contained in the ansible_facts.packages variable. The second task contains the following:

          - name: Show Package Facts for the custom package
            debug:
              var: ansible_facts.packages[custom_pkg]
            when: custom_pkg in ansible_facts.packages
    3. Execute the playbook:

      [student@workstation system-software]$ ansible-playbook repo_playbook.yml
      
      PLAY [Repository Configuration] **********************************************
      
      TASK [Gathering Facts] *******************************************************
      ok: [servera.lab.example.com]
      
      TASK [Gather Package Facts] **************************************************
      ok: [servera.lab.example.com]
      
      TASK [Show Package Facts for the custom package] *****************************
      skipping: [servera.lab.example.com]
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=2    changed=0    unreachable=0    failed=0
      skipped=1    rescued=0    ignored=0

      The debug task is skipped because the example-motd package is not installed on the remote host.

  4. Add a third task that uses the yum_repository module to ensure the configuration of the internal yum repository on the remote host. Ensure that:

    • The repository's configuration is stored in the file /etc/yum.repos.d/example.repo

    • The repository ID is example-internal

    • The base URL is http://materials.example.com/yum/repository

    • The repository is configured to check RPM GPG signatures

    • The repository description is Example Inc. Internal YUM repo

    The third task contains the following:

        - name: Ensure Example Repo exists
          yum_repository:
            name: example-internal
            description: Example Inc. Internal YUM repo
            file: example
            baseurl: http://materials.example.com/yum/repository/
            gpgcheck: yes
  5. Add a fourth task to the play that uses the rpm_key module to ensure that the repository public key is present on the remote host. The repository public key URL is http://materials.example.com/yum/repository/RPM-GPG-KEY-example.

    The fourth task appears as follows:

        - name: Ensure Repo RPM Key is Installed
          rpm_key:
            key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
            state: present
  6. Add a fifth task to ensure that the package referenced by the custom_pkg variable is installed on the remote host.

    The fifth task appears as follows:

        - name: Install Example motd package
          yum:
            name: "{{ custom_pkg }}"
            state: present
  7. The ansible_facts.packages fact is not updated when a new package is installed on a remote host.

    Copy the second task and add it as the sixth task in the play. Execute the playbook and verify that the ansible_facts.packages fact does not contain information about the example-motd installed on the remote host.

    1. The sixth task contains a copy of the second task:

          - name: Show Package Facts for the custom package
            debug:
              var: ansible_facts.packages[custom_pkg]
            when: custom_pkg in ansible_facts.packages

      The entire playbook now looks as follows:

      ---
      - name: Repository Configuration
        hosts: all
        vars:
          custom_pkg: example-motd
        tasks:
          - name: Gather Package Facts
            package_facts:
              manager: auto
      
          - name: Show Package Facts for the custom package
            debug:
              var: ansible_facts.packages[custom_pkg]
            when: custom_pkg in ansible_facts.packages
      
          - name: Ensure Example Repo exists
            yum_repository:
              name: example-internal
              description: Example Inc. Internal YUM repo
              file: example
              baseurl: http://materials.example.com/yum/repository/
              gpgcheck: yes
      
          - name: Ensure Repo RPM Key is Installed
            rpm_key:
              key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
              state: present
      
          - name: Install Example motd package
            yum:
              name: "{{ custom_pkg }}"
              state: present
      
          - name: Show Package Facts for the custom package
            debug:
              var: ansible_facts.packages[custom_pkg]
            when: custom_pkg in ansible_facts.packages
    2. Execute the playbook.

      [student@workstation system-software]$ ansible-playbook repo_playbook.yml
      PLAY [Repository Configuration] **********************************************
      
      TASK [Gathering Facts] *******************************************************
      ok: [servera.lab.example.com]
      
      TASK [Gather Package Facts] **************************************************
      ok: [servera.lab.example.com]1
      
      TASK [Show Package Facts for the custom package] *****************************
      skipping: [servera.lab.example.com]
      
      TASK [Ensure Example Repo exists] ********************************************
      changed: [servera.lab.example.com]
      
      TASK [Ensure Repo RPM Key is Installed] **************************************
      changed: [servera.lab.example.com]
      
      TASK [Install Example motd package] ******************************************
      changed: [servera.lab.example.com]
      
      TASK [Show Package Facts for the custom package] *****************************
      skipping: [servera.lab.example.com]2
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=5    changed=3    unreachable=0    failed=0
      skipped=2    rescued=0    ignored=0

      1

      The Gather Package Facts task determines the data contained in the ansible_facts.packages fact.

      2

      The task is skipped because the example-motd package is installed after the Gather Package Facts task.

  8. Insert a task immediately after the Install Example motd package task using the package_facts module to update the package facts. Set the module's manager keyword with a value of auto.

    The complete playbook is shown below:

    ---
    - name: Repository Configuration
      hosts: all
      vars:
        custom_pkg: example-motd
      tasks:
        - name: Gather Package Facts
          package_facts:
            manager: auto
    
        - name: Show Package Facts for the custom package
          debug:
            var: ansible_facts.packages[custom_pkg]
          when: custom_pkg in ansible_facts.packages
    
        - name: Ensure Example Repo exists
          yum_repository:
            name: example-internal
            description: Example Inc. Internal YUM repo
            file: example
            baseurl: http://materials.example.com/yum/repository/
            gpgcheck: yes
    
        - name: Ensure Repo RPM Key is Installed
          rpm_key:
            key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
            state: present
    
        - name: Install Example motd package
          yum:
            name: "{{ custom_pkg }}"
            state: present
    
        - name: Gather Package Facts
          package_facts:
            manager: auto
    
        - name: Show Package Facts for the custom package
          debug:
            var: ansible_facts.packages[custom_pkg]
          when: custom_pkg in ansible_facts.packages
  9. Use an Ansible ad hoc command to remove the example-motd package installed during the previous execution of the playbook. Execute the playbook with the inserted package_facts task and use the output to verify that the installation of the example-motd package.

    1. To remove the example-motd package from all hosts, use the ansible all command with the -m yum and -a 'name=example-motd state=absent' options.

      [student@workstation system-software]$ ansible all -m yum \
      > -a 'name=example-motd state=absent'
      servera.lab.example.com | CHANGED => {
      ...output omitted...
          "changed": true,
          "msg": "",
          "rc": 0,
          "results": [
              "Removed: example-motd-1.0-1.el7.x86_64"
          ]
      ...output omitted...
    2. Execute the playbook.

      [student@workstation system-software]$ ansible-playbook repo_playbook.yml
      
      PLAY [Repository Configuration] **********************************************
      
      TASK [Gathering Facts] *******************************************************
      ok: [servera.lab.example.com]
      
      TASK [Gather Package Facts] **************************************************
      ok: [servera.lab.example.com]
      
      TASK [Show Package Facts for the custom package] *****************************
      skipping: [servera.lab.example.com]1
      
      ...output omitted...
      
      TASK [Install Example motd package] ******************************************
      changed: [servera.lab.example.com]2
      
      TASK [Gather Package Facts] **************************************************
      ok: [servera.lab.example.com]3
      
      TASK [Show Package Facts for example-motd] ***********************************
      ok: [servera.lab.example.com] => {
          "ansible_facts.packages[custom_pkg]": [4
              {
                  "arch": "x86_64",
                  "epoch": null,
                  "name": "example-motd",
                  "release": "1.el7",
                  "source": "rpm",
                  "version": "1.0"
              }
          ]
      }
      
      PLAY RECAP *******************************************************************
      servera.lab.example.com    : ok=7    changed=1    unreachable=0    failed=0
      skipped=1    rescued=0    ignored=0

      1

      No package fact exists for the example-motd package because the package is not installed on the remote host.

      2

      The example-motd package is installed as a result of this task, as indicated by the changed status.

      3

      This task updates the package facts with information about the example-motd package.

      4

      The example-motd package fact exists and indicates only one example-motd package is installed. The installed package is at version 1.0.

Finish

On workstation, run the lab system-software finish script to clean up the resources created in this exercise.

[student@workstation ~]$ lab system-software finish

This concludes the guided exercise.

Revision: rh294-8.4-9cb53f0