Home > database >  Ansible - Repeat a list for N times
Ansible - Repeat a list for N times

Time:07-19

I have a main playbook that use include to call other playbooks in case conditions are met. That is working fine, but what I need is to execute these playbooks for n times, where n is a user input variable. So, if the user enters "5", the main playbook will call playbooks for 5 times.

This is the example:

---
- name: main playbook
  hosts: localhost
  connection: local
  gather_facts: False
  var_files: weqwewq

  tasks:
    - include: 1.yml
      when: x == "aaa"
    - include: 2.yml
      when: x == "bbb"
    - include: 3.yml
      when: x == "ccc"
    - include: 4.yml
      when: x == "ddd"

What I don't need is this:

  tasks:
    - include: 1.yml
      when: x == "aaa"
      with_sequence: count= "{{ user_input }}"
    - include: 2.yml
      when: x == "aaa bbb"
      with_sequence: count= "{{ user_input }}"
    - include: 3.yml
      when: x == "ccc"
      with_sequence: count= "{{ user_input }}"
    - include: 4.yml
      when: x == "ccc ddd"
      with_sequence: count= "{{ user_input }}"

but instead something like this

tasks:
  with_sequence: count= "{{ user_input }}"
    - include: 1.yml
      when: x == "aaa"
    - include: 2.yml
      when: x == "aaa bbb"
    - include: 3.yml
      when: x == "ccc"
    - include: 4.yml
      when: x == "ccc ddd"

but for this I'm getting an error:

"with_sequence is not a valid attribute for a play".

Any idea?

Thanks!

CodePudding user response:

Just add another include level, and add your sequence loop to that task.


First, note that the include module has been deprecated. You should be using include_tasks.

To loop over your tasks with a single loop construct, start with a top-level playbook like this:

- hosts: localhost
  gather_facts: false
  tasks:
    - include_tasks: tasks.yaml
      with_sequence: count=5

In tasks.yaml, place your include_tasks with conditions:

- name: show loop index
  debug:
    var: item

- include_tasks: tasks_1.yaml
  when: x = 'aaa'
- include_tasks: tasks_2.yaml
  when: x = 'bbb'
- include_tasks: tasks_3.yaml
  when: x = 'ccc'

And then create the individual task_N.yaml files as necessary

CodePudding user response:

Welcome to Stackoverflow. You can use Interactive Input to take N as input. Also, you might consider checking this for more information on the usage of variables in ansible.

Also, it is a good practice to use tasks to organize your code. Check the following code:

Update: I added stride parameter in with_sequence so now increment value can be set other than 1 (default).

Your main playbook file:

---
- name: main playbook
  hosts: localhost
  connection: local
  gather_facts: False
  vars_prompt:

    - name: counter
      prompt: Enter loop counter
      private: no


  tasks:
    - include_tasks: tasks.yml
      with_sequence: start=1 end="{{ counter }}" stride="{{ 1 }}"

The additional tasks.yml:

---

- include_tasks: 1.yml
  when: 
    - x is defined
    - x = "aaa"
- include_tasks: 2.yml
  when:
    - x is defined 
    - x = "bbb"
- include_tasks: 3.yml
  when:
    - x is defined
    - x = "ccc"
- include_tasks: 4.yml
  when:
    - x is defined
    - x = "ddd"

Here in tasks.yml I added conditionals to check whether x is defined.

  • Related