-
-
Save rsperl/fc8b9127a28615205a9f606a5398b3e4 to your computer and use it in GitHub Desktop.
Ansible Binary Golang Module #snippet
This file contains hidden or 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
go build helloworld.go | |
GOOS=windows GOARCH=amd64 go build helloworld.go |
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
type ModuleArgs struct { | |
Name string | |
} | |
type Response struct { | |
Msg string `json:"msg"` | |
Changed bool `json:"changed"` | |
Failed bool `json:"failed"` | |
} | |
func ExitJson(responseBody Response) { | |
returnResponse(responseBody) | |
} | |
func FailJson(responseBody Response) { | |
responseBody.Failed = true | |
returnResponse(responseBody) | |
} | |
func returnResponse(responseBody Response) { | |
var response []byte | |
var err error | |
response, err = json.Marshal(responseBody) | |
if err != nil { | |
response, _ = json.Marshal(Response{Msg: "Invalid response object"}) | |
} | |
fmt.Println(string(response)) | |
if responseBody.Failed { | |
os.Exit(1) | |
} else { | |
os.Exit(0) | |
} | |
} | |
func main() { | |
var response Response | |
if len(os.Args) != 2 { | |
response.Msg = "No argument file provided" | |
FailJson(response) | |
} | |
argsFile := os.Args[1] | |
text, err := ioutil.ReadFile(argsFile) | |
if err != nil { | |
response.Msg = "Could not read configuration file: " + argsFile | |
FailJson(response) | |
} | |
var moduleArgs ModuleArgs | |
err = json.Unmarshal(text, &moduleArgs) | |
if err != nil { | |
response.Msg = "Configuration file not valid JSON: " + argsFile | |
FailJson(response) | |
} | |
var name string = "World" | |
if moduleArgs.Name != "" { | |
name = moduleArgs.Name | |
} | |
response.Msg = "Hello, " + name + "!" | |
ExitJson(response) | |
} |
This file contains hidden or 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
--- | |
- hosts: all | |
gather_facts: false | |
tasks: | |
- name: ping | |
ping: | |
when: ansible_connection|default('ssh') != 'winrm' | |
- name: win_ping | |
win_ping: | |
when: ansible_connection|default('ssh') == 'winrm' | |
- name: Hello, World! | |
helloworld: | |
- name: Hello, Ansible! | |
helloworld: | |
name: Ansible | |
- name: Async Hello, World! | |
helloworld: | |
async: 1 | |
poll: 1 | |
when: ansible_connection|default('ssh') != 'winrm' | |
- name: Async Hello, Ansible! | |
helloworld: | |
name: Ansible | |
async: 1 | |
poll: 1 | |
when: ansible_connection|default('ssh') != 'winrm' |
This file contains hidden or 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
PLAY *************************************************************************** | |
TASK [ping] ******************************************************************** | |
skipping: [windows] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} | |
ok: [localhost] => {"changed": false, "ping": "pong"} | |
ok: [paramiko] => {"changed": false, "ping": "pong"} | |
ok: [ssh] => {"changed": false, "ping": "pong"} | |
TASK [win_ping] **************************************************************** | |
skipping: [paramiko] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} | |
skipping: [ssh] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} | |
ok: [localhost] => {"changed": false, "ping": "pong"} | |
ok: [windows] => {"changed": false, "ping": "pong"} | |
TASK [Hello, World!] *********************************************************** | |
ok: [localhost] => {"changed": false, "failed": false, "msg": "Hello, World!"} | |
ok: [ssh] => {"changed": false, "failed": false, "msg": "Hello, World!"} | |
ok: [paramiko] => {"changed": false, "failed": false, "msg": "Hello, World!"} | |
ok: [windows] => {"changed": false, "failed": false, "msg": "Hello, World!"} | |
TASK [Hello, Ansible!] ********************************************************* | |
ok: [localhost] => {"changed": false, "failed": false, "msg": "Hello, Ansible!"} | |
ok: [ssh] => {"changed": false, "failed": false, "msg": "Hello, Ansible!"} | |
ok: [paramiko] => {"changed": false, "failed": false, "msg": "Hello, Ansible!"} | |
ok: [windows] => {"changed": false, "failed": false, "msg": "Hello, Ansible!"} | |
TASK [Async Hello, World!] ***************************************************** | |
skipping: [windows] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} | |
ok: [localhost] => {"ansible_job_id": "850503757820.24772", "changed": false, "failed": false, "finished": 1, "msg": "Hello, World!"} | |
ok: [ssh] => {"ansible_job_id": "772391860036.14904", "changed": false, "failed": false, "finished": 1, "msg": "Hello, World!"} | |
ok: [paramiko] => {"ansible_job_id": "579359157443.29225", "changed": false, "failed": false, "finished": 1, "msg": "Hello, World!"} | |
TASK [Async Hello, Ansible!] *************************************************** | |
skipping: [windows] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} | |
ok: [localhost] => {"ansible_job_id": "936293883360.24795", "changed": false, "failed": false, "finished": 1, "msg": "Hello, Ansible!"} | |
ok: [ssh] => {"ansible_job_id": "636937348491.14933", "changed": false, "failed": false, "finished": 1, "msg": "Hello, Ansible!"} | |
ok: [paramiko] => {"ansible_job_id": "200792079727.29345", "changed": false, "failed": false, "finished": 1, "msg": "Hello, Ansible!"} | |
PLAY RECAP ********************************************************************* | |
localhost : ok=5 changed=0 unreachable=0 failed=0 | |
paramiko : ok=5 changed=0 unreachable=0 failed=0 | |
ssh : ok=5 changed=0 unreachable=0 failed=0 | |
windows : ok=3 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment