Anda di halaman 1dari 67

1.

Once you have started your Linux Academy Lab Servers and logged in
with the 'user' account and password provided, use the appropriate
command to generate a public and private key pair on 'Server1'. Verify the
key pair was created by changing to the appropriate directory and listing
the files.
[user@tcox4 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
b8:49:0a:1f:71:f9:0c:54:c8:8e:30:9b:93:b3:3c:bf
user@tcox4.mylabserver.com
The key's randomart image is:
+--[ RSA 2048]----+
| ..o. |
| o .o. |
| *.o+ |
| * .o.= |
| ..+. o S |
| +o + o |
| oo o |
| . |
| E. |
+-----------------+
[user@tcox4 ~]$ cd .ssh
[user@tcox4 .ssh]$ ll
total 8
-rw-------. 1 user user 1679 Sep 21 14:23 id_rsa
-rw-r--r--. 1 user user 408 Sep 21 14:23 id_rsa.pub
2. Exchange the public key with 'Server2' and the 'user' account on that
server. Verify that the identity appearing in the appropriate key file matches
the user and system name/IP of the originating 'Server1'.
[user@tcox4 .ssh]$ ssh-copy-id 54.86.179.231
The authenticity of host '54.86.179.231 (54.86.179.231)' can't be
established.
ECDSA key fingerprint is
0d:0c:b1:1d:e1:cf:6d:9f:51:bf:0f:dc:60:82:a1:73.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new
key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if
you are prompted now it is to install the new keys
user@54.86.179.231's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '54.86.179.231'"


and check to make sure that only the key(s) you wanted were added.

[user@tcox4 .ssh]$ ssh 54.86.179.231


Last login: Mon Sep 21 14:22:42 2015 from 216.46.60.98
[user@tcox5 ~]$ cd .ssh
[user@tcox5 .ssh]$ ll
total 4
-rw-------. 1 user user 408 Sep 21 14:24 authorized_keys
[user@tcox5 .ssh]$ cat authorized_keys
ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDBk5qaPck8F+aHk1BZBNmgqW6TSXctE0N+MddR
Ouqm2TvxMeI4s5bfCpWXSBKqhTo/SFqMjM7nHp4F8stG86VEP5UymiWaeVu6rrzxTrT/
9QIlFW+yWv9Mbg/GNN48rqDdZ4931sLsYnKx/dDh1S/iTofQpzgl6LB+bpsgWanaVPIc
KJkh0LP9LZXX0+VpPlMrk9Gpn1F0k1EdO5zPYyEI2MkmhJZCMKhz3dOZCIqMuayt1QKS
SpyDeVDx3dMMuFddXJOyuWEIYtIJC+u7hIIHV6Bsmzl4PI0dHnvmHk+Sn2DBEIn/K+9a
qRQMT9IR2a2TOX1EWBqaNDsxIeLjdhw3 user@tcox4.mylabserver.com

3. Verify that you are able to log into 'Server2' from 'Server1' with the 'user'
account without entering a password. Check the hostname file to be sure
you are logged into 'Server2'.
[user@tcox4 .ssh]$ ssh 54.86.179.231
Last login: Mon Sep 21 14:22:42 2015 from 216.46.60.98
[user@tcox5 .ssh]$ hostname
tcox5.mylabserver.com

4. Repeat Step #1 for 'Server2' (creating and exchanging the 'user' SSH
keys with 'Server1'). Create a file on 'Server1' using any method you
choose. Use 'secure copy' to copy that file to 'Server2'. Confirm the copy
succeeds without password entry.
[user@tcox4 ~]$ echo "test file" > testfile.txt
[user@tcox4 ~]$ scp testfile.txt 54.86.179.231:/home/user
testfile.txt

5. Using 'secure copy', copy the file from Step #4 back to 'Server1' from
'Server2', renaming it to something else during the copy process, verify that
no password was required.
[user@tcox4 ~]$ scp 54.86.179.231:/home/user/testfile.txt
copiedfrom.txt
testfile.txt

100% 10 0.0KB/s 00:00


[user@tcox4 ~]$ ll
total 8
-rw-rw-r--. 1 user user 10 Sep 21 14:31 copiedfrom.txt
drwxr-xr-x. 2 user user 6 Jan 7 2015 Desktop
-rw-rw-r--. 1 user user 0 Sep 21 14:30 testfile.txt

1. Verify that Ansible is running by displaying the version and module path.
Change to the Ansible configuration directory and list the contents.
[root@tcox3 ~]# ansible --version
ansible 1.9.2
configured module search path = None
[root@tcox3 ~]# cd /etc/ansible
[root@tcox3 ansible]# ll
total 20
-rw-r--r--. 1 root root 8629 Jun 25 21:11 ansible.cfg
-rw-r--r--. 1 root root 88 Sep 21 14:43 hosts
drwxr-xr-x. 2 root root 6 Jun 25 21:11 roles

2. Move the original Ansible Hosts file to another file in the same directory
called 'hosts.original'. Create a new empty 'hosts' file in the default
configuration directory location.
[root@tcox3 ansible]# mv hosts hosts.original && touch hosts && ll
total 20
-rw-r--r--. 1 root root 8629 Jun 25 21:11 ansible.cfg
-rw-r--r--. 1 root root 0 Sep 21 14:43 hosts
-rw-r--r--. 1 root root 965 Jun 25 21:11 hosts.original
drwxr-xr-x. 2 root root 6 Jun 25 21:11 roles

3. Create a section in the new 'hosts' file called 'local'. Make sure it
contains:
* Localhost
* Localhost.Localdomain
* 127.0.0.1
[root@tcox3 ansible]# vim hosts
[root@tcox3 ansible]# cat hosts
[local]
127.0.0.1
localhost
localhost.localdomain

4. Create a second section called 'web hosts' with the NAME of the second
Linux Academy lab server in your environment (see previous lab for setting
up the two host environment). Display the contents of the file.
[root@tcox3 ansible]# vim hosts
[root@tcox3 ansible]# cat hosts
[local]
127.0.0.1
localhost
localhost.localdomain

[web hosts]
tcox4.mylabserver.com
1. Verify that Ansible is running by displaying the version and module path.
Change to the Ansible configuration directory and list the contents.
[root@tcox3 ~]# ansible --version
ansible 1.9.2
configured module search path = None
[root@tcox3 ~]# cd /etc/ansible
[root@tcox3 ansible]# ll
total 20
-rw-r--r--. 1 root root 8629 Jun 25 21:11 ansible.cfg
-rw-r--r--. 1 root root 88 Sep 21 14:43 hosts
drwxr-xr-x. 2 root root 6 Jun 25 21:11 roles

2. Move the original Ansible Hosts file to another file in the same directory
called 'hosts.original'. Create a new empty 'hosts' file in the default
configuration directory location.
[root@tcox3 ansible]# mv hosts hosts.original && touch hosts && ll
total 20
-rw-r--r--. 1 root root 8629 Jun 25 21:11 ansible.cfg
-rw-r--r--. 1 root root 0 Sep 21 14:43 hosts
-rw-r--r--. 1 root root 965 Jun 25 21:11 hosts.original
drwxr-xr-x. 2 root root 6 Jun 25 21:11 roles

3. Create a section in the new 'hosts' file called 'local'. Make sure it
contains:
* Localhost
* Localhost.Localdomain
* 127.0.0.1
[root@tcox3 ansible]# vim hosts
[root@tcox3 ansible]# cat hosts
[local]
127.0.0.1
localhost
localhost.localdomain
4. Create a second section called 'web hosts' with the NAME of the second
Linux Academy lab server in your environment (see previous lab for setting
up the two host environment). Display the contents of the file.
[root@tcox3 ansible]# vim hosts
[root@tcox3 ansible]# cat hosts
[local]
127.0.0.1
localhost
localhost.localdomain

