In this lab you will:
apt
Ansible
as part of systems management.In this lab, we will have one server computer, running Ansible, and 5 identical workstation computers. We will use the docker-compose scale
option to create 5 containers from the same workstation image. The IP Addresses of the workstations and server are included in the diagram for reference.
+-------------+
| workstations|----+
| 172.44.5.2 | |
| through | |
| 172.44.5.6 | |
+-------------+ |
| +---------------+ ++++++++++++++
+---| host computer |-----| Internet |
| +---------------+ ++++++++++++++
+--------------+ |
| server |---+
| 172.44.5.254 |
+--------------+
This lab prep’s a little different as we will be scaling the workstation image to use 5 containers.
ist346-labs
PS > cd ist346-labs
PS ist346-labs> git pull origin master
lab-E
folder:PS ist346-labs> cd lab-E
PS ist346-labs\lab-E> docker-compose up -d --scale workstation=5
PS ist346-labs\lab-E> docker-compose ps
The apt
packaging system allows a user to manage software packages on a Linux system. Apt is a wrapper around the dpkg
packaging system which is used for managing software on Debian Linux based distributions such as Ubuntu Linux.
Let’s issue some commands to see how the apt
package system works.
server
container:PS ist346-labs\lab-E> docker-compose exec server bash
root@server:/#
root@server:/# apt-get update
This updates the list of packages from the available repositories.nethack-console
which is a text-based dungeon crawl game. To install, type:root@server:/# apt-get install -y nethack-console
-y
will confirm the installation so that you don’t have to type Y
to continue.root@server:/# nethack-console
bash: nethack-console: command not found
message. We know it was installed so where is it?dpkg
utility for this:root@server:/# dpkg -L nethack-console
-L
all of the files in the nethack-console
package./usr/games/nethack-console
we know this because by convention Linux installs games to /usr/games
.root@server:/# dpkg -L /usr/games/nethack-console
Who are you?
CTRL
+ c
to exit the game. (Hey, play games on your own time! hehe)root@server:/# apt list --installed
grep
. For example, let’s look for all packages with net
in them:root@server:/# apt list --installed | grep "net"
root@server:/# apt show nethack-console
Description: dungeon crawl game - text-based interface
root@server:/# apt list
less
so we can scroll through the output with our arrow keys.root@server:/# apt list | less
less
command is not found?apt
!!!: root@server:/# apt-get install -y less
less
:root@server:/# apt list | less
alpine
then press q
to quit less
.alpine
, try:root@server:/# apt show alpine
root@server:/# apt-get remove nethack-console
Okay we learned how to install packages on Linux systems, but how would you do this on 200 Linux systems without placing your hands on 200 keyboards? Read on to find out!
In this next part, we will use Ansible
to manage the 5 workstations on our network. What is Ansible? Simply put, it is a systems management automation engine. It allows you to easily perform tasks on remote computers such as changing configuration files, installing software and running programs.
I highly recommend watching this 3 1/2 minute video overview of ansible, from Lynda.com
https://www.lynda.com/Ansible-tutorials/introduction-Ansible/555799/598693-4.html?org=syracuse.edu
NOTE: You will need to log-in with your NetID and Password.
To make the lab run smoothly we’ve setup most of ansible for you. Typically to do this you will need:
server
)workstation
has that configured for you in this lab). As usual, the password to ssh into the hosts is IST346
To help you fully understand the power and flexibility of Ansible we will pretend our 5 workstations are divided up amongst 2 departments:
it
departmentsales
departmentWe can configure this through Ansible’s hosts
file located at /etc/ansible/hosts
, let’s do this now:
server
, type:PS ist346-labs\lab-E> docker-compose exec server bash
root@server:/#
from this prompt, let’s edit the Ansible hosts file:root@server:/# nano /etc/ansible/hosts
nano
text editor. Add the following lines to the bottom of the file:[it]
lab-e_workstation_[1:3]
[sales]
lab-e_workstation_[4:5]
When you are finished editing the file press CTRL
+ x
and when asked to save modified buffer press y
, and press ENTER
to keep the name file name.
Let’s test our setup by pinging machines:
sales
department ping the ischool.syr.edu
server two times.root@server:/# ansible sales -k -m shell -a 'ping -c 2 ischool.syr.edu'
root
password for the workstations, as you may recall, its IST346
. There is a way to execute these commands without the password, but that’s for another lab and another time. ;-)lab-e_workstation_5 | SUCCESS | rc=0 >>
lab-e_workstation_4 | SUCCESS | rc=0 >>
ansible
command:-k
prompts for the root password (root because that is who we are currently logged in as)-m shell
uses the shell module. Ansible has many modules to perform a variety of tasks.-a
allows us to specify the specific module arguments. This case, the ping
command.Ansible includes a ping module. This is not the same as the ping
command. This module verifies that the host is capable of being managed by Ansible.
root@server:/# ansible all -k -m ping
IST346
pong
letting you know that it can respond to ansible commands.Combining what we learned in the previous part of the lab, you’d probably figure you can use the shell
module to install software with ansible.
mc
on the sales
computers, type:
root@server:/# ansible sales -k -m shell -a 'apt-get install -y mc'
root@server:/# ansible sales -k -m shell -a 'apt-get install -y mc'
One advantage of Ansible modules is they ensure idempotence - that we can run the same tasks again and again without changing the final results. This is so important in systems management where you are often changing the files on a computer or the contents of a single file.
Let’s see this in action.
apt
module:root@server:/# ansible sales -k -m apt -a 'pkg=mc state=present update_cache=yes'
state=present
requests that the pkg=mc
be installed on the workstation.update_cache=yes
requests that an apt-get update
be performed before the install (should the install need to take place to begin with)"changed": false
to indicate that no action was taken.all
workstations:root@server:/# ansible all -k -m apt -a 'pkg=mc state=present update_cache=yes'
"changed": false
for workstations 4 and 5 (sales department), but "changed": true
for workstations 1 and 3 (the it department). Idempotency!This command line stuff is great, but what if the single “change” you need to make requires several steps? We could just issue each Ansible sequentially but I’m sure there’s a better way, correct? Well, this is the purpose of an Ansible Playbook. The playbook is a file which can run multiple Ansible tasks in addition to providing some additional configuration common across all the commands.
For example let’s assume we need to run a Ruby program on all the computers in the it
department. We need to install the ruby programming language to run the program, but we don’t want to leave it on the system after the program runs (let’s say for security purposes.)
Our playbook would look something like this:
- hosts: it
tasks:
- name: Install Ruby
apt:
name : ruby
state: present
update_cache: yes
- name: Run The Ruby Program
shell: "ruby -e 'puts \"hello from ruby\"'"
- name: Uninstall Ruby
apt:
name: ruby
state: absent
update_cache: yes
Let’s use this playbook.
root@server:/# curl -o ruby.yml -L https://raw.githubusercontent.com/mafudge/ist346-labs/master/lab-E/ruby.yml
ruby.yml
file from github to your server
container.root@server:/# cat ruby.yml
root@server:/# ansible-playbook -k ruby.yml
PLAY [it] **********************************************
TASK [Gathering Facts] **********************************************
ok: [lab-e_workstation_1]
ok: [lab-e_workstation_2]
ok: [lab-e_workstation_3]
TASK [Install Ruby] **********************************************
changed: [lab-e_workstation_2]
changed: [lab-e_workstation_3]
changed: [lab-e_workstation_1]
TASK [Run The Ruby Program] **********************************************
changed: [lab-e_workstation_2]
changed: [lab-e_workstation_3]
changed: [lab-e_workstation_1]
TASK [Uninstall Ruby] **********************************************
changed: [lab-e_workstation_1]
changed: [lab-e_workstation_2]
changed: [lab-e_workstation_3]
PLAY RECAP **********************************************
lab-e_workstation_1 : ok=4 changed=3 unreachable=0 failed=0
lab-e_workstation_2 : ok=4 changed=3 unreachable=0 failed=0
lab-e_workstation_3 : ok=4 changed=3 unreachable=0 failed=0
This concludes our lab. Time for a tear down!
root@server:/#
prompt, type:root@server:/# exit
PS ist346-labs\lab-E> docker-compose down
apt-get update
?bar
?chicken
?apt
to install peachtree
onto computers in the accounting
department (an Ansible hosts label).