Lab 10 – Troubleshooting Ansible

POD parameters : OpenShift Group user0

pod0-master.origin.com

pod0-node1.origin.com

pod0-spare.origin.com

User master node1 node2
user0
pod0-master.origin.com
Name  : pod1-master
eth0            : 10.1.64.110
Netmask  : 255.255.0.0
Gateway  : 172.16.0.1
pod0-node1.origin.com
Name  : pod1-master
eth0            : 10.1.64.110
Netmask  : 255.255.0.0
Gateway  : 172.16.0.1
pod1-node2.origin.com
Name  : pod1-node2
eth0            : 172.16.120.31
Netmask  : 255.255.0.0
Gateway  : 172.16.0.1

Introduction

In this lab, you will learn about troubleshooting playbooks and managed hosts.

1. Troubleshooting Playbooks

In this lab, a playbook has errors that need to be corrected. The project structure from previous units is going to be reused. The playbook for this exercise is the samba.yml playbook that configures a Samba service on pod0-node1.origin.com.

1.1 Login as “root” user on pod0-master.origin.com node:

ssh root@pod0-master.origin.com

1.2 Create and change to the~/test-troubleshooting/ directory.

mkdir ~/test-troubleshooting && cd ~/test-troubleshooting

1.3 Create a file named ansible.cfg in the current directory as follows, configuring the log_path parameter for Ansible to start logging to the ~/test-troubleshooting/ansible.log file.

cat > ansible.cfg <<EOF
[defaults]
log_path = ~/test-troubleshooting/ansible.log
EOF

1.4 Below is the samba.yml file, which contains error:

cat > samba.yml <<EOF
---
- name: "Install and Configure Samba"
  hosts: "pod0-master.origin.com"
  tasks:
    - name: "Install packages
      yum:
        name: "{{ item }}"
        state: latest
      with_items:
        - samba
        - samba-client
        - samba-common
        - firewalld

    - name: "Add service to firewall"
      firewalld:
        service: samba
        state: enabled
        immediate: true
        permanent: true

    - name: "Enable and start service"
      service: 
        name: "{{ item }}"
        state: started
        enabled: true
      with_items:
        - smb
        - nmb
        - firewalld
EOF

1.5 Run the playbook. This playbook sets up a Samba server if everything is correct. The run will fail due to missing double quotes on the name dictionary definition. The error message is very indicative of the issue with the run. Notice the dictionary name is assigned a value that contains a colon and does not have a closed quote.

ansible-playbook samba.yml

Sample Output:

ERROR! Syntax Error while loading YAML. 
  did not find expected key

The error appears to have been in '/root/troubleshooting/samba.yml': line 7, column 16, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      yum:
        name: "{{ item }}"
               ^ 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 }}"

1.6 Check that the error has been properly logged to the ~/test-troubleshooting/ansible.log file.

tail -25 ansible.log

Sample Output:

2018-07-13 01:35:14,386 p=14657 u=root |  ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in '/root/troubleshooting/samba.yml': line 7, column 16, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      yum:
        name: "{{ item }}"
               ^ 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 }}"

1.7 Edit the playbook and correct the error, and add quotes to the entire value being assigned to name. The corrected edition of samba.yml should contain the following content:

cat > samba.yml <<EOF
---
- name: "Install and Configure Samba"
  hosts: "pod0-master.origin.com"
  tasks:
    - name: "Install packages"
      yum:
        name: "{{ item }}"
        state: latest
      with_items:
        - samba
        - samba-client
        - samba-common
        - firewalld

    - name: "Add service to firewall"
      firewalld:
        service: samba
        state: enabled
        immediate: true
        permanent: true

    - name: "Enable and start service"
      service: 
        name: "{{ item }}"
        state: started
        enabled: true
      with_items:
        - smb
        - nmb
        - firewalld
EOF

1.8 Run the playbook using the ––syntax-check option. It should not show any additional syntax errors.

ansible-playbook samba.yml --syntax-check

Output:

playbook: samba.yml

1.9 Run the playbook. Notice that, it run successfully

ansible-playbook samba.yml

Sample Output:

PLAY [Install and Configure Samba] *********************************************

TASK [Gathering Facts] *********************************************************
ok: pod0-master.origin.com

TASK [Install packages] ********************************************************
ok: pod0-master.origin.com => (item=[u'samba', u'samba-client', u'samba-common', u'firewalld'])

TASK [Add service to firewall] *************************************************
ok: [pod0-master.origin.com]

TASK [Enable and start service] ************************************************
ok: [pod0-master.origin.com] => (item=smb)
ok: [pod0-master.origin.com] => (item=nmb)
ok: [pod0-master.origin.com] => (item=firewalld)

PLAY RECAP *********************************************************************
pod0-master.origin.com      : ok=4    changed=0    unreachable=0    failed=0 

1.10 Rerun the playbook with -vvvv to get more information about the run. An error is issued because the pod0-master.origin.com managed host is not reachable.

ansible-playbook -vvvv samba.yml

1.11 Run the playbook again. Execute the playbook using the –step option. It should run without errors.

ansible-playbook samba.yml --step
Note: Press “y” and “enter” to all performing task.

Sample Output:

PLAY [Install and Configure Samba] **************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [pod0-master.origin.com]
Perform task: TASK: Install packages (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Install packages (N)o/(y)es/(c)ontinue: *************************************************************************************************

TASK [Install packages] *************************************************************************************************************************************
ok: [pod0-master.origin.com] => (item=[u'samba', u'samba-client', u'samba-common', u'firewalld'])
Perform task: TASK: Add service to firewall (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Add service to firewall (N)o/(y)es/(c)ontinue: ******************************************************************************************

TASK [Add service to firewall] ******************************************************************************************************************************
ok: [pod0-master.origin.com]
Perform task: TASK: Enable and start service (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Enable and start service (N)o/(y)es/(c)ontinue: *****************************************************************************************

TASK [Enable and start service] *****************************************************************************************************************************
ok: [pod0-master.origin.com] => (item=smb)
ok: [pod0-master.origin.com] => (item=nmb)
ok: [pod0-master.origin.com] => (item=firewalld)

PLAY RECAP **************************************************************************************************************************************************
pod0-master.origin.com     : ok=4    changed=0    unreachable=0    failed=0