Lab 6 - Solution¶
Mandatory¶
Your first role¶
Check out the copy module, you will use it in this lab.
Connect to your
srv.<firstname>.labasansibleuserSwitch into your git repo
/home/ansible/trainingcreate a
rolesdirectoryCreate a
ntprole directoryCreate a
ntp/filesdirectoryCopy your
/etc/ntp.confinto this directory ->cp /etc/ntp.conf /home/ansible/training/roles/ntp/files/ntp.confCreate a
ntprole with the following tasks in thetasks/main.yml.- installation via
aptmodule - configuration via
copymodule (src: ntp.conf) - notify a handler after a config change
- installation via
mkdir /home/ansible/training/roles
mkdir -p /home/ansible/training/roles/ntp/tasks
mkdir /home/ansible/training/roles/ntp/handlers
mkdir /home/ansible/training/roles/ntp/files
vi /home/ansible/training/roles/ntp/tasks/main.yml
vi /home/ansible/training/roles/ntp/handlers/main.yml
cp /etc/ntp.conf /home/ansible/training/roles/ntp/files/ntp.conf
tree /home/ansible/training/roles/
.
└── ntp
├── files
│ └── ntp.conf
├── handlers
│ └── main.yml
└── tasks
└── main.yml
# file: roles/ntp/tasks/main.yml
---
- name: install ntp
apt:
name: ntp
state: latest
- name: configure ntp
copy:
src: ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: 0644
notify: restart ntp
# file: roles/ntp/handlers/main.yml
---
- name: restart ntp
service:
name: ntp
state: restarted
Playbook with a role¶
- Create a new playbook named
ntp-role.yml - Link
web1.<firstname>.labwith the new role - Run the playbook
# file: ntp-role.yml
---
- hosts: web1.pascal.lab
roles:
- ntp
ansible-playbook ntp-role.yml
Add Tags to the ntp role¶
- Tag the configuration task with
configuration - Tag the installation task with
installation
# file: roles/ntp/tasks/main.yml
---
- name: install ntp
apt:
name: ntp
state: latest
tags:
- installation
- name: configure ntp
copy:
src: ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: 0644
notify: restart ntp
tags:
- configuration
Test the handler¶
- change the ntp config
files/ntp.conf - Run the playbook again -> service should be restarted
echo "# comment" >> roles/ntp/files/ntp.conf
RUNNING HANDLER [ntp : restart ntp] ********************************************
changed: [web1.pascal.lab]
Create a python-pip role¶
- Create a new
python-piprole - use the
packagemodule for the installation of the packagepython-pip
# file: roles/python-pip/tasks/main.yml
---
- name: install python-pip
package:
name: python-pip
state: latest
tags:
- installation
- extend the role list with the
python-piprole in the already existingntp-role.ymlplaybook
# file: ntp-role.yml
---
- hosts: web1.pascal.lab
roles:
- ntp
- python-pip
Commit your changes¶
- add all files to your git repo and commit it
git status
git add roles
git add ntp-role.yml
git add roles/.
git commit -m 'added ntp and python-pip role, included in playbook'