[web hosts]
tcox4.mylabserver.com

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Using the 'setup' module for ansible, list all of the known facts on the
local system.
[test@tcox3 ~]$ ansible local -m setup
localhost | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.31.107.208"
],
"ansible_all_ipv6_addresses": [
"fe80::103f:68ff:fefe:2bd1"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/06/2015",
"ansible_bios_version": "4.2.amazon",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-
229.14.1.el7.x86_64",
(NOTE: Your output will be larger as this was concatenated
for space and readability)

4. Using the 'setup' module for ansible, list all of the known facts for all
systems configured in the 'hosts' file on the system.
[test@tcox3 ~]$ ansible all -m setup
(NOTE: Localhost truncated here... starting with another host output)
tcox4.mylabserver.com | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.31.108.45"
],
"ansible_all_ipv6_addresses": [
"fe80::1029:fbff:fed6:9aaf"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/06/2015",
"ansible_bios_version": "4.2.amazon",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-
229.14.1.el7.x86_64",
(NOTE: Your output will be larger as this was concatenated for space
and readability)

5. While listing the facts on the local system, filter the list showing only the
content pertaining to the system IP addresses.
[test@tcox3 ~]$ ansible local -m setup -a 'filter=ans*ipv4*'
localhost | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.31.107.208"
],
"ansible_default_ipv4": {
"address": "172.31.107.208",
"alias": "eth0",
"gateway": "172.31.96.1",
"interface": "eth0",
"macaddress": "12:3f:68:fe:2b:d1",
"mtu": 9001,
"netmask": "255.255.240.0",
"network": "172.31.96.0",
"type": "ether"
}
},
"changed": false
}

1. Display the current version and status of the Ansible application.


[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Using the appropriate module, from your control server, list all the hosts
configured in your environment. Using ansible, run a ping command
against all hosts in the environment.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com
[test@tcox3 ~]$ ansible all -m ping
localhost | success >> {
"changed": false,
"ping": "pong"
}
tcox5.mylabserver.com | success >> {
"changed": false,
"ping": "pong"
}

tcox4.mylabserver.com | success >> {


"changed": false,
"ping": "pong"
}

3. List the contents of your ansible 'hosts' file. Using only ONE of the
groups in the file, attempt to install the 'lynx' package on that server.
[test@tcox3 ~]$ cat /etc/ansible/hosts
[local]
localhost

[apacheweb]
tcox4.mylabserver.com

[appserver]
tcox5.mylabserver.com

[test@tcox3 ~]$ ansible apacheweb -s -m shell -a 'yum -y install


lynx'
tcox4.mylabserver.com | success | rc=0 >>
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: distro.ibiblio.org
* epel: mirror.symnds.com
* extras: mirror.cogentco.com
* updates: mirror.cogentco.com
Package lynx-2.8.8-0.3.dev15.el7.x86_64 already installed and latest
version
Nothing to do
4. Using the same group as Step #3, attempt to install the 'telnet' package
on that server using the more 'playbook friendly' module and syntax.
[test@tcox3 ~]$ ansible apacheweb -s -m yum -a 'pkg=telnet
state=installed update_cache=true'
tcox4.mylabserver.com | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"telnet-0.17-59.el7.x86_64 providing telnet is already
installed"
]
}

1. On the control server, display the ansible version and status and issue
an ansible command to list all configured hosts.
[test@tcox3 ~]$ ansible --version

ansible 1.9.2

configured module search path = None

[test@tcox3 ~]$ ansible all --list-hosts

tcox5.mylabserver.com

localhost

tcox4.mylabserver.com

2. Choosing ONE of the groups from the displayed list in Step #1 above,
query that system for all the 'facts' that can be displayed, while filtering the
content for the IP address information.
[test@tcox3 ~]$ ansible apacheweb -m setup -a 'filter=ans*ipv4*'
tcox4.mylabserver.com | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.31.108.45"
],
"ansible_default_ipv4": {
"address": "172.31.108.45",
"alias": "eth0",
"gateway": "172.31.96.1",
"interface": "eth0",
"macaddress": "12:29:fb:d6:9a:af",
"mtu": 9001,
"netmask": "255.255.240.0",
"network": "172.31.96.0",
"type": "ether"
}
},
"changed": false
}

3. Using the same group from Steps #1 and #2, issue a shell command
through ansible that will determine if the 'lynx' package is already installed
on that server.
[test@tcox3 ~]$ ansible apacheweb -m shell -a 'yum list installed |
grep lynx'
tcox4.mylabserver.com | success | rc=0 >>
lynx.x86_64 2.8.8-0.3.dev15.el7
@base

4. Using the same group, issue an ansible command (as sudo) that will
display the last ten lines of output from the remote system's 'syslog' file.
[test@tcox3 ~]$ ansible apacheweb -m shell -a 'tail -n 10
/var/log/dmesg'
tcox4.mylabserver.com | success | rc=0 >>
[ 3.577455] SELinux: initialized (dev autofs, type autofs), uses
genfs_contexts
[ 3.621406] SELinux: initialized (dev hugetlbfs, type hugetlbfs),
uses transition SIDs
[ 3.748973] systemd-udevd[504]: starting version 208
[ 3.828536] Installing knfsd (copyright (C) 1996
okir@monad.swb.de).
[ 3.839265] SELinux: initialized (dev nfsd, type nfsd), uses
genfs_contexts
[ 3.949670] piix4_smbus 0000:00:01.3: SMBus base address
uninitialized - upgrade BIOS or use force_addr=0xaddr
[ 4.082323] input: PC Speaker as
/devices/platform/pcspkr/input/input4
[ 4.191443] type=1305 audit(1443204264.317:4): audit_pid=546
old=0 auid=4294967295 ses=4294967295
subj=system_u:system_r:auditd_t:s0 res=1
[ 4.277258] ppdev: user-space parallel port driver
[ 4.459507] Adding 2097148k swap on /root/swap. Priority:-1
extents:2 across:3037200k SSFS

1. While logged into your control server, create a directory called


'Playbooks' and create an empty file in it called 'deploy_DATE.yml'.
Replace DATE with today's date.
[test@tcox3 ~]$ mkdir Playbooks
[test@tcox3 ~]$ cd Playbooks
[test@tcox3 ~]$ touch deploy_09242015.yml

2. Edit the 'deploy_DATE.yml' file and create a structure that will run the
following against ONE of the groups in your host configuration:
- Using the package installation module, install lynx package
- Using the package installation module, determine if the telnet package is
installed
CONTENT SHOULD LOOK SOMETHING LIKE:
- hosts: appserver
tasks:
- name: Install Lynx on App Servers
yum: pkg=lynx state=installed update_cache=true
- name: Querying for Telnet Install
yum: pkg=telnet state=present update_cache=true

3. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook -s deploy_09242015.yml

PLAY [appserver]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [tcox5.mylabserver.com]

TASK: [Install Lynx on App Servers]


*******************************************
ok: [tcox5.mylabserver.com]

TASK: [Querying for Telnet Install]


*******************************************
ok: [tcox5.mylabserver.com]

PLAY RECAP
********************************************************************
tcox5.mylabserver.com : ok=3 changed=0 unreachable=0
failed=0

4. Edit the playbook in Step #2 and create a new section for a DIFFERENT
group in your host configuration as follows:
- Using the package installation module, install the telnet package
- Using the package installation module, determine if the lynx package is
installed
FULL FILE SHOULD NOW LOOK SOMETHING LIKE:
- hosts: appserver
tasks:
- name: Install Lynx on App Servers
yum: pkg=lynx state=installed update_cache=true
- name: Querying for Telnet Install
yum: pkg=telnet state=present update_cache=true

