• No results found

Demonstration: Discovering Ansible modules

This demonstration will use Ansible ad hoc commands to show some common operations using the yum, service, and uri core modules. The demonstration will be conducted out of the /home/student/imp-moddemo directory on workstation using the supplied

configuration file and inventory. The workstation host will serve as both the control node and the managed host for this demonstration. It is assumed that SSH public key authentication is configured between the control node and the managed host, and that both the managed host and materials.example.com are running Apache HTTP web servers. The control node is already configured to connect to the managed host as the devops user, which has full sudo privileges that does not require password authentication. Privilege escalation has been enabled in the control node's configuration and will be performed using sudo as the root user and without password prompting. The tree package is not installed on workstation at the start of the demonstration.

The yum core module will be used to install the tree package on the managed host. Then the service core module will be used to restart the Apache HTTP web server on workstation.

Finally, the uri core module will be used to see what web pages on materials.example.com are visible to workstation.

1. Change directory to the working directory for the demonstration.

[student@workstation ~]$ cd /home/student/imp-moddemo

2. Determine the status of the tree package and the availability of the package for installation on workstation.

Demonstration: Discovering Ansible modules

[student@workstation imp-moddemo]$ yum list installed tree Loaded plugins: langpacks, search-disabled-repos

Error: No matching Packages to list

3. Install the tree package by executing an ad hoc command which uses the yum module. Be sure to activate privilege escalation because the command has to be executed with root privileges.

[student@workstation imp-moddemo]$ ansible localhost -m yum -a "name=tree state=present"

localhost | SUCCESS => {

"changed": true,

"msg": "",

"rc": 0,

...output omitted...

4. Reverify the status of the tree package on workstation.

[student@workstation imp-moddemo]$ yum list installed tree Loaded plugins: langpacks, search-disabled-repos

Installed Packages

tree.x86_64 1.6.0-10.el7 rhel_dvd

The package is now installed.

5. Determine the status of the httpd service on workstation.

[student@workstation imp-moddemo]$ systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset:

disabled)

Drop-In: /etc/systemd/system/httpd.service.d └─training.conf

Active: active (running) since Mon 2016-04-18 14:23:47 EDT; 38min ago Docs: man:httpd(8)

man:apachectl(8)

Process: 328 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS) Process: 12363 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)

Main PID: 333 (httpd)

Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"

CGroup: /system.slice/httpd.service ├─333 /usr/sbin/httpd -DFOREGROUND

Apr 18 14:23:47 workstation.lab.example.com systemd[1]: Starting The Apache HTTP Server...

Apr 18 14:23:47 workstation.lab.example.com systemd[1]: Started The Apache HTTP Server.

6. Restart the httpd service by executing an ad hoc command which uses the service module. Be sure to activate privilege escalation because the command has to be executed with root privileges.

[student@workstation imp-moddemo]$ ansible localhost -m service -a "name=httpd state=restarted"

localhost | SUCCESS => {

"changed": true,

"name": "httpd",

"state": "started"

}

7. Reverify the status of the httpd service on workstation.

[student@workstation imp-moddemo]$ systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset:

disabled)

Drop-In: /etc/systemd/system/httpd.service.d └─training.conf

Active: active (running) since Mon 2016-04-18 15:03:43 EDT; 3s ago Docs: man:httpd(8)

man:apachectl(8)

Process: 1559 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS) Process: 12363 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)

Main PID: 1590 (httpd)

Status: "Processing requests..."

CGroup: /system.slice/httpd.service ├─1590 /usr/sbin/httpd -DFOREGROUND

Apr 18 15:03:43 workstation.lab.example.com systemd[1]: Starting The Apache HTTP Server...

Apr 18 15:03:43 workstation.lab.example.com systemd[1]: Started The Apache HTTP Server.

The system messages indicate that the service was successfully restarted.

8. Make an HTTP request to the http://materials.example.com URL from workstation and evaluate the HTTP status return code.

[student@workstation imp-moddemo]$ ansible localhost -m uri -a "url=http://

materials.example.com/"

localhost | SUCCESS => {

"changed": false,

"content_length": "2457",

"content_location": "http://materials.example.com/",

"content_type": "text/html;charset=ISO-8859-1",

"date": "Mon, 18 Apr 2016 19:10:28 GMT",

"redirected": false,

"server": "Apache/2.4.6 (Red Hat Enterprise Linux)",

"status": 200 }

Demonstration: Discovering Ansible modules

The module executed successfully and reports that an HTTP status return code of 200 was received.

9. Make an HTTP request to the http://materials.example.com/nothing URL from workstation and evaluate the HTTP status return code.

[student@workstation imp-moddemo]$ ansible localhost -m uri -a "url=http://

materials.example.com/nothing"

localhost | FAILED! => {

"changed": false,

"content": "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>

\n<title>404 Not Found<title>\n<head><body>\n<h1>Not Found<h1>\n<p>The requested URL /nothing was not found on this server.<p>\n<body><html>\n",

"content_length": "205",

"content_type": "text/html; charset=iso-8859-1",

"date": "Mon, 18 Apr 2016 19:14:08 GMT",

"failed": true,

"msg": "Status code was not [200]",

"redirected": false,

"server": "Apache/2.4.6 (Red Hat Enterprise Linux)",

"status": 404 }

The module execution reports a failure and shows that an HTTP status return code of 404 was received.

References

ansible-doc(1) man page

About modules — Ansible Documentation http://docs.ansible.com/ansible/modules.html

Ansible modules - these modules ship with Ansible — Ansible Documentation https://github.com/ansible/ansible-modules-core