loopsansible

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.


Solution

  • Use range. For example,

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