Skip to content

Instantly share code, notes, and snippets.

@goldyfruit
Forked from sivel/go-build.sh
Created April 30, 2020 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldyfruit/052b4c77a5dfb366fe0d82928750b86b to your computer and use it in GitHub Desktop.
Save goldyfruit/052b4c77a5dfb366fe0d82928750b86b to your computer and use it in GitHub Desktop.
Ansible Binary Golang Module
go build helloworld.go
GOOS=windows GOARCH=amd64 go build helloworld.go
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)
}
---
- 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'
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