Bookmark this page

Guided Exercise: Reusing Content from Ansible Content Collections

  • Write a playbook that uses modules from an Ansible Content Collection.

Outcomes

  • Identify the Ansible Content Collection that provides a specific module.

  • Create a task that specifies a module by its fully qualified collection name.

  • Use the collections keyword in a playbook.

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

This command ensures that a project directory is created with the required files for this exercise.

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

Procedure 2.1. Instructions

  1. Change to the /home/student/manage-reusing/ directory and attempt to run the basic-web.yml playbook by using the ansible-playbook command.

    1. Change to the /home/student/manage-reusing/ directory and list the contents of the directory.

      [student@workstation ~]$ cd ~/manage-reusing/
      [student@workstation manage-reusing]$ ls
      ansible.cfg  basic-web.yml  inventory
    2. Use the ansible-playbook command to attempt to run the basic-web.yml playbook. The attempt fails with an error message.

      [student@workstation manage-reusing]$ ansible-playbook basic-web.yml
      ERROR! couldn't resolve module/action 'firewalld'. This often indicates a misspelling, missing collection, or incorrect module path.
      ...output omitted...
    3. Attempt to find the firewalld module in the default modules installed by the ansible-core package. The command does not return output indicating that the module is not found.

      [student@workstation manage-reusing]$ ansible-doc -l | grep firewalld

      Important

      The ansible-playbook command provided by Red Hat Ansible Automation Platform 2 is based on the Ansible Core 2.13 package. It does not include any Ansible Content Collections other than ansible.builtin, which is why the preceding two substeps fail. Your control node also does not have any additional Ansible Content Collections installed. The firewalld module was moved into a separate collection after Ansible 2.9.

  2. Use automation content navigator with the default ee-supported-rhel8 execution environment to identify the Ansible Content Collection that provides the firewalld module.

    1. Use the podman login command to log in to the classroom private automation hub at hub.lab.example.com. Log in as student with redhat123 as the password. This step simulates a customer logging in to registry.redhat.io and ensures that the specified automation execution environment is pulled down to the local system if it does not already exist.

      [student@workstation manage-reusing]$ podman login hub.lab.example.com
      Username: student
      Password: redhat123
      Login Succeeded!
    2. List the module documentation available within the ee-supported-rhel8 automation execution environment. Notice the colon at then end of the output which means that the output is extensive.

      [student@workstation manage-reusing]$ ansible-navigator doc -l \
      > --eei ee-supported-rhel8 --pp missing -m stdout
      add_host                      Add a host (and alternatively a group) to the ...
      amazon.aws.aws_az_info        Gather information about availability zones in ...
      amazon.aws.aws_caller_info    Get information about the user and account ...
      amazon.aws.aws_s3             manage objects in S3
      amazon.aws.cloudformation     Create or delete an AWS CloudFormation stack
      ...output omitted...
      :
    3. Type /firewalld to search the module documentation for firewalld. The ansible.posix content collection provides the firewalld module.

      add_host                      Add a host (and alternatively a group) to the ...
      amazon.aws.aws_az_info        Gather information about availability zones in ...
      amazon.aws.aws_caller_info    Get information about the user and account ...
      amazon.aws.aws_s3             manage objects in S3
      amazon.aws.cloudformation     Create or delete an AWS CloudFormation stack
      ...output omitted...
      /firewalld

      The fully qualified collection name for the firewalld module is ansible.posix.firewalld.

      ansible.posix.firewalld       Manage arbitrary ports/services with firewalld
      ansible.posix.firewalld_info    Gather information about firewalld
      ...output omitted...

      Press q to exit the ansible-navigator command.

  3. Create a copy of the basic-web.yml playbook to a file named basic-web-fqcn.yml and then update the new file to use fully qualified collection names for each module.

    1. Create a copy of the basic-web.yml playbook named basic-web-fqcn.yml.

      [student@workstation manage-reusing]$ cp basic-web.yml basic-web-fqcn.yml
    2. Update the basic-web-fqcn.yml playbook to use the fully qualified collection names for each module. The ansible.posix collection provides the firewalld module. The ansible.builtin collection provides the other modules. The modified basic-web-fqcn.yml playbook should consist of the following content:

      ---
      - name: Configure a basic web server
        hosts: servere.lab.example.com
        become: true
        tasks:
          - name: Install software
            ansible.builtin.yum:
              name:
                - httpd
                - firewalld
              state: latest
      
          - name: Start and enable services
            ansible.builtin.service:
              name: "{{ item }}"
              state: started
              enabled: true
            loop:
              - httpd
              - firewalld
      
          - name: Open access to http
            ansible.posix.firewalld:
              service: http
              immediate: true
              permanent: true
              state: enabled
      
          - name: Configure simple index.html
            ansible.builtin.copy:
              content: "Hello world from {{ ansible_facts['fqdn'] }}.\n"
              dest: /var/www/html/index.html

      Note

      Although not currently required, Red Hat recommends that you use the fully qualified collection name.

  4. Use the ee-supported-rhel8 automation execution environment to verify that the basic-web-fqcn.yml playbook completes successfully.

    1. Run the basic-web-fqcn.yml playbook.

      [student@workstation manage-reusing]$ ansible-navigator run \
      > basic-web-fqcn.yml --eei ee-supported-rhel8 --pp missing
      
        Play name                     Ok  Changed ... Failed ... Task count  Progress
      0│Configure a basic web server   5        4 ...      0 ...          5  Complete
      
      ^f/PgUp page up     ^b/PgDn page down     ↑↓ scroll    esc back  ... Successful

      When the Progress column shows Successful, press ESC to exit from the ansible-navigator command.

    2. Use the curl command to display web content on servere.lab.example.com.

      [student@workstation manage-reusing]$ curl servere.lab.example.com
      Hello world from servere.lab.example.com.
  5. Create a copy of the basic-web.yml playbook named basic-web-keyword.yml. Modify the basic-web-keyword.yml playbook so that it defines the included collections.

    1. Create a copy of the basic-web.yml playbook.

      [student@workstation manage-reusing]$ cp basic-web.yml basic-web-keyword.yml
    2. Define the ansible.builtin and ansible.posix collections in the basic-web-keyword.yml playbook using the collections keyword. Configure the play to target serverf.lab.example.com instead of servere.lab.example.com. The top of the modified playbook should consist of the following content:

      ---
      - name: Configure a basic web server
        hosts: serverf.lab.example.com
        become: true
        collections:
          - ansible.builtin
          - ansible.posix
        tasks:
      ...output omitted...
  6. Use the ee-supported-rhel8 automation execution environment to verify that the basic-web-keyword.yml playbook completes successfully.

    1. Run the basic-web-keyword.yml playbook.

      [student@workstation manage-reusing]$ ansible-navigator run \
      > basic-web-keyword.yml --eei ee-supported-rhel8 --pp missing
      
        Play name                     Ok  Changed ... Failed ... Task count  Progress
      0│Configure a basic web server   5        4 ...      0 ...          5  Complete
      
      ^f/PgUp page up     ^b/PgDn page down     ↑↓ scroll    esc back  ... Successful

      When the Progress column shows Successful, press ESC to exit from the ansible-navigator command.

    2. Use the curl command to display web content on serverf.lab.example.com.

      [student@workstation manage-reusing]$ curl serverf.lab.example.com
      Hello world from serverf.lab.example.com.

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 manage-reusing

This concludes the section.

Revision: do374-2.2-82dc0d7