Last active
March 4, 2018 10:19
-
-
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
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
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) | |
} | |
} |
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.
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
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