Skip to content

Instantly share code, notes, and snippets.

@mrlesmithjr
Last active January 12, 2019 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlesmithjr/f8a18d7e5eb26ad5390f927e183b5dbd to your computer and use it in GitHub Desktop.
Save mrlesmithjr/f8a18d7e5eb26ad5390f927e183b5dbd to your computer and use it in GitHub Desktop.
---
- hosts: localhost
gather_facts: false
connection: local
vars:
# Defines number of VMs to create
vm_count: 10
# Defines VM naming
vm_naming: vm
# Defines VM number to start with
vm_start: 0
# Defines VM number stepping
vm_step: 1
tasks:
- name: VM Loop Sequence
debug:
msg: "{{ vm_naming + ('0' + item|string if item < 10 else item|string) }}"
loop: "{{ range(vm_start, vm_count, vm_step)|list }}"
- name: Create VM Group
add_host:
group: VMS
host: "{{ vm_naming + ('0' + item|string if item < 10 else item|string) }}"
loop: "{{ range(vm_start, vm_count, vm_step)|list }}"
- debug: var=groups['VMS']
@mrlesmithjr
Copy link
Author

mrlesmithjr commented Jan 12, 2019

(default-python-2) ➜  sequence_loops ansible-playbook vm_loop_sequence.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'


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

TASK [VM Loop Sequence] ********************************************************
ok: [localhost] => (item=0) => {
    "msg": "vm00"
}
ok: [localhost] => (item=1) => {
    "msg": "vm01"
}
ok: [localhost] => (item=2) => {
    "msg": "vm02"
}
ok: [localhost] => (item=3) => {
    "msg": "vm03"
}
ok: [localhost] => (item=4) => {
    "msg": "vm04"
}
ok: [localhost] => (item=5) => {
    "msg": "vm05"
}
ok: [localhost] => (item=6) => {
    "msg": "vm06"
}
ok: [localhost] => (item=7) => {
    "msg": "vm07"
}
ok: [localhost] => (item=8) => {
    "msg": "vm08"
}
ok: [localhost] => (item=9) => {
    "msg": "vm09"
}

TASK [Create VM Group] *********************************************************
changed: [localhost] => (item=0)
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
changed: [localhost] => (item=3)
changed: [localhost] => (item=4)
changed: [localhost] => (item=5)
changed: [localhost] => (item=6)
changed: [localhost] => (item=7)
changed: [localhost] => (item=8)
changed: [localhost] => (item=9)

TASK [debug] *******************************************************************
ok: [localhost] => {
    "groups['VMS']": [
        "vm00",
        "vm01",
        "vm02",
        "vm03",
        "vm04",
        "vm05",
        "vm06",
        "vm07",
        "vm08",
        "vm09"
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

(default-python-2) ➜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment