0

When adding a variable list in Ansible how would one achieve a span of similar values? For instance "000-100" - in an Ansible hosts file this can be done by listing like so, "hostname-[a:v].com". Would this process be the similar in a variable list?

My use case is to provision many VM's within oVirt in a single go without having to make a line by line list.

---
- name: Create VM based on template
  hosts: ovirt-engine
  become: yes
  become_method: sudo

  vars:
  - temp: '{{temp_fedora25}}'
  - iname:
      - db-aa
      - db-ab
      - db-ac

  tasks:

    - name: Giving Birth to lil Baby VM's
      ovirt:
          user: '{{ovirt_usr}}'
          password: '{{ovirt_pass}}'
          url: '{{engine_url}}'
          instance_name: "{{item}}"
          instance_nic: ovirtmgmt
          resource_type: template
          image: '{{temp}}'
          zone: superblade-a
          disk_alloc: preallocated
      with_items: "{{iname}}"
Cœur
  • 37,241
  • 25
  • 195
  • 267
dectral
  • 58
  • 5

2 Answers2

0

You can use sequence lookup:

- name: numeric
  debug:
    msg: "{{ item }}"
  with_sequence: start=1 count=10 format=server-%0d


- name: characters from small 'a'
  debug:
    msg: "{{ item }}"
  with_sequence: start=0x61 count=10 format=server-%c

- name: save for future use
  set_fact:
    my_seq: "{{ lookup('sequence','start={} count={} format={}{}'.format(beg,cnt,pref,fmt),wantlist=True) }}"
  vars:
    beg: 1
    cnt: 10
    pref: host-
    fmt: '%0d'

You can skip set_fact and define my_seq in vars section, but if you use my_seq much, list generation will be done internally every time. With set_fact list is generated once.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Yep, right there in the docs...works well, with some tweaking for my case of course. I am wondering if these values can be reused as registered varibles? Im adding another answer with my full solution just to be thorough. – dectral Jun 30 '17 at 09:17
  • added example with `set_fact`. – Konstantin Suvorov Jun 30 '17 at 09:56
  • Super...does the trick.I'll have to scale up my playbook(s) to test passing those variables around. – dectral Jun 30 '17 at 10:34
0

With respect to the correct answer from Konstantin, I'm adding the full solution as per my case....

My goal is to be able to reuse the sequenced values as registered variables in order to pass the instance name to host name. This works so far but Im sure it can be streamlined by nesting variables perhaps?

---
- name: Create VM based on template
  hosts: ovirt-engine
  become: yes
  become_method: sudo

  vars:
  - temp: '{{temp_fedora25}}'
  - host_pre: db
  - host_seq: a%c
  - host_cnt: 3
  - host_srt: 0x61

  tasks:

    - name: Giving Birth to lil Baby VM's
      ovirt:
         user: '{{ovirt_usr}}'
         password: '{{ovirt_pass}}'
         url: '{{engine_url}}'
         instance_name: "{{item}}"
         instance_nic: ovirtmgmt
         resource_type: template
         image: '{{temp}}'
         zone: superblade-a
         disk_alloc: preallocated
      with_sequence: start="{{host_srt}}" count="{{host_cnt}}" format="{{host_pre}}-{{host_seq}}"
dectral
  • 58
  • 5