Bookmark this page

Guided Exercise: Configuring Parallelism

  • Explore the effects of different serial and forks directives on how Ansible processes a play.

Outcomes

  • Tune parallel and serial executions of a playbook across multiple managed hosts.

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

This command ensures that the environment is ready for this exercise.

[student@workstation ~]$ lab start update-parallelism

Procedure 8.2. Instructions

  1. Clone the https://git.lab.example.com/student/update-parallelism.git Git repository to the /home/student/git-repos directory and then create a branch for this exercise.

    1. Create the /home/student/git-repos directory if it does not exist, and then change to this directory.

      [student@workstation ~]$ mkdir -p ~/git-repos
      [student@workstation ~]$ cd ~/git-repos
    2. Clone the repository https://git.lab.example.com/student/update-parallelism.git, and then change into the cloned repository:

      [student@workstation git-repos]$ git clone \
      > https://git.lab.example.com/student/update-parallelism.git
      Cloning into 'update-parallelism'...
      ...output omitted...
      [student@workstation git-repos]$ cd update-parallelism
    3. Create the exercise branch.

      [student@workstation update-parallelism]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  2. Examine the contents of the project directory to become familiar with the project files.

    1. Examine the contents of the ansible.cfg file. Note that the inventory file is set to inventory, the forks parameter is set to 4, and the ansible.posix.timer and ansible.posix.profile_tasks callback plug-ins are enabled.

      [defaults]
      inventory=inventory
      remote_user=devops
      forks=4
      callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks
    2. Examine the contents of the inventory file. Note that it contains a host group, webservers, that contains four hosts.

      [webservers]
      servera.lab.example.com
      serverb.lab.example.com
      serverc.lab.example.com
      serverd.lab.example.com
    3. Examine the contents of the update_httpd.yml playbook. This playbook runs against the webservers host group, ensuring that the latest httpd package is installed and that the httpd service is enabled and started.

      ---
      - name: Update the web server
        hosts: webservers
        become: true
        gather_facts: false
      
        tasks:
          - name: Latest httpd package installed
            ansible.builtin.yum:
              name: httpd
              state: latest
            notify:
              - Restart httpd
      
        handlers:
          - name: Restart httpd
            ansible.builtin.service:
              name: httpd
              enabled: true
              state: restarted
    4. Examine the contents of the remove_httpd.yml playbook. This playbook runs against the webservers host group, ensuring that the httpd service is disabled and stopped and that the httpd package is not installed.

      ---
      - name: Remove the web server
        hosts: webservers
        become: true
        gather_facts: false
      
        tasks:
          - name: Query service facts
            ansible.builtin.service_facts:
      
          - name: Disable httpd service
            ansible.builtin.service:
              name: httpd
              enabled: false
              state: stopped
            when: ansible_facts['services']['httpd.service'] is defined
      
          - name: Remove httpd package
            ansible.builtin.yum:
              name: httpd
              state: absent
              autoremove: true
  3. Use the ansible-navigator run command to run the update_httpd.yml playbook.

    1. Watch the playbook as it runs and note how Ansible performs each task on all four hosts at the same time. Also, note the output from the callback plug-ins to determine how long it takes the ansible-navigator command to run the playbook.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout update_httpd.yml
      
      PLAY [Update the web server] ***********************************************
      
      ...output omitted...
      
      PLAY RECAP *****************************************************************
      servera.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverb.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverc.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverd.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      Playbook run took 0 days, 0 hours, 0 minutes, 12 seconds
      Monday 05 December 2022  20:21:04 +0000 (0:00:01.638)       0:00:12.687 ****
      ============================================================================
      Latest httpd package installed -------------------------------------- 11.02s
      Restart httpd -------------------------------------------------------- 1.64s
    2. Run the remove_httpd.yml playbook to stop and disable the httpd service and remove the httpd package.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout remove_httpd.yml
      ...output omitted...
  4. Change the value of the forks parameter to 1 in the ansible.cfg file, and then time the execution of the update_httpd.yml playbook again.

    1. Change the value of the forks parameter to 1 in the ansible.cfg file.

      [defaults]
      inventory=inventory
      remote_user=devops
      forks=1
      callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks
    2. Run the update_httpd.yml playbook again. Watch the playbook as it runs. This time Ansible performs each task on one host at a time. Notice how decreasing the number of forks causes the playbook execution to take longer.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout update_httpd.yml
      
      PLAY [Update the web server] ***********************************************
      
      ...output omitted...
      
      PLAY RECAP *****************************************************************
      servera.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverb.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverc.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      serverd.lab.example.com    : ok=2    changed=2    unreachable=0    failed=0  ...
      Playbook run took 0 days, 0 hours, 0 minutes, 23 seconds
      Monday 05 December 2022  20:28:31 +0000 (0:00:05.212)       0:00:23.075 ****
      ============================================================================
      Latest httpd package installed -------------------------------------- 17.84s
      Restart httpd -------------------------------------------------------- 5.21s
    3. Run the remove_httpd.yml playbook to stop and disable the httpd service and remove the httpd package.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout remove_httpd.yml
      ...output omitted...
  5. Add the serial parameter to the play in the update_httpd.yml playbooks so that the play only runs on two hosts simultaneously. The beginning of the playbook should consist of the following content:

    ---
    - name: Update the web server
      hosts: webservers
      become: true
      gather_facts: false
      serial: 2
  6. Set the value of the forks parameter to 2 in the ansible.cfg file. The forks parameter must be at least equal to the value for of the serial parameter; otherwise, it restricts execution speed.

    [defaults]
    inventory=inventory
    remote_user=devops
    forks=2
    callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks
  7. Run the update_httpd.yml playbook again.

    1. Run the update_httpd.yml playbook again, and watch as it runs. Note that Ansible runs through the entire play on two hosts before rerunning the play on the two remaining hosts.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout update_httpd.yml
      
      PLAY [Update the web server] ***********************************************
      
      TASK [Latest httpd package installed] **************************************
      Monday 05 December 2022  20:32:49 +0000 (0:00:00.024)       0:00:00.024 ****
      changed: [serverb.lab.example.com]
      changed: [servera.lab.example.com]
      
      ...output omitted...
      
      PLAY [Update the web server] ***********************************************
      
      TASK [Latest httpd package installed] **************************************
      Monday 05 December 2022  20:32:56 +0000 (0:00:01.633)       0:00:06.508 ****
      changed: [serverd.lab.example.com]
      changed: [serverc.lab.example.com]
      
      ...output omitted...
    2. Run the remove_httpd.yml playbook to stop and disable the httpd service and remove the httpd package.

      [student@workstation update-parallelism]$ ansible-navigator run \
      > -m stdout remove_httpd.yml
      ...output omitted...
  8. Set the value of the forks parameter back to 4 in the ansible.cfg file.

    [defaults]
    inventory=inventory
    remote_user=devops
    forks=4
    callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks
  9. Set the serial parameter in the update_httpd.yml playbook to 3. The beginning of the playbook should consist of the following content:

    ---
    - name: Update the web server
      hosts: webservers
      become: true
      gather_facts: false
      serial: 3
  10. Run the update_httpd.yml playbook again, and watch as it runs. Note that Ansible runs through the entire play on three hosts, and then runs the play on the one remaining host.

    [student@workstation update-parallelism]$ ansible-navigator run \
    > -m stdout update_httpd.yml
    
    PLAY [Update the web server] ***********************************************
    
    TASK [Latest httpd package installed] **************************************
    Monday 05 December 2022  20:36:44 +0000 (0:00:00.026)       0:00:00.026 ****
    changed: [serverb.lab.example.com]
    changed: [servera.lab.example.com]
    changed: [serverc.lab.example.com]
    
    ...output omitted...
    
    PLAY [Update the web server] ***********************************************
    
    TASK [Latest httpd package installed] **************************************
    Monday 05 December 2022  20:36:51 +0000 (0:00:01.666)       0:00:06.753 ****
    changed: [serverd.lab.example.com]
    
    ...output omitted...

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 update-parallelism

This concludes the section.

Revision: do374-2.2-82dc0d7