- hosts: apacheweb
tasks:
- name: Install Lynx on Web Servers
yum: pkg=telnet state=installed update_cache=true
- name: Querying for Lynx Install
yum: pkg=lynx state=present update_cache=true

5. Run the full playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook -s deploy_09242015.yml

PLAY [appserver]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [tcox5.mylabserver.com]

TASK: [Install Lynx on App Servers]


*******************************************
ok: [tcox5.mylabserver.com]

TASK: [Querying for Telnet Install]


*******************************************
ok: [tcox5.mylabserver.com]

PLAY [apacheweb]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [tcox4.mylabserver.com]

TASK: [Install Lynx on Web Servers]


*******************************************
ok: [tcox4.mylabserver.com]

TASK: [Querying for Lynx Install]


*********************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=3 changed=0 unreachable=0
failed=0
tcox5.mylabserver.com : ok=3 changed=0 unreachable=0
failed=0

6. Tail the last lines of the ansible log file and compare to the results in
Step #5.
[test@tcox3 Playbooks]$ tail /var/log/ansible.log
2015-09-25 18:25:43,538 p=1682 u=test |
2015-09-25 18:25:43,569 p=1682 u=test | PLAY [appserver]
**************************************************************
2015-09-25 18:25:43,569 p=1682 u=test | GATHERING FACTS
***************************************************************
2015-09-25 18:25:43,782 p=1682 u=test | ok: [tcox5.mylabserver.com]
2015-09-25 18:25:43,783 p=1682 u=test | TASK: [Install Lynx on App
Servers] *******************************************
2015-09-25 18:25:47,166 p=1682 u=test | ok: [tcox5.mylabserver.com]
2015-09-25 18:25:47,166 p=1682 u=test | TASK: [Querying for Telnet
Install] *******************************************
2015-09-25 18:25:51,595 p=1682 u=test | ok: [tcox5.mylabserver.com]
2015-09-25 18:25:51,596 p=1682 u=test | PLAY RECAP
********************************************************************
2015-09-25 18:25:51,596 p=1682 u=test | tcox5.mylabserver.com
: ok=3 changed=0 unreachable=0 failed=0
1. Run the appropriate ansible command to list all the hosts that are
configured on your system for access by the control server. Display the
ansible 'hosts' file in active use on the control server so that the hosts and
groups are displayed.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com
[test@tcox3 ~]$ cat /etc/ansible/hosts
[local]
localhost

[apacheweb]
tcox4.mylabserver.com

[appserver]
tcox5.mylabserver.com

2. Create a directory called "Playbooks". Change to that directory and


create a playbook called 'myfirstplaybook.yml' in that directory. Edit the file
(and then display) so that it has the initial comment at the top (formatted
appropriately) called "My First Playbook".
[test@tcox3 ~]$ mkdir Playbooks
[test@tcox3 ~]$ cd Playbooks
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible

3. Edit the playbook and create a target section that will run against either
one of the defined hosts in Step #1 above or a group of hosts in Step #1
above. Save the plabook and execute the appropriate command to run the
playbook and display the results.
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb

[test@tcox3 Playbooks]$ ansible-playbook -s myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

4. Edit the playbook and add the following to the target section we just
created:
- Force use of SSH connections
- Always run the playbook as the user 'test'
- Run this playbook as SUDO by default
- Do not use the setup module to gather facts from systems during
execution
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no

5. Display the file and run the playbook, displaying the results of the run.
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no

