Lab 7 - Solution¶
Mandatory¶
Host Groups¶
- Edit your existing inventory file
hosts - Create the host_group
weband add your webservers - Create the host_group
dband add your dbserver - Run the ping module to test the new host groups
webanddb
# file: hosts
[web]
web1.pascal.lab
web2.pascal.lab
[db]
db.pascal.lab
# should only run on web1 and web2
ansible -m ping web
web1.pascal.lab | SUCCESS => {
"changed": false,
"ping": "pong"
}
web2.pascal.lab | SUCCESS => {
"changed": false,
"ping": "pong"
}
# should only run on db
ansible -m ping db
db.pascal.lab | SUCCESS => {
"changed": false,
"ping": "pong"
}
group_vars¶
- Create the
group_varsdirectory in/home/ansible/training - Create
group_vars/all.yml - add the variable
myvarwith the valuevariable for all servers - Run a task (ad-hoc or in playbook) on all servers to print out the variable
myvarwith the Debug Module
mkdir /home/ansible/training/group_vars
vi /home/ansible/training/group_vars/all.yml
# file: group_vars/all.yml
---
myvar: variable for all servers
ansible -m debug -a "msg='{{ myvar }}'" all
# result
web1.pascal.lab | SUCCESS => {
"msg": "variable for all servers"
}
web2.pascal.lab | SUCCESS => {
"msg": "variable for all servers"
}
db.pascal.lab | SUCCESS => {
"msg": "variable for all servers"
}
- Add the variable
myvarwith the valuevariable for web serversonly for thewebgroup - Run a task (ad-hoc or in playbook) on all servers to print out the variable
myvarwith the Debug Module
# file: group_vars/web.yml
---
myvar: variable for web servers
# web1 and web2 should have the new defined value
ansible -m debug -a "msg='{{ myvar }}'" all
web1.pascal.lab | SUCCESS => {
"msg": "variable for web servers"
}
db.pascal.lab | SUCCESS => {
"msg": "variable for all servers"
}
web2.pascal.lab | SUCCESS => {
"msg": "variable for web servers"
}
host_vars¶
- Create the
host_varsdirectory in/home/ansible/training - Create a host variable file for the server
web1.<firstname>.lab - Add the variable
myvarwith the valuehost variable for web1 - Run a task (ad-hoc or in playbook) on all servers to print out the variable
myvarwith the Debug Module
mkdir /home/ansible/training/host_vars
vi /home/ansible/training/host_vars/web1.<firstname>.lab
# file: host_vars/web1.pascal.lab
---
myvar: host variable for web1
# web1 should have the host_vars value
ansible -m debug -a "msg='{{ myvar }}'" all
web1.pascal.lab | SUCCESS => {
"msg": "host variable for web1"
}
db.pascal.lab | SUCCESS => {
"msg": "variable for all servers"
}
web2.pascal.lab | SUCCESS => {
"msg": "variable for web servers"
}
Role Variables & Loops¶
- Extend your
ntprole - Create a
vars/main.ymlfile - Add list variable
ntp_packageswith the itemsntp&ntpdate - Rewrite your
tasks/main.yml - use the new defined variable
ntp_packagesin combination withwith_itemsto install both packages (see Chapter Variables) - Run the playbook
# file: roles/ntp/vars/main.yml
---
ntp_packages:
- ntp
- ntpdate
# file: roles/ntp/tasks/main.yml
- name: install ntp packages
apt:
name: '{{ item }}'
state: latest
with_items:
- '{{ ntp_packages }}'
tags:
- installation
# run the playbook, ntpdate should be installed
ansible-playbook ntp-role.yml
Role Default Variables¶
- Extend your
ntprole - Create a
defaults/main.ymlfile - Add a list of servers to a new variable
ntp_servers(you can use your example from your lab4.yml)
# file: roles/ntp/defaults/main.yml
---
ntp_servers:
- 0.ch.pool.ntp.org
- 1.ch.pool.ntp.org
- 2.ch.pool.ntp.org
- 3.ch.pool.ntp.org
- Extend the
tasks/main.ymlwith a debug task to print out the defined list variablentp_servers
# file: roles/ntp/tasks/main.yml
- name: debug ntp_servers
debug:
msg: '{{ ntp_servers }}'
- Run the playbook and verify the debug output
# run the playbook
ansible-playbook ntp-role.yml
TASK [ntp : debug ntp_servers] *************************************************
ok: [web1.test.lab] => {
"msg": [
"0.ch.pool.ntp.org",
"1.ch.pool.ntp.org",
"2.ch.pool.ntp.org",
"3.ch.pool.ntp.org"
]
}
Commit your changes¶
- add all files to your git repo and commit it
git status
git add roles/*
git commit -m 'lab 7'