What is pre_tasks in Ansible?
pre_tasks
is a task which Ansible executes before executing any tasks
mentioned in .yml
file.
Consider this scenario. You provisioned a new instance on Amazon EC2
cloud or Google Cloud
. First thing you need to do is to install OS updates. Then install latest Python, Install Java and so on.
Once all of above pre tasks are done, you need to copy your application and start those applications. It’s very mandatory to install all basic binaries before you copy your application dependencies.
In this tutorial we will go over all details on how to execute pre tasks using Ansible pre_task
tag.

We will follow below scenario in this tutorial:
- create file
crunchify-hosts
file and add an IP on which we will execute pre_task. - create file
crunchify-install-python-java.yml
which is ansible playbook.- pre_task: install python-simplejson
- pre_task: install python-minimal
- pre_task: install system update
- pre_task: install latest JRE
- Get Python version
- Get Java version
- Print all debug results
- run command ansible-playbook -i ./crunchify-hosts crunchify-install-python-java.yml which will perform all our tasks
crunchify-hosts file
[local] localhost ansible_connection=local ansible_python_interpreter=python [crunchify] 13.58.187.197 [crunchify:vars] ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/crunchify/Documents/ansible/crunchify.pem ansible_python_interpreter=/usr/bin/python3
Here as you see i’m using crunchify.pem
file for password less authentication. I can simply connect to my host without any password prompt.
crunchify-install-python-java.yml file
We are using register
keyword in Ansible to register variable. It stores the return value of raw
tasks.
With the help of debug
and stdout_lines
, you can print result on command line.
--- - hosts: crunchify become: yes pre_tasks: - raw: sudo apt-get -y install python-simplejson register: py_simple_output - raw: sudo apt-get -y install python-minimal register: py_minimal_output - raw: sudo apt-get update register: system_output - raw: sudo apt-get install -y default-jre register: java_output tasks: - debug: var=py_simple_output.stdout_lines - debug: var=py_minimal_output.stdout_lines - debug: var=system_output.stdout_lines - debug: var=java_output.stdout_lines - name: get Python version shell: python --version 2>&1 register: py_output - debug: var=py_output.stdout_lines - name: get Java version shell: java --version 2>&1 register: java_output - debug: var=java_output.stdout_lines
Run command:
ansible-playbook -i ./crunchify-hosts crunchify-install-python-java.yml
Ansible Output:
bash1.2 $ ansible-playbook -i ./crunchify-hosts crunchify-install-python-java.yml PLAY [crunchify] *************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************* ok: [13.58.187.197] TASK [raw] ********************************************************************************************************************************************************* changed: [13.58.187.197] TASK [raw] ********************************************************************************************************************************************************* changed: [13.58.187.197] TASK [raw] ********************************************************************************************************************************************************* changed: [13.58.187.197] TASK [raw] ********************************************************************************************************************************************************* changed: [13.58.187.197] TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "py_simple_output.stdout_lines": [ "", "Reading package lists... 0%", "", "Reading package lists... 100%", "", "Reading package lists... Done", "", "", "Building dependency tree... 0%", "", "Building dependency tree... 50%", "", "Building dependency tree ", "", "", "Reading state information... 0%", "", "Reading state information... Done", "", "python-simplejson is already the newest version (3.13.2-1).", "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "py_minimal_output.stdout_lines": [ "", "Reading package lists... 0%", "", "Reading package lists... 100%", "", "Reading package lists... Done", "", "", "Building dependency tree... 0%", "", "Building dependency tree... 50%", "", "Building dependency tree ", "", "", "Reading state information... 0%", "", "Reading state information... Done", "", "python-minimal is already the newest version (2.7.15~rc1-1).", "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "system_output.stdout_lines": [ "", "0% [Working]", " ", "Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic InRelease", "", "0% [Connecting to security.ubuntu.com (91.189.88.162)]", " ", "Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease", "", " ", "Get:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]", "", "0% [Connecting to security.ubuntu.com (91.189.88.162)]", "0% [1 InRelease gpgv 242 kB] [Connecting to security.ubuntu.com (91.189.88.162)", " ", "0% [Connecting to security.ubuntu.com (91.189.88.162)]", "0% [2 InRelease gpgv 88.7 kB] [Connecting to security.ubuntu.com (91.189.88.162", " ", "0% [Waiting for headers]", "0% [3 InRelease gpgv 74.6 kB] [Waiting for headers]", " ", "Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease", "", " ", "0% [3 InRelease gpgv 74.6 kB]", " ", "0% [Working]", "0% [4 InRelease gpgv 88.7 kB]", " ", "100% [Working]", " ", "Fetched 74.6 kB in 0s (249 kB/s)", "", "Reading package lists... 0%", "", "Reading package lists... 5%", "", "Reading package lists... 8%", "", "Reading package lists... 53%", "", "Reading package lists... 79%", "", "Reading package lists... 99%", "", "Reading package lists... Done", "" ] } TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "java_output.stdout_lines": [ "", "Reading package lists... 0%", "", "Reading package lists... 100%", "", "Reading package lists... Done", "", "", "Building dependency tree... 0%", "", "Building dependency tree... 50%", "", "Building dependency tree ", "", "", "Reading state information... 0%", "", "Reading state information... Done", "", "default-jre is already the newest version (2:1.11-68ubuntu1~18.04.1).", "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [get Python version] ****************************************************************************************************************************************** changed: [13.58.187.197] TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "py_output.stdout_lines": [ "Python 2.7.15rc1" ] } TASK [get Java version] ******************************************************************************************************************************************** changed: [13.58.187.197] TASK [debug] ******************************************************************************************************************************************************* ok: [13.58.187.197] => { "java_output.stdout_lines": [ "openjdk 11.0.2 2019-01-15", "OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)", "OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)" ] } PLAY RECAP ********************************************************************************************************************************************************* 13.58.187.197 : ok=13 changed=6 unreachable=0 failed=0
That’s it.
As you see, in this tutorial we have install Python, java and system updates on remote host. Also, returned result back to mac terminal Window.
What’s next?
Try checking out tutorial on How to copy File, Directory or Script from localhost to Remote host.