View on GitHub

ansible.training.crafteo.io

Ansible training pages for Crafteo - crafteo.io

Ansible: tasks and modules

Let’s add some tasks to our playbook using Ansible modules. Ansible use modules to perform actions on targets and return values from module execution (such as whether a change was performed and action return values)

Module often used by playbooks:

See Ansible module index for all available modules.

Install a package

Update playbook.yml file to install apache2 package (Apache HTTP server) using apt module with the following parameters:

Run your playbook and, on success, ensure Apache has been installed. Apache service should be running and serving the default page:

Try running the playbook a second time and see what happens.

Manage file content

Let’s override the default index.html of our Apache server by instructing Ansible to manage /var/www/html/index.html, the default Apache index file.

Configure your Playbook to copy the index.html file already existing in our repository on the target node such as:

Run the playbook to apply changes and check on the machine changes were applied (use SSH command to connect to your node: ssh ubuntu@your-name.training.crafteo.io)

Run a command

Ansible can also run commands on targets. Use an appropriate module to run the following command:

ls -al /var/www/html

Important note: using this method should be avoided when possible. Ansible modules running commands are not idempotent most of the time, you are responsible to ensure the command’s idempotency.

If possible, use an Ansible module instead of running a command. For example, to install apache2, you can run command:

sudo apt-get install apache2

However, the apt Ansible module exists for managing packages, the prefered way is then something like:

- name: install Apache2
  apt:
    name: apache2
    state: present