Kostas Demiris
Kostas Demiris

Reputation: 3611

Using a variable when iterating over range in Ansible

I have this task :

---
- name: Deploy & Register the Gitlab runners
  command: >
    docker run
    --name runner{{item}}
    -d
    -e CI_SERVER_URL="{{CI_SERVER_URL}}"
    -e REGISTRATION_TOKEN="{{REGISTRATION_TOKEN}}"
    -e DOCKER_PRIVILEGED=true
    -e REGISTER_LOCKED=false
    -v /var/run/docker.sock:/var/run/docker.sock
    --restart=always
    flakm/gitlab-runner-auto-register:latest
  with_items:
    - [ 1 , 2 ]

which works. But I would like to be able to customise the number of the runners.

So, I would like something like this

  with_items:
    - [ 1 , {{ NUMBER_ΟF_RUNNERS }} ]

but this does not work.

I have read this from the Ansible docs, but could not find anything.

Upvotes: 1

Views: 844

Answers (2)

Kostas Demiris
Kostas Demiris

Reputation: 3611

I found this solution

with_sequence: count={{NUMBER_OF_RUNNERS}}

here https://docs.ansible.com/ansible/2.4/playbooks_loops.html#looping-over-integer-sequences

Upvotes: 0

Vladimir Botka
Vladimir Botka

Reputation: 67959

Use range. For example

  loop: "{{ range(1, NUMBER_ΟF_RUNNERS + 1)|list }}"

Upvotes: 2

Related Questions