Ansible: Test a task by applying it locally

2 min read | by Jordi Prats

If you need to test a set of ansible tasks without having to go into much trouble, you can use ansible-playbook to do a quick local test. To do so you just need ansible installed and create a yaml file with the playbook to execute using the following format:

--- - hosts: 127.0.0.1 tasks: - name: mkdir /tmp/test command: cmd: mkdir -p /tmp/test 

The only difference with what we would be using on a role is the first two lines where we tell ansible to use the implicit localhost:

- hosts: 127.0.0.1 tasks: 

After these lines we can just paste the tasks we want to run (we will have to fix identation) and we are all set: We just need to instruct ansible-playbook to use this file:

$ ansible-playbook test.yaml  [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [127.0.0.1] ************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************************************************************************************** ok: [127.0.0.1] TASK [mkdir /tmp/test] ******************************************************************************************************************************************************************************************** [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [127.0.0.1] PLAY RECAP ******************************************************************************************************************************************************************************************************** 127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0  

The most relevant sections are TASK and PLAY RECAP:

  • On the TASK section we can see the task execution outcome
  • On the PLAY RECAP section we can find the number of tasks that have finished with each of the possible states

Posted on 26/01/2021

Categories