variablesansibleovirt

Ansible variable list span


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}}"

Solution

  • 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.