[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

PLAY RECAP
********************************************************************

1. Run the appropriate ansible command to list all the hosts that are
configured on your system for access by the control server. Display the
ansible 'hosts' file in active use on the control server so that the hosts and
groups are displayed.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com
[test@tcox3 ~]$ cat /etc/ansible/hosts
[local]
localhost
[apacheweb]
tcox4.mylabserver.com
[appserver]
tcox5.mylabserver.com

2. Create a directory called 'conf'. Change to that directory and create two
files, one called 'copyright.yml' and one called 'webdefaults.yml'. In the
copyright file, create variables called 'message' and 'author' containing
appropriate text of your choice. In the webdefaults file, create two variables
called 'apache_version' and 'apache_mod_req' with values of your choice.
Display the files.
[test@tcox3 Playbooks]$ mkdir conf
[test@tcox3 Playbooks]$ cd conf
[test@tcox3 conf]$ cat
copyright.yml webdefaults.yml
[test@tcox3 conf]$ cat copyright.yml
---
message: Copyright 2015 by Linux Academy
author: Linux Academy
[test@tcox3 conf]$ cat webdefaults.yml
---
apache_version: 2.6
apache_mod_req: mod_ssl

3. Change back to the 'Playbooks' directory and edit your


'myfirstplaybook.yml' file. Create a variables section that contains a local
variable called 'playbook_version' and set it to a value appropriate to your
desires. Display the file and then run the playbook.
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
vars:
playbook_version: 0.1b

[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

PLAY RECAP
********************************************************************

4. Edit the 'myfirstplaybook.yml' file and create another variables section


that appropriately imports the external files that we created in Step #2.
Display the newly edited file.
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
vars:
playbook_version: 0.1b
vars_files:
- conf/copyright.yml
- conf/webdefaults.yml

5. Run the ansible playbook just edited and note the results.
[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml
PLAY [apacheweb]
**************************************************************

PLAY RECAP
********************************************************************
1. Run the appropriate ansible command to list all the hosts that are
configured on your system for access by the control server. Display the
ansible 'hosts' file in active use on the control server so that the hosts and
groups are displayed.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com
[test@tcox3 ~]$ cat /etc/ansible/hosts
[local]
localhost
[apacheweb]
tcox4.mylabserver.com
[appserver]
tcox5.mylabserver.com

2. Edit the previously created 'myfirstplaybook.yml' file from our previous


exercises. Create a task section that uses the 'yum' module to install the
apache web server on the host(s) indicated in the playbook. Display the file
after editing.
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
vars:
playbook_version: 0.1b
vars_files:
- conf/copyright.yml
- conf/webdefaults.yml
tasks:
- name: Install Apache Web Server
action: yum name=httpd state=installed

3. Run the playbook, after the initial installation of apache, run the playbook
a second time to note the differing output.
[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install Apache Web Server]


*********************************************
changed: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=1 unreachable=0
failed=0

[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install Apache Web Server]


*********************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

4. Add another task to the playbook that uses the 'yum' module to check
and see if the 'lynx' package is installed and then display that file.
[test@tcox3 Playbooks]$ vim myfirstplaybook.yml
[test@tcox3 Playbooks]$ cat myfirstplaybook.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
vars:
playbook_version: 0.1b
vars_files:
- conf/copyright.yml
- conf/webdefaults.yml
tasks:
- name: Install Apache Web Server
action: yum name=httpd state=installed
- name: Verify the Lynx Web Browser
action: yum name=lynx state=present

5. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook myfirstplaybook.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install Apache Web Server]


*********************************************
ok: [tcox4.mylabserver.com]

TASK: [Verify the Lynx Web Browser]


*******************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the concepts discussed in the LOOP video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Defines a list of users within the playbook, using the 'user' module,
iterates through that list of users, adding them all to the remote system
[test@tcox3 Playbooks]$ vim loop.yml
[test@tcox3 Playbooks]$ cat loop.yml
--- # LOOP Playbook Example
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Add a list of users
user: name={{ item }} state=present
with_items:
- user1
- user2
- user3

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook loop.yml

PLAY [apacheweb]
**************************************************************

TASK: [Add a list of users]


***************************************************
ok: [tcox4.mylabserver.com] => (item=user1)
ok: [tcox4.mylabserver.com] => (item=user2)
ok: [tcox4.mylabserver.com] => (item=user3)

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

[test@tcox3 Playbooks]$ ssh tcox4


Last login: Wed Oct 14 15:22:34 2015 from ec2-52-91-231-138.compute-
1.amazonaws.com
[test@tcox4 ~]$ cat /etc/passwd| grep user
user1:x:1005:1005::/home/user1:/bin/bash
user2:x:1006:1006::/home/user2:/bin/bash
user3:x:1007:1007::/home/user3:/bin/bash

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'NOTIFY' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Installs the 'Apache Web Server' using the appropriate package module
- Upon installation of the web server, notifies the appropriately titled handler
to restart the service using the 'service' module
[test@tcox3 Playbooks]$ vim notify.yml
[test@tcox3 Playbooks]$ cat notify.yml
--- # My First YAML Playbook for Ansible
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Install Apache Web Server
action: yum name=httpd state=installed
notify: Restart HTTPD
handlers:
- name: Restart HTTPD
action: service name=httpd state=restarted

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook notify.yml

PLAY [apacheweb]
**************************************************************
TASK: [Install Apache Web Server]
*********************************************
changed: [tcox4.mylabserver.com]

NOTIFIED: [Restart HTTPD]


*****************************************************
changed: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=2 changed=2 unreachable=0
failed=0

[test@tcox3 Playbooks]$ ssh tcox4


Last login: Wed Oct 14 15:27:43 2015 from ec2-52-91-231-138.compute-
1.amazonaws.com
[test@tcox4 ~]$ ps aux | grep http
root 2042 0.0 0.4 213080 4752 ? Ss 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
apache 2043 0.0 0.2 213080 2752 ? S 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
apache 2044 0.0 0.2 213080 2752 ? S 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
apache 2045 0.0 0.2 213080 2752 ? S 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
apache 2046 0.0 0.2 213080 2752 ? S 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
apache 2047 0.0 0.2 213080 2752 ? S 15:27 0:00
/usr/sbin/httpd -DFOREGROUND
test 2077 0.0 0.0 112640 964 pts/0 R+ 15:28 0:00
grep --color=auto http

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None
2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'WHEN' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Gathers remote facts
- Installs the 'Apache Web Server' using the appropriate package module
- Each section will determine WHICH Apache package to install based on
the value of the fact called 'ansible_os_family' and using the appropriate
module for package management
[test@tcox3 Playbooks]$ vim when.yml
[test@tcox3 Playbooks]$ cat when.yml
--- # WHEN Playbook Example
- hosts: aptserver
user: test
sudo: yes
connection: ssh
vars:
playbook_type: conditionalexample
vars_files:
- conf/copyright.yml
- conf/webdefaults.yml
tasks:
- name: Install Apache Appropriate to the Distribution Type
(Debian/Ubuntu)
command: apt-get -y install apache2
when: ansible_os_family == "Debian"
- name: Install Apache Appropriate to the Distribution Type
(RedHat/CentOS)
command: yum -y install httpd
when: ansible_os_family == "RedHat"
4. Run the playbook and display the results.
[test@tcox3 Playbooks]$ ansible-playbook when.yml

PLAY [aptserver]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [tcox1.mylabserver.com]

TASK: [Install Apache Appropriate to the Distribution Type


(Debian/Ubuntu)] ***
changed: [tcox1.mylabserver.com]

TASK: [Install Apache Appropriate to the Distribution Type


(RedHat/CentOS)] ***
skipping: [tcox1.mylabserver.com]

PLAY RECAP
********************************************************************

tcox1.mylabserver.com : ok=2 changed=1 unreachable=0


failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'LOOKUP' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Display a JSON formatted debug message that will show the remote
environment HOME variable
[test@tcox3 Playbooks]$ vim lookup.yml
[test@tcox3 Playbooks]$ cat lookup.yml
--- # LOOKUP PLAYBOOK EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- debug: msg="{{ lookup('env','HOME') }} is the value listed"

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook lookup.yml

PLAY [apacheweb]
**************************************************************

TASK: [debug msg="/home/test is the value listed"]


****************************
ok: [tcox4.mylabserver.com] => {
"msg": "/home/test is the value listed"
}

PLAY RECAP
********************************************************************

tcox4.mylabserver.com : ok=1 changed=0 unreachable=0


failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com
3. Create a playbook, using the 'LOCAL ACTION' concepts from the video:
- Uses local connection
- Logs in to the remote system as 'test' user
- Connects to the localhost
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Installs the 'Telnet Client' using the appropriate package module
[test@tcox3 Playbooks]$ vim local.yml
[test@tcox3 Playbooks]$ cat local.yml
--- # LOCAL ACTION PLAYBOOK
- hosts: 127.0.0.1
connection: local
tasks:
- name: Install Telnet Client
yum: pkg=telnet state=latest

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook local.yml

PLAY [127.0.0.1]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [127.0.0.1]

TASK: [Install Telnet Client]


*************************************************
ok: [127.0.0.1]

PLAY RECAP
********************************************************************

127.0.0.1 : ok=2 changed=0 unreachable=0


failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None
2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'RUN ONCE' concepts from the video:
- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Runs the remote 'uptime' command and logs it to the remote /home/test
directory in a file called 'uptime.log'
- List the remote /var directory and pipe the output to the /home/test
directory in a file called 'dir.list'
- Indicate that the directory listing command should only be run ONCE on
the first host and not again
[test@tcox3 Playbooks]$ vim runonce.yml
[test@tcox3 Playbooks]$ cat runonce.yml
--- # RUNONCE PLAYBOOK EXAMPLE
- hosts: all
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Run the uptime command on all hosts and log it
raw: /usr/bin/uptime >> /home/test/uptime.log
- name: List the /var directory and log it
raw: ls -al /var >> /home/test/dir.list
run_once: true
4. Run the playbook and display the results.
[test@tcox3 Playbooks]$ ansible-playbook runonce.yml

PLAY [all]
********************************************************************

TASK: [Run the uptime command on all hosts and log it]
************************
ok: [localhost]
ok: [tcox4.mylabserver.com]
ok: [tcox5.mylabserver.com]
ok: [tcox1.mylabserver.com]

TASK: [List the /var directory and log it]


************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************

localhost : ok=2 changed=0 unreachable=0


failed=0
tcox1.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0
tcox4.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0
tcox5.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'PROMPT' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Creates a local variable called 'playbook_version' and assigns some value
- Prompts the playbook user for a package to install, defaulting to the telnet
package if nothing is entered. This command should echo the typed output
to the console.
[test@tcox3 Playbooks]$ vim prompt.yml
[test@tcox3 Playbooks]$ cat prompt.yml
--- # PROMPT FOR USER PACKAGE EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
vars:
playbook_version: 0.01b
vars_prompt:
- name: pkgtoinstall
prompt: Install Which Package?
default: telnet
private: no
tasks:
- name: Install the indicated package
yum: pkg={{ pkgtoinstall }} state=latest

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook prompt.yml
Install Which Package? [telnet]: lynx

PLAY [apacheweb]
**************************************************************

TASK: [Install the indicated package]


*****************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'INCLUDE' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Create a file in a directory called 'plays', this file should contain the single
task to install the telnet package
- Include that file in your playbook as a task that will result in the play in the
file in the step above being run
- Using the 'raw' module, verify the telnet package was installed and write
the results to the remote server /home/test directory in a file called
'pkgs.log'
[test@tcox3 Playbooks]$ vim include.yml
[test@tcox3 Playbooks]$ cat include.yml
--- # FULL INCLUDE TASKS EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- include: plays/packages.yml
- name: Verify the telnet package is installed
raw: yum list installed | grep telnet > /home/test/pkgs.log

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook include.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install the telnet client]


*********************************************
ok: [tcox4.mylabserver.com]

TASK: [Install the Lynx web browser]


******************************************
ok: [tcox4.mylabserver.com]

TASK: [Verify the telnet package is installed]


********************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=3 changed=0 unreachable=0
failed=0

[test@tcox3 Playbooks]$ ssh tcox4


Last login: Tue Oct 27 13:25:23 2015 from ec2-52-23-228-15.compute-
1.amazonaws.com
[test@tcox4 ~]$ ls -al pkgs.log
-rw-r--r--. 1 root root 81 Oct 27 13:25 pkgs.log
[test@tcox4 ~]$ cat pkgs.log
telnet.x86_64 1:0.17-59.el7
@base

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'TAGS' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Using the 'yum' module, install the telnet and lynx packages. Tag that
tasks with a label called 'packages'
- Using the 'raw' module, verify that telnet is now installed on the remote
server, write the results to the /home/test directory in a file called 'pkg.log'.
Tag this task with a label called 'logging'
[test@tcox3 Playbooks]$ vim tags.yml
[test@tcox3 Playbooks]$ cat tags.yml
--- # TAG FUNCTIONALITY EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Install the telnet and lynx packages
yum: pkg={{ item }} state=latest
with_items:
- telnet
- lynx
tags:
- packages
- name: Verify that telnet was installed
raw: yum list installed | grep telnet > /home/test/pkg.log
tags:
- logging

4. Run the playbook and display the results. Run the playbook a second
time and ONLY run the tag called 'packages'.
[test@tcox3 Playbooks]$ ansible-playbook tags.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install the telnet and lynx packages]


