Lab 6 - Solution """""""""""""""" Mandatory ========= Your first role --------------- * Check out the `copy module `_, you will use it in this lab. * Connect to your ``srv..lab`` as ``ansible`` user * Switch into your git repo ``/home/ansible/training`` * create a ``roles`` directory * Create a ``ntp`` role directory * Create a ``ntp/files`` directory * Copy your ``/etc/ntp.conf`` into this directory -> ``cp /etc/ntp.conf /home/ansible/training/roles/ntp/files/ntp.conf`` * Create a ``ntp`` role with the following tasks in the ``tasks/main.yml``. * installation via ``apt`` module * configuration via ``copy`` module (``src: ntp.conf``) * notify a handler after a config change .. code-block:: bash 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 .. code-block:: bash tree /home/ansible/training/roles/ . └── ntp ├── files │   └── ntp.conf ├── handlers │   └── main.yml └── tasks └── main.yml .. code-block:: yaml # 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..lab`` with the new role * Run the playbook .. code-block:: yaml # file: ntp-role.yml --- - hosts: web1.pascal.lab roles: - ntp .. code-block:: bash ansible-playbook ntp-role.yml Add Tags to the ntp role ------------------------ * Tag the configuration task with ``configuration`` * Tag the installation task with ``installation`` .. code-block:: yaml # 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 .. code-block:: bash 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-pip`` role * use the ``package`` module for the installation of the package ``python-pip`` .. code-block:: yaml # 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-pip`` role in the already existing ``ntp-role.yml`` playbook .. code-block:: yaml # file: ntp-role.yml --- - hosts: web1.pascal.lab roles: - ntp - python-pip Run playbook with tag --------------------- Run only the tag ``installation`` .. code-block:: bash ansible-playbook -t installation ntp-role.yml Commit your changes ------------------- * add all files to your git repo and commit it .. code-block:: bash 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'