Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bugcy013/94bc21de04654303556ffc546c48663e to your computer and use it in GitHub Desktop.
Save bugcy013/94bc21de04654303556ffc546c48663e to your computer and use it in GitHub Desktop.
Ansible Module Sample Code using Linux Bash script Example 5
#touch.bash
#!/bin/bash
# import variables from ansible
source $1
state=${state:-present}
if [[ $state == "present" ]]; then
if [ ! -e $file ]; then
touch $file
echo { \"changed\": true }
exit 0
else
echo { \"changed\": false }
exit 0
fi
fi
if [[ $state == "absent" ]]; then
if [ -e $file ]; then
rm $file
echo { \"changed\": true }
exit 0
else
echo { \"changed\": false}
exit 0
fi
fi
# test.yaml
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: touch without state
touch:
file: ./me.txt
register: touch_out
- debug:
var: touch_out
- name: touchw with state (after create)
touch:
file: ./me.txt
state: present
- name: touch with absent
touch:
file: ./me.txt
state: absent
- name: touch absent with no file
touch:
file: ./me.txt
state: absent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment