0

As mentioned here, If you are using a cloud provider, you should not be managing your inventory in a static file. Instead use dynamic inventory


Ansible documentation only gives python boto sdk as dyamic inventories, as shown here.

     ansible -i ec2.py -u ubuntu us-east-1d -m ping

Does ansible allow(-i) executing dynamic inventories written using AWS Go sdk? instead of python boto sdk.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

1

Yes, ansible will use any command that can produce the necessary JSON output, including just a shell script, as specified by the fine manual:

In previous versions you had to create a script or program that can output JSON in the correct format when invoked with the proper arguments. You can still use and write inventory scripts, as we ensured backwards compatibility via the script inventory plugin and there is no restriction on the programming language used.

as a concrete golang example:

package main
import (
    "encoding/json"
    "fmt"
)
func main() {
    i := map[string]interface{}{
        "_meta": map[string]interface{}{
            "hostvars": map[string]interface{}{
                "example.host": map[string]interface{}{
                    "ansible_host": "127.0.0.1",
                    "ansible_user": "ubuntu",
                },
            },
        },
        "all": map[string]interface{}{
            "children": []string{"ungrouped"},
        },
        "ungrouped": map[string]interface{}{
            "hosts": []string{"example.host"},
        },
    }
    ba, err := json.Marshal(i)
    if err != nil { panic(err) }
    fmt.Println(string(ba))
}

Invoked via the usual mechanism:

go build -o sample-inv ./main.go
ansible -i ./sample-inv -m ping all
mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • Can you refer any example of dynamic inventory with GoLang? – overexchange Mar 06 '20 at 15:53
  • I am guessing from your comment that you didn't even **attempt** something on your own, but I guess that's StackOverflow's default position now, so there ya go – mdaniel Mar 08 '20 at 05:28
  • Can you also comment on this? https://stackoverflow.com/q/60569134/3317808 – overexchange Mar 08 '20 at 05:35
  • It looks like it already has an answer and also that **you commented** a repo on your own question that contains an implementation of a working module in golang -- what is going on with your questions? is this a psychology experiment? – mdaniel Mar 08 '20 at 17:17
  • I think you misunderstood... Please see my second question, about plays that are remote execution type – overexchange Mar 08 '20 at 18:23
  • Did you even try the project you linked to? that's exactly what it does (I even went to the trouble of trying it locally before replying). I wish you good luck with your journey – mdaniel Mar 10 '20 at 16:01
  • if you see the playbook code, it says, `hosts: localhost`. So, when I say remote execution type, I meant running the module on remote host. – overexchange Mar 10 '20 at 17:01