**********************************
ok: [tcox4.mylabserver.com] => (item=telnet,lynx)

TASK: [Verify that telnet was installed]


**************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0

[test@tcox3 Playbooks]$ ansible-playbook --tag "packages" tags.yml


PLAY [apacheweb]
**************************************************************

TASK: [Install the telnet and lynx packages]


**********************************
ok: [tcox4.mylabserver.com] => (item=telnet,lynx)

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'UNTIL' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Installs the Apache web server using the appropriate package
management module
- Runs a command to verify that the 'systemctl' status command shows the
httpd service is running
- Register the result of the above command, using the debug module to
display the value captured during each iteration
- This command should run FIVE times with a break of FIVE seconds
between each try until the command succeeds OR the fifth loop indicates
the service did not start
[test@tcox3 Playbooks]$ ansible-playbook until.yml

PLAY [apacheweb]
**************************************************************

TASK: [Installing Apache Web Server]


******************************************
changed: [tcox4.mylabserver.com]

TASK: [Verify Service Status]


*************************************************
changed: [tcox4.mylabserver.com]

TASK: [debug var=result]


******************************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"result": {
"attempts": 3,
"changed": true,
"cmd": "systemctl status httpd",
"delta": "0:00:00.007014",
"end": "2015-11-02 23:34:29.364704",
"invocation": {
"module_args": "systemctl status httpd",
"module_name": "shell"
},
"rc": 0,
"start": "2015-11-02 23:34:29.357690",
"stderr": "",
"stdout": "httpd.service - The Apache HTTP Server\n
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)\n
Active: active (running) since Mon 2015-11-02 23:34:25 UTC; 4s ago\n
Main PID: 1701 (httpd)\n Status: \"Processing requests...\"\n
CGroup: /system.slice/httpd.service\n |-1701
/usr/sbin/httpd -DFOREGROUND\n |-1702 /usr/sbin/httpd -
DFOREGROUND\n |-1703 /usr/sbin/httpd -
DFOREGROUND\n |-1704 /usr/sbin/httpd -
DFOREGROUND\n |-1705 /usr/sbin/httpd -
DFOREGROUND\n `-1706 /usr/sbin/httpd -DFOREGROUND\n\nNov
02 23:34:25 tcox4.mylabserver.com systemd[1]: Starting The Apache
HTTP Server...\nNov 02 23:34:25 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server.",
"stdout_lines": [
"httpd.service - The Apache HTTP Server",
" Loaded: loaded
(/usr/lib/systemd/system/httpd.service; disabled)",
" Active: active (running) since Mon 2015-11-02
23:34:25 UTC; 4s ago",
" Main PID: 1701 (httpd)",
" Status: \"Processing requests...\"",
" CGroup: /system.slice/httpd.service",
" |-1701 /usr/sbin/httpd -DFOREGROUND",
" |-1702 /usr/sbin/httpd -DFOREGROUND",
" |-1703 /usr/sbin/httpd -DFOREGROUND",
" |-1704 /usr/sbin/httpd -DFOREGROUND",
" |-1705 /usr/sbin/httpd -DFOREGROUND",
" `-1706 /usr/sbin/httpd -DFOREGROUND",
"",
"Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]:
Starting The Apache HTTP Server...",
"Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server."
],
"warnings": []
}
}
}

PLAY RECAP
********************************************************************

tcox4.mylabserver.com : ok=3 changed=2 unreachable=0


failed=0

HERE IS AN EXAMPLE OF WHAT YOUR PLAYBOOK COULD LOOK LIKE:


--- # UNTIL EXAMPLE
- hosts: apacheweb
sudo: yes
connection: ssh
user: test
gather_facts: no
tasks:
- name: Installing Apache Web Server
yum: pkg=httpd state=latest
- name: Verify Service Status
shell: systemctl status httpd
register: result
until: result.stdout.find(“active (running)”) != -1 retries: 5
delay: 5 - debug: var=result

4. Run the playbook and display the results.


See above for full output
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'IGNORE_ERRORS' concepts from the


video:
- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to the server/group in Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Run a command on the remote server that will ALWAYS fail
- Indicate that the playbook should continue to run even if errors are
generated
- Using the 'yum' module, install the telnet client AFTER the above
command to show the playbook continues
[test@tcox3 Playbooks]$ vim failure.yml
[test@tcox3 Playbooks]$ cat failure.yml
--- # ERROR HANDLING EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Execute a command that will fail
command: /bin/false
ignore_errors: yes
- name: Install telnet
yum: pkg=telnet state=latest

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook failure.yml

PLAY [apacheweb]
**************************************************************

TASK: [Execute a command that will fail]


**************************************
failed: [tcox4.mylabserver.com] => {"changed": true, "cmd":
["/bin/false"], "delta": "0:00:00.001857", "end": "2015-10-28
17:54:49.571966", "rc": 1, "start": "2015-10-28 17:54:49.570109",
"warnings": []}
...ignoring

TASK: [Install telnet]


********************************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=2 changed=1 unreachable=0
failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'INCLUDE' concepts from the video:


- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to ALL servers
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Create a directory called 'plays'. Create a YAML file that runs a task to
install one or more packages using the 'yum' module
- Include that file in the playbook so that the created YAML file in the 'plays'
directory above runs in the 'tasks' section
- After the included tasks run, verify that the package is installed remotely
using the 'raw' module, log the output to a file called 'pkgs.log' in remote
/home/test
[test@tcox3 Playbooks]$ vim include.yml
[test@tcox3 Playbooks]$ cat include.yml
--- # FULL INCLUDE TASKS EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- include: plays/packages.yml
- name: Verify the telnet package is installed
raw: yum list installed | grep telnet > /home/test/pkgs.log

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook include.yml

