ansible.builtin.sequence (v2.16.5) — lookup

generate a list based on a number sequence

| "added in version" 1.0 of ansible.builtin"

Authors: Jayson Vantuyl (!UNKNOWN) <jayson@aggressive.ly>

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

generates a sequence of items. You can specify a start value, an end value, an optional "stride" value that specifies the number of steps to increment the sequence, and an optional printf-style format string.

Arguments can be specified as key=value pair strings or as a shortcut form of the arguments string is also accepted: [start-]end[/stride][:format].

Numerical values can be specified in decimal, hexadecimal (0x3f8) or octal (0600).

Starting at version 1.9.2, negative strides are allowed.

Generated items are strings. Use Jinja2 filters to convert items to preferred type, e.g. C({{ 1 + item|int }}).

See also Jinja2 C(range) filter as an alternative.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create some test users
  ansible.builtin.user:
    name: "{{ item }}"
    state: present
    groups: "evens"
  with_sequence: start=0 end=32 format=testuser%02x
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create a series of directories with even numbers for some reason
  ansible.builtin.file:
    dest: "/var/stuff/{{ item }}"
    state: directory
  with_sequence: start=4 end=16 stride=2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: a simpler way to use the sequence plugin create 4 groups
  ansible.builtin.group:
    name: "group{{ item }}"
    state: present
  with_sequence: count=4
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: the final countdown
  ansible.builtin.debug:
    msg: "{{item}} seconds to detonation"
  with_sequence: start=10 end=0 stride=-1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Use of variable
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_sequence: start=1 end="{{ end_at }}"
  vars:
    - end_at: 10

Inputs

    
end:
    default: 0
    description: number at which to end the sequence, dont use this with count
    type: integer

count:
    default: 0
    description: number of elements in the sequence, this is not to be used with end
    type: integer

start:
    default: 0
    description: number at which to start the sequence
    type: integer

format:
    description: return a string with the generated number formatted in

stride:
    description: increments between sequence numbers, the default is 1 unless the end
      is less than the start, then it is -1.
    type: integer

Outputs

_list:
  description:
  - A list containing generated sequence of items
  elements: str
  type: list