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..lab`` into ``prod`` * Add ``web2..lab`` into ``test`` .. code-block:: yaml # file: prod [web] web1.pascal.lab [db] db.pascal.lab .. code-block:: yaml # 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 .. code-block:: bash # 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`` .. code-block:: yaml # file: test [test:children] web db .. code-block:: yaml # 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 .. code-block:: yaml # file: group_vars/test.yml --- envvar: test .. code-block:: yaml # 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 .. code-block:: bash # 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 .. code-block:: bash # 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 .. code-block:: yaml # file: group_vars/test.yml --- flask_app_version: v1.1 .. code-block:: yaml # file: group_vars/prod.yml --- flask_app_version: v1.0 * Create a role default variable for ``flask_app_version`` and set it to ``master`` .. code-block:: yaml # 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`` .. code-block:: yaml # 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 .. code-block:: yaml # file: deploy.yml --- - hosts: web roles: - flask_app * Run the playbook on both inventories ``test`` and ``prod`` .. code-block:: bash # 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`` .. code-block:: bash ssh web1..lab cd /opt/flask_app git describe --tags v1.0 ssh web2..lab cd /opt/flask_app git describe --tags v1.1 Commit your changes ------------------- * add all files to your git repo and commit it .. code-block:: bash git status git add roles/* git commit -m 'lab 10'