Created
June 18, 2021 09:51
-
-
Save devops-school/0e93142c1fe00594295c53e528333eae to your computer and use it in GitHub Desktop.
Ansible Module Sample Code using Linux Bash script Example 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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