2017-06-29 74 views
0

在Ansible中添加變量列表時,如何獲得一個相似值的跨度?例如「000-100」 - 在一個Ansible主機文件中,這可以通過列舉如「hostname- [a:v] .com」來完成。這個過程是否與變量列表中的類似?Ansible變量列表範圍

我的用例是一次性提供oVirt中的許多虛擬機,而不必逐行列表。

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

回答

0

您可以使用sequence查找:

- 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' 

你可以跳過set_fact和瓦爾部分定義my_seq,但如果你使用my_seq多,列表生成將在內部每次完成。用set_fact列表生成一次。

+0

是的,就在那裏的文檔......效果很好,當然還有一些調整我的情況。我想知道這些值是否可以作爲註冊變量重用?我用我的完整解決方案添加另一個答案只是爲了徹底。 – arkitoure

+0

用'set_fact'添加了一個例子。 –

+0

超級...訣竅。我將不得不放大我的劇本來測試通過這些變量。 – arkitoure

0

對於從康斯坦丁正確的答案,我加入了完整的解決方案按我的情況....

我的目標是能夠重複使用的測序值作爲註冊的變量爲了通過實例名稱爲主機名。這工作到目前爲止,但我確定它可以通過嵌套變量簡化?

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