Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Created January 4, 2021 22:53
Show Gist options
  • Save zeitounator/c40d42bbf3b57f6b87f4d1ac51cb3bac to your computer and use it in GitHub Desktop.
Save zeitounator/c40d42bbf3b57f6b87f4d1ac51cb3bac to your computer and use it in GitHub Desktop.
---
all:
vars:
ansible_python_interpreter: auto
hosts:
test:
ansible_connection: docker
---
- hosts: all
gather_facts: false
tasks:
- name: Run a local command
command: "{{ playbook_dir }}/test_command {{ exit_value | default(0) }}"
delegate_to: localhost
register: test_command
changed_when: false
failed_when: test_command.rc not in [0,1]
- name: Debug local command
debug:
var: test_command.stdout
- name: Do something
command: "echo do something"
when: test_command.rc == 0
# make sure all files are ok
$ ls -l
total 12
-rw-r--r-- 1 user users 57 janv. 4 23:25 hosts.yml
-rw-r--r-- 1 user users 332 janv. 4 23:36 playbook.yml
-rwxr--r-- 1 user users 47 janv. 4 23:29 test_command
# Launch our test docker container target
$ docker run -d --rm --name test python:latest tail -f /dev/null
# Note: I changed the logic so the local task does not fail
# when I get an expected RC. But that does not change the overall demo.
# Launch playbook with "suceeding" script
$ ansible-playbook -i hosts.yml playbook.yml
PLAY [all] *****************************************************************************************************************************************************************************************************************************
TASK [Run a local command] *************************************************************************************************************************************************************************************************************
ok: [test]
TASK [Debug local command] *************************************************************************************************************************************************************************************************************
ok: [test] => {
"test_command.stdout": "Argument passed is 0"
}
TASK [Do something] ********************************************************************************************************************************************************************************************************************
changed: [test]
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
test : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# Launch play book with "failing" script
$ ansible-playbook -i hosts.yml playbook.yml -e exit_value=1
PLAY [all] *****************************************************************************************************************************************************************************************************************************
TASK [Run a local command] *************************************************************************************************************************************************************************************************************
ok: [test]
TASK [Debug local command] *************************************************************************************************************************************************************************************************************
ok: [test] => {
"test_command.stdout": "Argument passed is 1"
}
TASK [Do something] ********************************************************************************************************************************************************************************************************************
skipping: [test]
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
test : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
#!/bin/bash
echo Argument passed is $1
exit $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment