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


Posted on 26/01/2021

Categories