Bookmark this page

Guided Exercise: Troubleshooting Playbooks

In this exercise, you troubleshoot a playbook that has been given to you that does not work properly.

Outcomes

  • You should be able to troubleshoot and resolve issues in playbooks.

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 troubleshoot-playbook

Procedure 8.1. Instructions

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

    [student@workstation ~]$ cd ~/troubleshoot-playbook/
    [student@workstation troubleshoot-playbook]$
  2. Create a file named ansible.cfg in the current directory. Configure the log_path parameter to write Ansible logs to the /home/student/troubleshoot-playbook/ansible.log file. Configure the inventory parameter to use the /home/student/troubleshoot-playbook/inventory file deployed by the lab script.

    The completed ansible.cfg file should contain the following:

    [defaults]
    log_path = /home/student/troubleshoot-playbook/ansible.log
    inventory = /home/student/troubleshoot-playbook/inventory
  3. Run the samba.yml playbook. It fails with an error.

    This playbook would set up a Samba server if everything were correct. However, the run fails because the random_var variable definition is not in double quotation marks. (If a colon is part of a value, the value must be protected by single or double quotation marks.) Read the error message to see how ansible-navigator run reports the problem. Notice that the variable random_var is assigned a value that contains a colon and is not protected by double quotation marks.

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml
    ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
    JSON: Expecting value: line 1 column 1 (char 0)
    
    Syntax Error while loading YAML.
      mapping values are not allowed in this context
    
    The error appears to be in '/home/student/troubleshoot-playbook/samba.yml': line 8, column 30, but may be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
        install_state: installed
        random_var: This is colon: test
                                 ^ here
  4. Confirm that ansible-navigator has logged the error to the /home/student/troubleshoot-playbook/ansible.log file.

    [student@workstation troubleshoot-playbook]$ tail ansible.log
    
    The error appears to be in '/home/student/troubleshoot-playbook/samba.yml': line 8, column 30, but may be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
        install_state: installed
        random_var: This is colon: test
                                 ^ here
  5. Edit the samba.yml playbook and correct the error by adding double quotation marks to the entire value being assigned to random_var. The corrected version of the playbook contains the following content:

    ...output omitted...
      vars:
        install_state: installed
        random_var: "This is colon: test"
    ...output omitted...
  6. Verify the syntax of the playbook using the --syntax-check option. The ansible-navigator command issues another error because there is too much white space indenting the last task, deliver samba config.

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml --syntax-check
    ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
    JSON: Expecting value: line 1 column 1 (char 0)
    
    Syntax Error while loading YAML.
      did not find expected '-' indicator
    
    The error appears to be in '/home/student/troubleshoot-playbook/samba.yml': line 38, column 6, but may
    be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
    
         - name: Deliver samba config
         ^ here
    Please review the log for errors.
  7. Edit the playbook and remove the extra space for all lines in that task. The corrected playbook should appear as follows:

    ...output omitted...
        - name: Configure firewall for samba
          ansible.posix.firewalld:
            state: enabled
            permanent: true
            immediate: true
            service: samba
    
        - name: Deliver samba config
          ansible.builtin.template:
            src: samba.j2
            dest: /etc/samba/smb.conf
            owner: root
            group: root
            mode: 0644
  8. Run the playbook using the --syntax-check option. The ansible-navigator command issues an error because the install_state variable is being used as a parameter in the install samba task and is not quoted. (If a Jinja2 expression is at the start of a value, the value must be protected by double quotation marks.)

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml --syntax-check
    ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
    JSON: Expecting value: line 1 column 1 (char 0)
    
    Syntax Error while loading YAML.
      found unacceptable key (unhashable type: 'AnsibleMapping')
    
    The error appears to be in '/home/student/troubleshoot-playbook/samba.yml': line 14, column 17, but may be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
    
            name: samba
            state: {{ install_state }}
                    ^ here
    We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:
    
        with_items:
          - {{ foo }}
    
    Should be written as:
    
        with_items:
          - "{{ foo }}"
    Please review the log for errors.
  9. Edit the playbook and correct the install samba task. The reference to the install_state variable should be in double quotation marks. The resulting file should consist of the following content:

    ...output omitted...
      tasks:
        - name: Install samba
          ansible.builtin.dnf:
            name: samba
            state: "{{ install_state }}"
    ...output omitted...
  10. Run the playbook using the --syntax-check option. It should not show any additional syntax errors.

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml --syntax-check
    playbook: /home/student/troubleshoot-playbook/samba.yml
  11. Run the playbook again. The debug install_state variable task returns the message The state for the samba service is installed. This task makes use of the ansible.builtin.debug module, and displays the value of the install_state variable. An error is also shown in the deliver samba config task, because no samba.j2 file is available in the /home/student/troubleshoot-playbook/ directory.

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml
    
    PLAY [Install a samba server] **************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [servera.lab.example.com]
    
    TASK [Install samba] ***********************************************************
    changed: [servera.lab.example.com]
    
    TASK [Install firewalld] *******************************************************
    ok: [servera.lab.example.com]
    
    TASK [Debug install_state variable] ********************************************
    ok: [servera.lab.example.com] => {
        "msg": "The state for the samba service is installed"
    }
    
    TASK [Start firewalld] *********************************************************
    ok: [servera.lab.example.com]
    
    TASK [Configure firewall for samba] ********************************************
    changed: [servera.lab.example.com]
    
    TASK [Deliver samba config] ****************************************************
    An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
    fatal: [servera.lab.example.com]: FAILED! => {"changed": false, "msg": "Could not find or access 'samba.j2'\nSearched in:\n\t/home/student/troubleshoot-playbook/templates/samba.j2\n\t/home/student/troubleshoot-playbook/samba.j2\n\t/home/student/troubleshoot-playbook/templates/samba.j2\n\t/home/student/troubleshoot-playbook/samba.j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
    
    PLAY RECAP *********************************************************************
    servera.lab.example.com    : ok=6    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
    Please review the log for errors.
  12. Edit the playbook and correct the src parameter in the deliver samba config task to be samba.conf.j2. The finished file should consist of the following content:

    ...output omitted...
        - name: Deliver samba config
          ansible.builtin.template:
            src: samba.conf.j2
            dest: /etc/samba/smb.conf
            owner: root
    ...output omitted...
  13. Run the playbook again and all tasks should succeed.

    [student@workstation troubleshoot-playbook]$ ansible-navigator run \
    > -m stdout samba.yml
    
    PLAY [Install a samba server] **************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [servera.lab.example.com]
    
    TASK [Install samba] ***********************************************************
    ok: [servera.lab.example.com]
    
    TASK [Install firewalld] *******************************************************
    ok: [servera.lab.example.com]
    
    TASK [Debug install_state variable] ********************************************
    ok: [servera.lab.example.com] => {
        "msg": "The state for the samba service is installed"
    }
    
    TASK [Start firewalld] *********************************************************
    ok: [servera.lab.example.com]
    
    TASK [Configure firewall for samba] ********************************************
    ok: [servera.lab.example.com]
    
    TASK [Deliver samba config] ****************************************************
    changed: [servera.lab.example.com]
    
    TASK [Start samba] *************************************************************
    changed: [servera.lab.example.com]
    
    PLAY RECAP *********************************************************************
    servera.lab.example.com    : ok=8    changed=2    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 troubleshoot-playbook

This concludes the section.

Revision: rh294-9.0-c95c7de