Bookmark this page

Guided Exercise: Delegating Tasks and Facts

  • Run a playbook that includes a task that is delegated to another host.

Outcomes

  • Delegate a task to run on another host.

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

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

Procedure 8.1. Instructions

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

    1. From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.

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

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

      [student@workstation update-delegation]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  2. Review the contents of the inventory.yml inventory file. Verify that the web_servers host group contains servers A to F.

    web_servers:
      hosts:
        server[a:f].lab.example.com:
  3. Add a task to the query_times.yml playbook that perform the following actions:

    • Query the time on each server in the web_servers host group.

    • Store the results in the /tmp/times.txt file on the workstation machine.

    1. Add a task that adds two lines of text for each server to the /tmp/times.txt file on the workstation machine. The first line in that file contains the name of a web server. The second line contains the time on the managed host. For both lines, the task uses the facts gathered from the web_servers host group. If the file does not exist, it is created.

      Ensure that each line of text appends to the end of the /tmp/times.txt file on the workstation machine by delegating this task for each managed host to workstation.

      The task should consist of the following content:

          - name: Save server times to workstation
            ansible.builtin.lineinfile:
              path: /tmp/times.txt
              state: present
              mode: '0644'
              create: true
              insertafter: EOF
              line: |
                {{ ansible_facts['fqdn'] }}
                {{ '%c' | strftime(ansible_facts['date_time']['epoch']) }}
            delegate_to: workstation

      Note

      You can use the strftime filter to display time in a particular format by passing a format to the filter. Consult the date(1) man page for available formats.

  4. Run the playbook by using the ansible-navigator run command to test the delegation of tasks:

    [student@workstation update-delegation]$ ansible-navigator run \
    > -m stdout query_times.yml
    
    PLAY [Query server times and store them locally] **************************...
    
    TASK [Gathering Facts] ****************************************************...
    ok: [servera.lab.example.com]
    ...output omitted...
    ok: [serverf.lab.example.com]
    
    TASK [Save server times to workstation] ***********************************...
    changed: [servera.lab.example.com -> workstation]
    ...output omitted...
    changed: [serverf.lab.example.com -> workstation]
    
    PLAY RECAP ****************************************************************...
    servera.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
    serverb.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
    serverc.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
    serverd.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
    servere.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
    serverf.lab.example.com   : ok=2    changed=1    unreachable=0    failed=0 ...
  5. Verify that the playbook has gathered the times correctly and that Ansible stored the information in the /tmp/times.txt file on the workstation machine.

    [student@workstation update-delegation]$ cat /tmp/times.txt
    serverb.lab.example.com
    Thu Dec  1 14:49:24 2022
    
    servera.lab.example.com
    Thu Dec  1 14:49:24 2022
    
    serverd.lab.example.com
    Thu Dec  1 14:49:24 2022
    
    serverc.lab.example.com
    Thu Dec  1 14:49:24 2022
    
    servere.lab.example.com
    Thu Dec  1 14:49:24 2022
    
    serverf.lab.example.com
    Thu Dec  1 14:49:25 2022

    The order of hosts in your file might differ. There might be multiple entries for each host if you run the playbook multiple times.

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-delegation

This concludes the section.

Revision: do374-2.2-82dc0d7