Ansible_inventory.rb -> Howto define array in context and pass to ansible?

Hello again,

I am using the inventory script from below and I am pretty impressed with its simplicity and functionality.

I am trying to use a playbook for NTP configuration which expects an array “ntp_servers”.
When I define the variable in the playbook as [192.168.1.1] it works fine. If I try to pass the context var “ntp_servers” to the playbook the value is not interpreted as array.

I get a chrony.conf like this
server [ iburst
server " iburst
server 1 iburst
server 9 iburst
server 2 iburst
server . iburst
server 1 iburst
server 6 iburst
server 8 iburst
server . iburst
server 1 iburst
server . iburst
server 1 iburst
server " iburst
server ] iburst

Any help howto to pass the value in the correct way would be very much appreciated.

Thanks
Uli

Hello, try my opennebula ansible inventory implementation

I create host groups using VM labels, so you have to just label your NTP servers in opennebula.

1 Like

Hello Kristian,

thanks. I finally had the chance to try your implementation. It is always good to have a choice.
I get a slightly better result, but still not the expected one.
When defining the ntp_servers = [192.168.1.1,ntp.company.loc] as context variable I end up with an ntp.conf only including the second value from the array:

server ntp.company.loc iburst

Expected result would be:

server 192.168.1.1 iburst
server ntp.company.loc iburst

When I define
ntp_servers: [192.168.1.1, ntp.pupisoft.loc]
within the ansible play I get the expected result.
Do you have an idea how to pass an array/list from OpenNebula to Ansible?

Thanks
Uli

Hello, my implementation works with VM Lables. So you have to “tag” your two running VMs with label “ntp-servers”. My one-inv script will return array like this

{
  "ntp-servers": [
    "192.168.1.1",
    "192.168.1.2"
  ]
}

Also if you have more labels, it create another groups of VMs. You can also skip som especific labels to use for grouping in config.

I also fixed crash when VM with no label found, so you need to update my script to version 1.0.2

When I understand it correctly, you have to define custom context vars and want on provisioned VM to configure right ntp servers. In that case, you should extend context scripts https://github.com/OpenNebula/addon-context-linux.

Another way is to extend my script (I can do that). My script also returns meta data “host vars” which reads from USER_TEMPLATE and ansible have access to that vars. So we can extend host vars generation to parse som einput as array. Also we can parse TEMPLATE/CONTEXT, it depends what you need and if is possible for you to use USER_TEMPATE instead of CONTEXT one

The NTP Server is just one use case where I want to use an array of values and pass it to ansible.
My plan is to use a TEMPLATE/CONTEXT variable, e.g.:
CONTEXT = [
NTP_SERVERS = “[192.168.1.1, ntp.company.loc]”,

“So we can extend host vars generation to parse som einput as array.”
-> I think this is exactly what is missing!

Thanks
uli

Hello, I playing a bit with user template and find following solution.

Define custom tags

Result from one-inv script will be

{
  "GroupName1": [
    "185.174.170.13"
  ],
  "_meta": {
    "hostvars": {
      "185.174.170.13": {
        "DESCRIPTION": "CentOS 7.4 64bit Minimal",
        "HYPERVISOR": "kvm",
        "INPUTS_ORDER": "",
        "LABELS": "GroupName1",
        "LOGO": "images/logos/centos.png",
        "MEMORY_UNIT_COST": "MB",
        "NTP_SERVERS": {
          "SERVER1": "192.168.1.1",
          "SERVER2": "ntp.company.loc"
        },
        "SCHED_DS_REQUIREMENTS": "ID=\"112\" | ID=\"114\" | ID=\"116\"",
        "USER_INPUTS": {
          "CPU": "M|range-float||0.5..6|0.5",
          "MEMORY": "M|range||1024..16384|1024",
          "VCPU": "O|range||1..24|1"
        }
      }
    }
  }
}

I think that you can use instead of SERVER1, SERVER2 just numbers 0, 1