1

I want to run ansible role by iterating it through count of value that I am providing.

Say for example: Below is the Ansible role main.yml, in which I include a yaml file to execute where this included yaml file should execute the number of times with the loop which I have.

Here is my small piece of code where I used with_sequence module. But if you have any other suggestion to run the create_db.yml file multiple times, please share with or how to loop through with the current code that I have.

Could someone help me on this?

---
# tasks file for create db
  - hosts: localhost
    become: yes
    tasks:
      - include: create_db.yml 
        with_sequence: count = 2

I am getting below error while executing the playbook fatal: [localhost]: FAILED! => {"msg": "unrecognized arguments to with_sequence: [u'_raw_params']"}

Alfred Hitchkock
  • 357
  • 2
  • 14
  • 2
    What is the error you get ? How is this not fulfilling your requirement ? Meanwhile, given the name of the include I would somewhat expect the loop to provide e.g. a name for the db, hence would rather be `with_items: ['admin_db', 'user_db']. But since you are not showing the include content and not giving any requirement.... it all comes down to: are you happy with the situation?. If yes, then there is actually no question. – Zeitounator Apr 27 '21 at 01:24
  • 1
    I am getting ```fatal: [localhost]: FAILED! => {"msg": "unrecognized arguments to with_sequence: [u'_raw_params']"} ``` this error while executing the play book..As I wanted to run the create_db.yml file 2 times ( as per my count) I observed this error and not able to loop this – Alfred Hitchkock Apr 27 '21 at 04:52
  • 1
    @Zeitounator, there is no with_items here... this is my actual code to run the create_db.yml playbook – Alfred Hitchkock Apr 27 '21 at 04:52
  • 1
    => `count=2` i.e. no spaces. Consider moving this to the [new `loop` syntax](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-sequence). You should also use `include_tasks` in place of the yet to be deprecated `include` – Zeitounator Apr 27 '21 at 06:06

1 Answers1

2

As mentioned in the comments, the use case for running the same task file twice without any change in parameters is not clear. But the error...

unrecognized arguments to with_sequence

... indicates that the syntax for specifying count is incorrect. Please note that there should be no spaces around =, i.e. count=2.

    tasks:
      - include: create_db.yml 
        with_sequence: count=2
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • 1
    yeah my bad... actually the use case is, I want to create multiple EC2 instances which should spread across AZs in AWS. I tried with ```count``` module, but it create instances in same AZ. So I have the logic to store subnet which picked on first run and made sure that the same subnet is not picked on the second run. I can achieve this by executing the create_db.yml file based on the iteration. – Alfred Hitchkock Apr 27 '21 at 07:09