apenella / go-ansible

Go-ansible is a Go package that enables the execution of ansible-playbook or ansible commands directly from Golang applications. It supports a wide range of options for each command, enabling smooth integration of Ansible functionality into your projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to define variable for inventory and extra-vars.??

Psharm89 opened this issue · comments

I am using go func will which pass the hostname and dbname to the ansiblePlaybookOptions but i am trying to use that variable getting below error.

 ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
		Inventory: "%s",host
		ExtraVars: map[string]interface{}{
			"dbname":    "%s",dbname

		},
	}

syntax error: unexpected newline, expecting comma or }

hi, @Psharm89
try this:

 ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
		Inventory: host,
		ExtraVars: map[string]interface{}{
			"dbname": dbname,
		},
	}

But you have to take care with Inventory value. When you run it, ansible-playbook will receive as --inventory option the value of your host var and it should be a value accepted as an ansible inventory.

Take a look at this:

Yes, ansible execution is working fine for me, when i am passing constants value of hostname and dbname.

ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
		Inventory: "38f9d32e88,",         //constant value Hostname
		ExtraVars: map[string]interface{}{
			"dbname":    "test0963",          // constant value DBname
		},
	}

but i am looking for how can i pass variable in inventory or hostname and extra-vars because my hostname and dbname keep changing.

some rough example:

func ansible(host, dbname string) (string, string) {
   ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
		Inventory: host       
		ExtraVars: map[string]interface{}{
			"dbname":   dbname,         
		},
	}
}
func main(){
   go ansible("38f9d32e88", "test0963")
}

getting error no hosts matched.

Ansible=>  PLAY [all] *********************************************************************
Ansible=>  skipping: no hosts matched
Ansible=>
Ansible=>  PLAY RECAP *********************************************************************
Ansible=>

Hi @Psharm89
you missed the comma at the end of the host when you call ansiblefunc.

func main(){
   go ansible("38f9d32e88,", "test0963")
}

Thanks its worked for me.