PLAY [apacheweb]
**************************************************************

TASK: [Install the telnet client]


*********************************************
ok: [tcox4.mylabserver.com]

TASK: [Install the Lynx web browser]


******************************************
changed: [tcox4.mylabserver.com]

TASK: [Verify the telnet package is installed]


********************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=3 changed=1 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create an outline for a playbook that lists the following (name it


'webserver.txt'):
- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to web servers
- The playbook runs as 'sudo'
- Indicate to capture a pre-task timestamp
- Install the apache web server, telnet client and lynx web browser
- Capture a log of all installed packages on the system
- Indicate to capture a post-task timestamp
[test@tcox3 Outline]$ vim webserver.txt
[test@tcox3 Outline]$ cat webserver.txt
- webservers
- test user
- sudo rights

- date/time stamp for when the playbook start

- install the apache web server


- start the web service

- verify that the web service is running

- install client software


- telnet
- lynx

- log all the packages install on the system

- date/time stamp for when the playbook ends

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the the 'outline' created in our previous


exercise
test@tcox3 Outline]$ vim webserver.original
[test@tcox3 Outline]$ cat webserver.original
--- # Outline to Playbook Translation
- hosts: apacheweb
user: test
sudo: yes
gather_facts: no
tasks:
- name: date/time stamp for when the playbook starts
raw: /usr/bin/date > /home/test/playbook_start.log
- name: install the apache web server
yum: pkg=httpd state=latest
- name: start the web service
service: name=httpd state=restarted
- name: verify that the web service is running
command: systemctl status httpd
register: result
- debug: var=result
- name: install client software - telnet
yum: pkg=telnet state=latest
- name: install client software - lynx
yum: pkg=lynx state=latest
- name: log all the packages install on the system
raw: yum list installed > /home/test/installed.log
- name: date/time stamp for when the playbook ends
raw: /usr/bin/date > /home/test/playbook_end.log

4. Run the playbook and display the results.


[test@tcox3 Outline]$ ansible-playbook webserver.original

PLAY [apacheweb]
**************************************************************
TASK: [date/time stamp for when the playbook starts]
**************************
ok: [tcox4.mylabserver.com]

TASK: [install the apache web server]


*****************************************
changed: [tcox4.mylabserver.com]

TASK: [start the web service]


*************************************************
changed: [tcox4.mylabserver.com]

TASK: [verify that the web service is running]


********************************
changed: [tcox4.mylabserver.com]

TASK: [debug var=result]


******************************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"result": {
"changed": true,
"cmd": [
"systemctl",
"status",
"httpd"
],
"delta": "0:00:00.008202",
"end": "2015-11-02 23:51:52.712205",
"invocation": {
"module_args": "systemctl status httpd",
"module_name": "command"
},
"rc": 0,
"start": "2015-11-02 23:51:52.704003",
"stderr": "",
"stdout": "httpd.service - The Apache HTTP Server\n
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)\n
Active: active (running) since Mon 2015-11-02 23:51:52 UTC; 176ms
ago\n Main PID: 2019 (httpd)\n Status: \"Processing
requests...\"\n CGroup: /system.slice/httpd.service\n |-
2019 /usr/sbin/httpd -DFOREGROUND\n |-2020 /usr/sbin/httpd
-DFOREGROUND\n |-2021 /usr/sbin/httpd -
DFOREGROUND\n |-2022 /usr/sbin/httpd -
DFOREGROUND\n |-2023 /usr/sbin/httpd -
DFOREGROUND\n `-2024 /usr/sbin/httpd -DFOREGROUND\n\nNov
02 23:51:52 tcox4.mylabserver.com systemd[1]: Starting The Apache
HTTP Server...\nNov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server.",
"stdout_lines": [
"httpd.service - The Apache HTTP Server",
" Loaded: loaded
(/usr/lib/systemd/system/httpd.service; disabled)",
" Active: active (running) since Mon 2015-11-02
23:51:52 UTC; 176ms ago",
" Main PID: 2019 (httpd)",
" Status: \"Processing requests...\"",
" CGroup: /system.slice/httpd.service",
" |-2019 /usr/sbin/httpd -DFOREGROUND",
" |-2020 /usr/sbin/httpd -DFOREGROUND",
" |-2021 /usr/sbin/httpd -DFOREGROUND",
" |-2022 /usr/sbin/httpd -DFOREGROUND",
" |-2023 /usr/sbin/httpd -DFOREGROUND",
" `-2024 /usr/sbin/httpd -DFOREGROUND",
"",
"Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Starting The Apache HTTP Server...",
"Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server."
],
"warnings": []
}
}
}

TASK: [install client software - telnet]


**************************************
changed: [tcox4.mylabserver.com]

TASK: [install client software - lynx]


****************************************
changed: [tcox4.mylabserver.com]

TASK: [log all the packages install on the system]


****************************
ok: [tcox4.mylabserver.com]

TASK: [date/time stamp for when the playbook ends]


****************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************

tcox4.mylabserver.com : ok=9 changed=5 unreachable=0


failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Optimize the playbook created in our previous exercise such that:


- Handlers are in the appropriate section
- The 'raw' module is replaced by one of the other shell modules that allow
us to 'register' the output for display
- Register the result of any shell module output for local JSON display with
debug statements
[test@tcox3 Outline]$ vim webserver.yml
[test@tcox3 Outline]$ cat webserver.yml
--- # Outline to Playbook Translation
- hosts: apacheweb
user: test
sudo: yes
gather_facts: no
tasks:
- name: date/time stamp for when the playbook starts
command: /usr/bin/date
register: timestamp_start
- debug: var=timestamp_start
- name: install the apache web server
yum: pkg=httpd state=latest
notify: Start HTTPD
- name: verify that the web service is running
command: systemctl status httpd
register: result
ignore_errors: yes
- debug: var=result
- name: install client software - telnet
yum: pkg=telnet state=latest
- name: install client software - lynx
yum: pkg=lynx state=latest
- name: log all the packages install on the system
command: yum list installed
register: installed_result
- debug: var=installed_result
- name: date/time stamp for when the playbook ends
command: /usr/bin/date
register: timestamp_end
- debug: var=timestamp_end
handlers:
- name: Start HTTPD
service: name=httpd state=restarted
4. Run the playbook and display the results.
[test@tcox3 Outline]$ ansible-playbook webserver.yml

PLAY [apacheweb]
**************************************************************

TASK: [date/time stamp for when the playbook starts]


**************************
changed: [tcox4.mylabserver.com]

TASK: [debug var=timestamp_start]


*********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"timestamp_start": {
"changed": true,
"cmd": [
"/usr/bin/date"
],
"delta": "0:00:00.001476",
"end": "2015-11-02 23:55:34.125425",
"invocation": {
"module_args": "/usr/bin/date",
"module_name": "command"
},
"rc": 0,
"start": "2015-11-02 23:55:34.123949",
"stderr": "",
"stdout": "Mon Nov 2 23:55:34 UTC 2015",
"stdout_lines": [
"Mon Nov 2 23:55:34 UTC 2015"
],
"warnings": []
}
}
}

TASK: [install the apache web server]


*****************************************
changed: [tcox4.mylabserver.com]

TASK: [verify that the web service is running]


********************************
failed: [tcox4.mylabserver.com] => {"changed": true, "cmd":
["systemctl", "status", "httpd"], "delta": "0:00:00.010313", "end":
"2015-11-02 23:55:38.917295", "rc": 3, "start": "2015-11-02
23:55:38.906982", "warnings": []}
stdout: httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: inactive (dead)

Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]: Starting The


