Skip to content

Instantly share code, notes, and snippets.

@devops-school
Created June 18, 2021 09:51
Show Gist options
  • Select an option

  • Save devops-school/0e93142c1fe00594295c53e528333eae to your computer and use it in GitHub Desktop.

Select an option

Save devops-school/0e93142c1fe00594295c53e528333eae to your computer and use it in GitHub Desktop.

Revisions

  1. devops-school created this gist Jun 18, 2021.
    27 changes: 27 additions & 0 deletions Ansible-module-sample-bash.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #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
    25 changes: 25 additions & 0 deletions Ansible-module-sample-bash.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # 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