Lab 10 - Solution¶
Mandatory¶
Split your inventory¶
Create two inventory files for
testandprodUse the same structure, but only use one web server
- Add
web1.<firstname>.labintoprod - Add
web2.<firstname>.labintotest
- Add
# file: prod
[web]
web1.pascal.lab
[db]
db.pascal.lab
# file: test
[web]
web2.pascal.lab
[db]
db.pascal.lab
- Run a ad-hoc command on the hostgroup
webfor both inventory filestestandprod - Verify, if the task was executed on the right server
# run ping on the group web in the inventory test
ansible -m ping -i test web
# output
web2.pascal.lab | SUCCESS
# run ping on the group web in the inventory prod
ansible -m ping -i prod web
# output
web1.pascal.lab | SUCCESS
Set some variables¶
- In your
testinventory, create a parent group calledtestand add the groupwebanddb - In your
prodinventory, create a parent group calledprodand add the groupwebanddb
# file: test
[test:children]
web
db
# file: prod
[prod:children]
web
db
- Create a variable
envvar: testonly for thetestservers - Create a variable
envvar: prodonly for theprodservers
# file: group_vars/test.yml
---
envvar: test
# file: group_vars/prod.yml
---
envvar: prod
- Create a playbook
env.ymland create a task to debug theenvvarvariable - Run the playbook with both inventories
# file: env.yml
---
- hosts: all
tasks:
- name: debug envvar
debug:
msg: '{{ envvar }}'
# run env.yml on test
ansible-playbook -i test env.yml
# run env.yml on prod
ansible-playbook -i prod env.yml
Change the inventory in ansible.cfg¶
- Make the
testinventory to your default inventory
# file: ansible.cfg
[defaults]
inventory = test
Create a new role¶
Create a new role
flask_appCreate a new group variable
flask_app_versionfortestandprod- value for
prod-> v1.0 - value for
test-> v1.1
- value for
# file: group_vars/test.yml
---
flask_app_version: v1.1
# file: group_vars/prod.yml
---
flask_app_version: v1.0
- Create a role default variable for
flask_app_versionand set it tomaster
# file: flask_app/defaults/main.yml
---
flask_app_version: master
Create a task to checkout the git repo (see Git Module)
- https://github.com/pstauffer/flask-mysql-app.git
- checkout the version defined in the variable
flask_app_version - destination path should be
/opt/flask_app
# file: roles/flask_app/tasks/main.yml
---
- name: checkout flask_app
git:
repo: https://github.com/pstauffer/flask-mysql-app.git
dest: /opt/flask_app
version: '{{ flask_app_version }}'
Create a new playbook
deploy.yml- run only on
webhostgroup - add the
flask_approle
- run only on
# file: deploy.yml
---
- hosts: web
roles:
- flask_app
- Run the playbook on both inventories
testandprod
# run on test
ansible-playbook -i test deploy.yml
# run on prod
ansible-playbook -i prod deploy.yml
- Verify the checkout of the correct version -> Log on the servers, switch into the
/opt/flask_appdirectory and rungit describe --tagsorgit status
ssh web1.<firstname>.lab
cd /opt/flask_app
git describe --tags
v1.0
ssh web2.<firstname>.lab
cd /opt/flask_app
git describe --tags
v1.1
Commit your changes¶
- add all files to your git repo and commit it
git status
git add roles/*
git commit -m 'lab 10'