diff --git a/aws/ssm/ansible.go b/aws/ssm/ansible.go index e39f723..5bd2f39 100644 --- a/aws/ssm/ansible.go +++ b/aws/ssm/ansible.go @@ -1,7 +1,49 @@ package ssm +import ( + "github.com/aws/aws-sdk-go/aws" + assm "github.com/aws/aws-sdk-go/service/ssm" + "os" +) + func (s ssm) RunAnsible() error { s.log.Info("Running ssm ansible command") - // TODO: implement + + command, err := s.cl.SendCommand(&assm.SendCommandInput{ + DocumentName: aws.String("AWS-RunAnsiblePlaybook"), + DocumentVersion: aws.String("$LATEST"), + InstanceIds: s.provideInstanceIDs(), + Parameters: s.provideAnsibleCommands(), + TimeoutSeconds: &s.conf.CommandExecMaxWait, + }) + if err != nil { + return err + } + + s.log.Info("Command deployed successfully") + s.log.Info("Waiting for results") return nil } + +func (s ssm) provideAnsibleCommands() map[string][]*string { + var resp = map[string][]*string{} + checkStr := "False" + + playbookStr, err := os.ReadFile(s.conf.AnsiblePlaybook) + if err != nil { + s.log.Fatalln("Could not read ansible playbook", "err", err.Error()) + } + playbook := string(playbookStr) + resp["playbook"] = []*string{&playbook} + + if s.conf.AnsibleDryRun { + checkStr = "True" + } + resp["check"] = []*string{&checkStr} + + // TODO: implement "ploybookurl" and "extravars" + resp["playbookurl"] = []*string{} + resp["extravars"] = []*string{} + + return resp +} diff --git a/conf/conf.go b/conf/conf.go index 2f88399..b88d6df 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -21,6 +21,7 @@ type Config struct { BashFile string AnsiblePlaybook string + AnsibleDryRun bool AWSProfile string AWSRegion string @@ -56,6 +57,7 @@ func DefaultConfig() Config { AWSInstanceTags: "", CommandResultMaxWait: 30, CommandExecMaxWait: 300, + AnsibleDryRun: false, } } @@ -71,6 +73,7 @@ func (c *Config) processFlags() { flag.StringVar(&c.AWSInstanceTags, "tags", c.AWSInstanceTags, "comma delimited list of ec2 tags") flag.IntVar(&c.CommandResultMaxWait, "max-wait", c.CommandResultMaxWait, "maximum wait time in seconds for command execution") flag.Int64Var(&c.CommandExecMaxWait, "max-exec", c.CommandExecMaxWait, "maximum command execution time in seconds") + flag.BoolVar(&c.AnsibleDryRun, "dry-run", c.AnsibleDryRun, "run ansible in dry-run mode") flag.Parse() }