Ansible: Test a task by applying it locally

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

- hosts:
  - name: mkdir /tmp/test
      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:

After these alines we can just paste the tasks we want to run (correctly indented) and we are ready to to: We just need to call ansible-playbook with thise 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 [] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: []

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: []

PLAY RECAP ********************************************************************************************************************************************************************************************************                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Posted on 26/01/2021