Apache HTTP Server...
Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]: Started The Apache
HTTP Server.
Nov 02 23:49:27 tcox4.mylabserver.com systemd[1]: Stopping The
Apache HTTP Server...
Nov 02 23:49:28 tcox4.mylabserver.com systemd[1]: Stopped The Apache
HTTP Server.
Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]: Stopped The Apache
HTTP Server.
Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]: Starting The
Apache HTTP Server...
Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]: Started The Apache
HTTP Server.
Nov 02 23:55:23 tcox4.mylabserver.com systemd[1]: Stopping The
Apache HTTP Server...
Nov 02 23:55:24 tcox4.mylabserver.com systemd[1]: Stopped The Apache
HTTP Server.
...ignoring

TASK: [debug var=result]


******************************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"result": {
"changed": true,
"cmd": [
"systemctl",
"status",
"httpd"
],
"delta": "0:00:00.010313",
"end": "2015-11-02 23:55:38.917295",
"invocation": {
"module_args": "systemctl status httpd",
"module_name": "command"
},
"rc": 3,
"start": "2015-11-02 23:55:38.906982",
"stderr": "",
"stdout": "httpd.service - The Apache HTTP Server\n
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)\n
Active: inactive (dead)\n\nNov 02 23:34:25 tcox4.mylabserver.com
systemd[1]: Starting The Apache HTTP Server...\nNov 02 23:34:25
tcox4.mylabserver.com systemd[1]: Started The Apache HTTP
Server.\nNov 02 23:49:27 tcox4.mylabserver.com systemd[1]: Stopping
The Apache HTTP Server...\nNov 02 23:49:28 tcox4.mylabserver.com
systemd[1]: Stopped The Apache HTTP Server.\nNov 02 23:51:52
tcox4.mylabserver.com systemd[1]: Stopped The Apache HTTP
Server.\nNov 02 23:51:52 tcox4.mylabserver.com systemd[1]: Starting
The Apache HTTP Server...\nNov 02 23:51:52 tcox4.mylabserver.com
systemd[1]: Started The Apache HTTP Server.\nNov 02 23:55:23
tcox4.mylabserver.com systemd[1]: Stopping The Apache HTTP
Server...\nNov 02 23:55:24 tcox4.mylabserver.com systemd[1]: Stopped
The Apache HTTP Server.",
"stdout_lines": [
"httpd.service - The Apache HTTP Server",
" Loaded: loaded
(/usr/lib/systemd/system/httpd.service; disabled)",
" Active: inactive (dead)",
"",
"Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]:
Starting The Apache HTTP Server...",
"Nov 02 23:34:25 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server.",
"Nov 02 23:49:27 tcox4.mylabserver.com systemd[1]:
Stopping The Apache HTTP Server...",
"Nov 02 23:49:28 tcox4.mylabserver.com systemd[1]:
Stopped The Apache HTTP Server.",
"Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Stopped The Apache HTTP Server.",
"Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Starting The Apache HTTP Server...",
"Nov 02 23:51:52 tcox4.mylabserver.com systemd[1]:
Started The Apache HTTP Server.",
"Nov 02 23:55:23 tcox4.mylabserver.com systemd[1]:
Stopping The Apache HTTP Server...",
"Nov 02 23:55:24 tcox4.mylabserver.com systemd[1]:
Stopped The Apache HTTP Server."
],
"warnings": []
}
}
}

TASK: [install client software - telnet]


**************************************
changed: [tcox4.mylabserver.com]

TASK: [install client software - lynx]


****************************************
changed: [tcox4.mylabserver.com]

TASK: [log all the packages install on the system]


****************************
changed: [tcox4.mylabserver.com]

TASK: [debug var=installed_result]


********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"installed_result": {
"changed": true,
"cmd": [
"yum",
"list",
"installed"
],
"delta": "0:00:00.333644",
"end": "2015-11-02 23:55:46.460731",
"invocation": {
"module_args": "yum list installed",
"module_name": "command"
},
"rc": 0,
"start": "2015-11-02 23:55:46.127087",
"stderr": "",
"stdout": "Loaded plugins: fastestmirror\nInstalled
Packages\nGConf2.x86_64 3.2.6-
8.el7 @base
\nImageMagick.x86_64 6.7.8.9-10.el7
(((OUTPUT CONCATENATED FOR PACKAGE DISPLAY)))
"yum.noarch 3.4.3-
125.el7.centos @base ",
"yum-metadata-parser.x86_64 1.1.4-
10.el7 installed",
"yum-plugin-fastestmirror.noarch 1.1.31-
29.el7 @base ",
"yum-utils.noarch 1.1.31-
29.el7 @base ",
"zenity.x86_64 3.8.0-
4.el7 @base ",
"zip.x86_64 3.0-
10.el7 installed",
"zlib.x86_64 1.2.7-
13.el7 installed"
],
"warnings": [
"Consider using yum module rather than running yum"
]
}
}
}

TASK: [date/time stamp for when the playbook ends]


****************************
changed: [tcox4.mylabserver.com]

TASK: [debug var=timestamp_end]


***********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"timestamp_end": {
"changed": true,
"cmd": [
"/usr/bin/date"
],
"delta": "0:00:00.002033",
"end": "2015-11-02 23:55:46.652791",
"invocation": {
"module_args": "/usr/bin/date",
"module_name": "command"
},
"rc": 0,
"start": "2015-11-02 23:55:46.650758",
"stderr": "",
"stdout": "Mon Nov 2 23:55:46 UTC 2015",
"stdout_lines": [
"Mon Nov 2 23:55:46 UTC 2015"
],
"warnings": []
}
}
}

NOTIFIED: [Start HTTPD]


*******************************************************
changed: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=12 changed=8 unreachable=0
failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the the 'outline' created in our previous


exercise
[test@tcox3 Outline]$ vim webserver.yml
[test@tcox3 Outline]$ cat webserver.yml
--- # Outline to Playbook Translation
- hosts: apacheweb
user: test
sudo: yes
gather_facts: no
tasks:
- name: date/time stamp for when the playbook starts
command: /usr/bin/date
register: timestamp_start
- debug: var=timestamp_start
- name: install the apache web server
yum: pkg=httpd state=latest
notify: Start HTTPD
- name: verify that the web service is running
command: systemctl status httpd
register: result
ignore_errors: yes
- debug: var=result
- name: install client software - telnet
yum: pkg=telnet state=latest
- name: install client software - lynx
yum: pkg=lynx state=latest
- name: log all the packages install on the system
command: yum list installed
register: installed_result
- debug: var=installed_result
- name: date/time stamp for when the playbook ends
command: /usr/bin/date
register: timestamp_end
- debug: var=timestamp_end
handlers:
- name: Start HTTPD
service: name=httpd state=restarted
4. Run the playbook using the 'dry run' command and display the results
[test@tcox3 Outline]$ ansible-playbook webserver.yml --check

PLAY [apacheweb]
**************************************************************

TASK: [date/time stamp for when the playbook starts]


**************************
skipping: [tcox4.mylabserver.com]
ok: [tcox4.mylabserver.com]

TASK: [debug var=timestamp_start]


*********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"timestamp_start": {
"invocation": {
"module_args": "/usr/bin/date",
"module_name": "command"
},
"msg": "check mode not supported for command",
"skipped": true
}
}
}

TASK: [install the apache web server]


*****************************************
changed: [tcox4.mylabserver.com]

TASK: [verify that the web service is running]


********************************
skipping: [tcox4.mylabserver.com]
ok: [tcox4.mylabserver.com]

TASK: [debug var=result]


******************************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"result": {
"invocation": {
"module_args": "systemctl status httpd",
"module_name": "command"
},
"msg": "check mode not supported for command",
"skipped": true
}
}
}

