6

My intention is to generate a list containing numbers from 1 to 22 and for that I wrote below Ansible script.

- hosts: localhost
  gather_facts: no
  tasks:
    - name: Generate sequance
      set_fact:
        mysequence: "{{ item | list }}"
      with_sequence: start=1 end=22

    - debug: var=mysequence

When I run the code I get this weird result

$ ansible-playbook test.yml

PLAY [localhost] **************************************************************************************************************************

TASK [Generate sequance] ******************************************************************************************************************
ok: [localhost] => (item=1)
ok: [localhost] => (item=2)
ok: [localhost] => (item=3)
ok: [localhost] => (item=4)
ok: [localhost] => (item=5)
ok: [localhost] => (item=6)
ok: [localhost] => (item=7)
ok: [localhost] => (item=8)
ok: [localhost] => (item=9)
ok: [localhost] => (item=10)
ok: [localhost] => (item=11)
ok: [localhost] => (item=12)
ok: [localhost] => (item=13)
ok: [localhost] => (item=14)
ok: [localhost] => (item=15)
ok: [localhost] => (item=16)
ok: [localhost] => (item=17)
ok: [localhost] => (item=18)
ok: [localhost] => (item=19)
ok: [localhost] => (item=20)
ok: [localhost] => (item=21)
ok: [localhost] => (item=22)

TASK [debug] ******************************************************************************************************************************
ok: [localhost] => {
    "mysequence": [
        "2",
        "2"
    ]
}

PLAY RECAP ********************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

I am using latest version of Ansible. Any help is greatly appreciated!


I need this list for another task. Here is a snippet:

- name: Reboot 22 VMs
  vmware_guest:
    validate_certs: false
    hostname: x.x.x.x
    username: [email protected]
    password: PASSS
    datacenter: DC1
    folder: "DC1/vm/Pod-{{item[1]}}"
    name: "{{item[0]}}-Pod-{{item[1]}}"
    state: restarted
  delegate_to: localhost
  with_nested:
    - "{{myVarList}}"
    - [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
5
  • 2
    What will you be eventually using this list of numbers for? Is there a particular reason why you are trying to do this? I suspect you should be using some alternative method, but more context would allow us to point you to something better.
    – Zoredache
    Mar 9, 2018 at 23:02
  • I need this list for another task. with_nested:- "{{myVarList}}"``- [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
    – gh0st
    Mar 9, 2018 at 23:07
  • Right, you need to explain why you need this list of numbers and what you wanted to accomplish. Mar 9, 2018 at 23:07
  • @MichaelHampton Please see added information
    – gh0st
    Mar 9, 2018 at 23:15
  • 1
    This is the wrong approach. You need to use a dynamic inventory instead. Mar 10, 2018 at 1:01

1 Answer 1

11

Nothing is weird in your results: you set mysequence 22 times, the values of the first 21 iterations are overwritten, the last value is a string 22 converted to a list with list filter. In result you get two-element list with 2 and 2.

What you wanted the task to look like is:

- name: Generate sequance
  set_fact:
    mysequence: "{{ mysequence | default([]) + [item | int] }}"
  with_sequence: start=1 end=22

But what you really wanted is a way of generating a list of integers in a Jinja2 template:

- set_fact:
    mysequence: "{{ range(1, 22 + 1) | list }}"
2
  • Nice answer. Since the OP is mostly using the sequence in a string context, they could probably skip all the bits to convert it to integers though. IE just going with only {{ lookup('sequence', 'start=1 end=22').split(',')}}
    – Zoredache
    Mar 9, 2018 at 23:51
  • 1
    Yes, but the request was for a list of integers.
    – techraf
    Mar 9, 2018 at 23:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .