Lab 10 - Solution

Mandatory

Split your inventory

  • Create two inventory files for test and prod

  • Use the same structure, but only use one web server

    • Add web1.<firstname>.lab into prod
    • Add web2.<firstname>.lab into test
# 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 web for both inventory files test and prod
  • 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 test inventory, create a parent group called test and add the group web and db
  • In your prod inventory, create a parent group called prod and add the group web and db
# file: test
[test:children]
web
db
# file: prod
[prod:children]
web
db
  • Create a variable envvar: test only for the test servers
  • Create a variable envvar: prod only for the prod servers
# file: group_vars/test.yml
---
envvar: test
# file: group_vars/prod.yml
---
envvar: prod
  • Create a playbook env.yml and create a task to debug the envvar variable
  • 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 test inventory to your default inventory
# file: ansible.cfg
[defaults]
inventory = test

Create a new role

  • Create a new role flask_app

  • Create a new group variable flask_app_version for test and prod

    • value for prod -> v1.0
    • value for test -> v1.1
# 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_version and set it to master
# file: flask_app/defaults/main.yml
---
flask_app_version: master
# 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 web hostgroup
    • add the flask_app role
# file: deploy.yml
---
- hosts: web
  roles:
    - flask_app
  • Run the playbook on both inventories test and prod
# 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_app directory and run git describe --tags or git 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'