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

Is their a mechanism for hostvars?

rhugga opened this issue · comments

Im building my inventory dynamically, but I want to use host variables, ie assume the following traditional inventory:

Can this library implement host variables that would have the same effect as this inventory file?

all:
  vars:
    new_kernel_version: kernel-3.10.0-1160.36.2.el7.x86_64
  children:
    cluster:
      hosts:
        host1:
          ansible_host: host1.example.com
          rack: rack1
          platform: xyz

Hi @rhugga
Thanks for the question!

go-ansible executes ansible or ansible-playbook commands on your behalf, then if you can run it using a command the library should accept it.
In case I missed any option on the ansible cli, just tell me.

Thanks!

This isn't really a command line option but the behavior of ansible. For example, this host file sets host level variables:

all:
  vars:
    new_kernel_version: kernel-3.10.0-1160.36.2.el7.x86_64
  children:
    cluster:
      hosts:
        host1:
          ansible_host: host1.example.com
          rack: rack1
          platform: xyz
        host1:
          ansible_host: host2.example.com
          rack: rack2
          platform: xyz

In my code I can reference hostvars[inventory_hostname]['rack'] based on the above inventory file and get the rack for each host.

new_kernel_version is a global variable and I can set that using ExtraVars: map[string]interface{}.

I guess the better question is how can I "inject" variables into the host's variable scope that I could then refer to in playbooks.

hi! @rhugga
go-ansible does not offer extra capabilities over ansible commands, and you need to define variables as ansible does.
You can inject variables into the host's variable is using set_fact, and even you can combine it with hostvars[inventory_hostname] or `groups[my_group] but I don't know if it responds your question.

Thanks!

Hi @rhugga
regarding that issue, do you need anything else?

Thanks!