Lab 7 - Solution """""""""""""""" Mandatory ========= Host Groups ----------- * Edit your existing inventory file ``hosts`` * Create the host_group ``web`` and add your webservers * Create the host_group ``db`` and add your dbserver * Run the ping module to test the new host groups ``web`` and ``db`` .. code-block:: yaml # file: hosts [web] web1.pascal.lab web2.pascal.lab [db] db.pascal.lab .. code-block:: bash # 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_vars`` directory in ``/home/ansible/training`` * Create ``group_vars/all.yml`` * add the variable ``myvar`` with the value ``variable for all servers`` * Run a task (ad-hoc or in playbook) on **all** servers to print out the variable ``myvar`` with the `Debug Module `_ .. code-block:: bash mkdir /home/ansible/training/group_vars vi /home/ansible/training/group_vars/all.yml .. code-block:: yaml # file: group_vars/all.yml --- myvar: variable for all servers .. code-block:: bash 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 ``myvar`` with the value ``variable for web servers`` only for the ``web`` group * Run a task (ad-hoc or in playbook) on **all** servers to print out the variable ``myvar`` with the `Debug Module `_ .. code-block:: yaml # file: group_vars/web.yml --- myvar: variable for web servers .. code-block:: bash # 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_vars`` directory in ``/home/ansible/training`` * Create a host variable file for the server ``web1..lab`` * Add the variable ``myvar`` with the value ``host variable for web1`` * Run a task (ad-hoc or in playbook) on **all** servers to print out the variable ``myvar`` with the `Debug Module `_ .. code-block:: bash mkdir /home/ansible/training/host_vars vi /home/ansible/training/host_vars/web1..lab .. code-block:: yaml # file: host_vars/web1.pascal.lab --- myvar: host variable for web1 .. code-block:: bash # 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 ``ntp`` role * Create a ``vars/main.yml`` file * Add list variable ``ntp_packages`` with the items ``ntp`` & ``ntpdate`` * Rewrite your ``tasks/main.yml`` * use the new defined variable ``ntp_packages`` in combination with ``with_items`` to install both packages (see Chapter Variables) * Run the playbook .. code-block:: yaml # 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 ``ntp`` role * Create a ``defaults/main.yml`` file * Add a list of servers to a new variable ``ntp_servers`` (you can use your example from your **lab4.yml**) .. code-block:: yaml # 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.yml`` with a debug task to print out the defined list variable ``ntp_servers`` .. code-block:: yaml # file: roles/ntp/tasks/main.yml - name: debug ntp_servers debug: msg: '{{ ntp_servers }}' * Run the playbook and verify the debug output .. code-block:: bash # 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 .. code-block:: bash git status git add roles/* git commit -m 'lab 7'