# Ansible\_inventory.rb -\> Howto define array in context and pass to ansible?

**URL:** <https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257>\
**Category:** Product Support\
**Created:** [November 12, 2017, 7:15pm UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257 "2017-11-12T19:15:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@uli](https://forum.opennebula.io/u/uli)\
**Post date:** [November 12, 2017, 7:15pm UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/1 "2017-11-12T19:15:56Z")

</div>

Hello again,

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

> <https://github.com/jmelis/one-tools/blob/master/ansible_inventory.rb>

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

---

<div class="post-metadata">

**Author:** ![feldsam](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/feldsam/32/4441_2.png) [@feldsam](https://forum.opennebula.io/u/feldsam)\
**Post date:** [November 13, 2017, 8:06am UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/2 "2017-11-13T08:06:33Z")

</div>

Hello, try my [opennebula ansible inventory implementation](https://github.com/FELDSAM-INC/ansible-one-inv)

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

---

<div class="post-metadata">

**Author:** ![uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@uli](https://forum.opennebula.io/u/uli)\
**Post date:** [November 23, 2017, 8:47am UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/3 "2017-11-23T08:47:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![feldsam](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/feldsam/32/4441_2.png) [@feldsam](https://forum.opennebula.io/u/feldsam)\
**Post date:** [November 24, 2017, 9:49am UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/4 "2017-11-24T09:49:55Z")

</div>

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

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![feldsam](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/feldsam/32/4441_2.png) [@feldsam](https://forum.opennebula.io/u/feldsam)\
**Post date:** [November 24, 2017, 9:58am UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/5 "2017-11-24T09:58:53Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@uli](https://forum.opennebula.io/u/uli)\
**Post date:** [November 24, 2017, 11:52am UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/6 "2017-11-24T11:52:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![feldsam](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/feldsam/32/4441_2.png) [@feldsam](https://forum.opennebula.io/u/feldsam)\
**Post date:** [November 26, 2017, 6:18pm UTC](https://forum.opennebula.io/t/ansible-inventory-rb-howto-define-array-in-context-and-pass-to-ansible/5257/7 "2017-11-26T18:18:28Z")

</div>

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

Define custom tags

 ![35](https://canada1.discourse-cdn.com/flex031/uploads/opennebula/original/2X/9/93f90f430757fd692147f28f19f620d9e2408215.png)

Result from one-inv script will be

```auto
{
  "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
