Skip to content

Instantly share code, notes, and snippets.

@shirou
Last active March 4, 2018 10:19
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shirou/5444078 to your computer and use it in GitHub Desktop.
Save shirou/5444078 to your computer and use it in GitHub Desktop.
ansible module written in go-lang. This is almost same as http://ansible.cc/docs/moduledev.html#reading-input
package main
import (
"fmt"
"os"
"io/ioutil"
"strings"
"time"
"encoding/json"
)
type Time struct {
Time string `json:"time"` // this will be changed to {"time": string}
}
func main() {
// the format is a little bit wired. see http://golang.org/pkg/time/#pkg-constants
f := "2006-01-02 15:04:06"
data, err:= ioutil.ReadFile(os.Args[1])
if err != nil {panic(err)}
arg := strings.Split(string(data), " ")[0]
if strings.Contains(arg, "="){
f = strings.Split(arg, "=")[1]
}
t := time.Now()
m := Time{t.Format(f)} // create Time struct with string
s, err := json.Marshal(m) // produce JSON from Time struct
if err != nil {
fmt.Println(err)
}else{
fmt.Printf("%s", s)
}
}
@cygnetix
Copy link

cygnetix commented Oct 7, 2015

For the sake of anyone else who may be investigating using golang for writing Ansible modules:
This is a great example but unfortunately compiled modules (go/c/c++ et al) are not currently supported, despite what the documentation says.

See this PR for details: ansible/ansible#6374

@cygnetix
Copy link

For what it's worth, I've found that I can still run custom scripts written in golang using Ansible's script module: http://docs.ansible.com/ansible/script_module.html.

@alex-leonhardt
Copy link

ansible 2.2 has now support for binary modules ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment