Bookmark this page

Guided Exercise: Troubleshooting Ansible Managed Hosts

In this exercise, you will troubleshoot task failures that are occurring on one of your managed hosts when running a playbook.

Outcomes

You should be able to troubleshoot managed hosts.

Log in to workstation as student using student as the password.

On workstation, run the lab troubleshoot-host start script. It ensures that Ansible is installed on workstation. It also downloads the inventory, mailrelay.yml, and postfix-relay-main.conf.j2 files from http://materials.example.com/labs/troubleshoot-host/ to the /home/student/troubleshoot-host/ directory.

[student@workstation ~]$ lab troubleshoot-host start

Procedure 8.2. Instructions

  1. On workstation, change to the /home/student/troubleshoot-host/ directory.

    [student@workstation ~]$ cd ~/troubleshoot-host/
    [student@workstation troubleshoot-host]$
  2. Run the mailrelay.yml playbook using check mode.

    [student@workstation troubleshoot-host]$ ansible-playbook mailrelay.yml --check
    PLAY [create mail relay servers] ***********************************************
    ...output omitted...
    TASK [check main.cf file] ******************************************************
    ok: [servera.lab.example.com]
    
    TASK [verify main.cf file exists] **********************************************
    ok: [servera.lab.example.com]  => {
        "msg": "The main.cf file exists"
    }
    ...output omitted...
    TASK [email notification of always_bcc config] *********************************
    fatal: [servera.lab.example.com]: FAILED! => {"msg": "The conditional check 'bcc_state.stdout != 'always_bcc ='' failed. The error was: error while evaluating conditional (bcc_state.stdout != 'always_bcc ='): 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/home/student/troubleshoot-host/mailrelay.yml': line 42, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: email notification of always_bcc config\n      ^ here\n"}
    ...output omitted...
    PLAY RECAP *********************************************************************
    servera.lab.example.com    : ok=6    changed=3    unreachable=0    failed=1

    The verify main.cf file exists task uses the stat module. It confirmed that main.cf exists on servera.lab.example.com.

    The email notification of always_bcc config task failed. It did not receive output from the check for always_bcc task because the playbook was executed using check mode.

  3. Using an ad hoc command, check the header for the /etc/postfix/main.cf file.

    [student@workstation troubleshoot-host]$ ansible servera.lab.example.com \
    > -u devops -b -a "head /etc/postfix/main.cf"
    servera.lab.example.com | FAILED | rc=1 >>
    head: cannot open '/etc/postfix/main.cf' for reading: No such file or directorynon-zero return code

    The command failed because the playbook was executed using check mode. Postfix is not installed on servera.lab.example.com

  4. Run the playbook again, but without specifying check mode. The error in the email notification of always_bcc config task should disappear.

    [student@workstation troubleshoot-host]$ ansible-playbook mailrelay.yml
    PLAY [create mail relay servers] ***********************************************
    ...output omitted...
    TASK [check for always_bcc] ****************************************************
    changed: [servera.lab.example.com]
    
    TASK [email notification of always_bcc config] *********************************
    skipping: [servera.lab.example.com]
    
    RUNNING HANDLER [restart postfix] **********************************************
    changed: [servera.lab.example.com]
    
    PLAY RECAP *********************************************************************
    servera.lab.example.com    : ok=8    changed=5    unreachable=0    failed=0
    skipped=1    rescued=0    ignored=0
  5. Using an ad hoc command, display the top of the /etc/postfix/main.cf file.

    [student@workstation troubleshoot-host]$ ansible servera.lab.example.com \
    > -u devops -b -a "head /etc/postfix/main.cf"
    servera.lab.example.com | SUCCESS | rc=0 >>
    # Ansible managed
    #
    # Global Postfix configuration file. This file lists only a subset
    # of all parameters. For the syntax, and for a complete parameter
    # list, see the postconf(5) manual page (command: "man 5 postconf").
    #
    # For common configuration examples, see BASIC_CONFIGURATION_README
    # and STANDARD_CONFIGURATION_README. To find these documents, use
    # the command "postconf html_directory readme_directory", or go to
    # http://www.postfix.org/BASIC_CONFIGURATION_README.html etc.

    Now it starts with a line that contains the string, "Ansible managed". This file was updated and is now managed by Ansible.

  6. Edit the mailrelay.yml playbook and add a task to enable the smtp service through the firewall. Add the task as the last task, before the handlers.

    ...output omitted...
        - name: postfix firewalld config
          firewalld:
            state: enabled
            permanent: true
            immediate: true
            service: smtp
    ...output omitted...
  7. Run the playbook. The postfix firewalld config task should have been executed with no errors.

    [student@workstation troubleshoot-host]$ ansible-playbook mailrelay.yml
    PLAY [create mail relay servers] ***********************************************
    ...output omitted...
    TASK [postfix firewalld config] ************************************************
    changed: [servera.lab.example.com]
    
    PLAY RECAP *********************************************************************
    servera.lab.example.com    : ok=8    changed=2    unreachable=0    failed=0
    skipped=1    rescued=0    ignored=0
  8. Using an ad hoc command, check that the smtp service is now configured on the firewall at servera.lab.example.com.

    [student@workstation troubleshoot-host]$ ansible servera.lab.example.com \
    > -u devops -b -a "firewall-cmd --list-services"
    servera.lab.example.com | CHANGED | rc=0 >>
    cockpit dhcpv6-client samba smtp ssh
  9. Use telnet to test if the SMTP service is listening on port TCP/25 on servera.lab.example.com. Disconnect when you are finished.

    [student@workstation troubleshoot-host]$ telnet servera.lab.example.com 25
    Trying 172.25.250.10...
    Connected to servera.lab.example.com.
    Escape character is '^]'.
    220 servera.lab.example.com ESMTP Postfix
    quit
    221 2.0.0 Bye
    Connection closed by foreign host.

Finish

On workstation, run the lab troubleshoot-host finish script to clean up this exercise.

[student@workstation ~]$ lab troubleshoot-host finish

This concludes the guided exercise.

Revision: rh294-8.4-9cb53f0