TASK: [install client software - telnet]


**************************************
changed: [tcox4.mylabserver.com]

TASK: [install client software - lynx]


****************************************
changed: [tcox4.mylabserver.com]

TASK: [log all the packages install on the system]


****************************
skipping: [tcox4.mylabserver.com]
ok: [tcox4.mylabserver.com]

TASK: [debug var=installed_result]


********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"installed_result": {
"invocation": {
"module_args": "yum list installed",
"module_name": "command"
},
"msg": "check mode not supported for command",
"skipped": true
}
}
}

TASK: [date/time stamp for when the playbook ends]


****************************
skipping: [tcox4.mylabserver.com]
ok: [tcox4.mylabserver.com]

TASK: [debug var=timestamp_end]


***********************************************
ok: [tcox4.mylabserver.com] => {
"var": {
"timestamp_end": {
"invocation": {
"module_args": "/usr/bin/date",
"module_name": "command"
},
"msg": "check mode not supported for command",
"skipped": true
}
}
}

NOTIFIED: [Start HTTPD]


*******************************************************
changed: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************

tcox4.mylabserver.com : ok=8 changed=4 unreachable=0


failed=0
1. Verify that your ansible installation is available by displaying the version
of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create an abitrary configuration file called test.conf.j2 that contains:


- references to at least three variables that are passed in at the command
line or from within a playbook
- reference to one variable that can be gathered when remote facts are
obtained
- Display the result.
[test@tcox3 files]$ vim test.conf.j2
[test@tcox3 files]$ cat test.conf.j2
# Configuration for our custom widget
<Connectivity>
ConnectionType {{ connectionType }}
</Connectivity>

<Account Information>
Username {{ userName }}
Password {{ userPassword }}
</Account Information>

<System Information>
DistributionType {{ ansible_os_family }}
</System Information>
NOTE THAT THIS IS ONE EXAMPLE, YOUR EXAMPLE CAN AND
PROBABLY WILL VARY, THIS IS JUST AN EXAMPLE

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'TEMPLATE' concepts from the video:


- Uses local connection
- Logs in to the remote system as 'test' user
- Connects to the localhost
- The playbook runs as 'sudo'
- Sets variables that match those referenced in the previously created
template file (see previous exercise)
- Installs the template configuration file created earlier to the
/home/test/test.conf file, setting user/group ownership to 'test' and the file
permissions 750
[test@tcox3 files]$ vim test.yml
[test@tcox3 files]$ cat test.yml
--- # Testing the J2 Template Module
- hosts: apacheweb:debian
connection: ssh
user: test
sudo: yes
gather_facts: yes
vars:
userName: test
userPassword: password123
connectionType: SFTP
tasks:
- name: Install the configuration file customized for the system
template: src=test.conf.j2 dest=/home/test/test.conf
owner=test group=test mode=750

4. Run the playbook and display the results


[test@tcox3 files]$ ansible-playbook test.yml

PLAY [apacheweb:debian]
*******************************************************

GATHERING FACTS
***************************************************************
ok: [tcox4.mylabserver.com]
ok: [tcox1.mylabserver.com]

TASK: [Install the configuration file customized for the system]


**************
changed: [tcox1.mylabserver.com]
ok: [tcox4.mylabserver.com]
PLAY RECAP
********************************************************************
tcox1.mylabserver.com : ok=2 changed=1 unreachable=0
failed=0
tcox4.mylabserver.com : ok=2 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'START AT' concepts from the video:
- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Uses three separate, named, tasks to install three packages (your choice)
--- # START AT PLAYBOOK EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Install Telnet
yum: pkg=telnet state=latest
- name: Install Lynx
yum: pkg=lynx state=latest
- name: Install at
yum: pkg=at state=latest

4. Run the playbook, starting with the SECOND named task and display
the results.
[test@tcox3 Playbooks]$ ansible-playbook startat.yml --start-
at="Install Lynx"

PLAY [apacheweb]
**************************************************************

TASK: [Install Lynx]


**********************************************************
changed: [tcox4.mylabserver.com]

TASK: [Install at]


************************************************************
changed: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=2 changed=2 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'STEP INTO' concepts from the video:
- Uses SSH
- Logs in to the remote system as 'test' user
- Connects to one server or group from Step #2 above
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Uses three separate, named, tasks to install three packages (your choice)
--- # START AT PLAYBOOK EXAMPLE
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Install Telnet
yum: pkg=telnet state=latest
- name: Install Lynx
yum: pkg=lynx state=latest
- name: Install at
yum: pkg=at state=latest

4. Run the playbook, indicating that you should be prompted for each task
run.
[test@tcox3 Playbooks]$ ansible-playbook startat.yml --step

PLAY [apacheweb]
**************************************************************
Perform task: Install Telnet (y/n/c): y

Perform task: Install Telnet (y/n/c):


****************************************
ok: [tcox4.mylabserver.com]
Perform task: Install Lynx (y/n/c): y

Perform task: Install Lynx (y/n/c):


******************************************
ok: [tcox4.mylabserver.com]
Perform task: Install at (y/n/c): y

Perform task: Install at (y/n/c):


********************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=3 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None

2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'Variables from Command Line' concepts


from the video:
- Uses SSH
- Logs in to the remote system as a VARIABLE user
- Connects to one server or group passed in as a VARIABLE
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Installs a package passed in as a VARIABLE
[test@tcox3 Playbooks]$ vim fromcmdline.yml
[test@tcox3 Playbooks]$ cat fromcmdline.yml
--- # VARIABLES AT A COMMAND LINE PASSING EXAMPLE
- hosts: '{{ hosts }}'
user: '{{ user }}'
sudo: yes
connection: ssh
gather_facts: no
tasks:
- name: Install some software
yum: pkg={{ pkg }} state=latest

4. Run the playbook and display the results


[test@tcox3 Playbooks]$ ansible-playbook fromcmdline.yml --extra-
vars "hosts=apacheweb user=test pkg=telnet"

PLAY [apacheweb]
**************************************************************

TASK: [Install some software]


*************************************************
ok: [tcox4.mylabserver.com]

PLAY RECAP
********************************************************************
tcox4.mylabserver.com : ok=1 changed=0 unreachable=0
failed=0

1. Verify that your ansible installation is available by displaying the version


of ansible while logged in as the 'user' user.
[test@tcox3 ~]$ ansible --version
ansible 1.9.2
configured module search path = None
2. Run the ansible command that lists all of the hosts configured in your
control server 'hosts' file for the system.
[test@tcox3 ~]$ ansible all --list-hosts
tcox5.mylabserver.com
localhost
tcox4.mylabserver.com

3. Create a playbook, using the 'DELEGATE TO' concepts from the video:
- Uses local connection
- Logs in to the remote system as 'test' user
- Connects to the localhost
- The playbook runs as 'sudo'
- Skip gathering remote facts
- Installs the 'Telnet Client' using the appropriate package module
[test@tcox3 Playbooks]$ vim local.yml
[test@tcox3 Playbooks]$ cat local.yml
--- # LOCAL ACTION PLAYBOOK
- hosts: 127.0.0.1
connection: local
tasks:
- name: Install Telnet Client
yum: pkg=telnet state=latest

4. Run the playbook and display the results.


[test@tcox3 Playbooks]$ ansible-playbook local.yml

PLAY [127.0.0.1]
**************************************************************

GATHERING FACTS
***************************************************************
ok: [127.0.0.1]

TASK: [Install Telnet Client]


*************************************************
ok: [127.0.0.1]

PLAY RECAP
********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0
failed=0

Anda mungkin juga menyukai