Looking For next Integer Directory created , if current already exist.

i am looking for some concent like below

  1. Look For /var/test01 , if not there create it- test01
  2. Look For /var/test01 , if already exist then move to next name :- test02 (move like next integer number)
  3. Look for /var/test01 or 02 , then move to next name :- test03 (move like next integer number)
    and so on.

the below concept is not working

  • name: “Register a Variable and Search to perform Operation”
    hosts: localhost
    tasks:
  • name: Create a series of directories with even numbers for some reason
    ansible.builtin.file:
    dest: “/var/test{{ ‘%02d’ | format(item) }}”
    state: directory
    mode: ‘0655’
    become: true
    loop: “{{ range(0, 100) | list }}”

You are asking us to solve your problem. You aren’t asking us a question about ansible itself.

Walter

I tried 2-3 combination , but nothing worked , So i thought of asking here , Since my issue fix from here only.
Then i found the solution by small research on jinja and dict register output

  • name: “Register a Variable and Search to perform Operation”
    hosts: localhost
    vars:
    list_of_sequence: “{{ range(1, 100) | list }}”
    tasks:
  • name: Create a series of directories with even numbers for some reason
    ansible.builtin.file:
    path: “/var/test{{ ‘%02d’ | format(item) }}”
    state: directory
    mode: ‘0655’
    become: true
    register: res
    when: not (res.changed | d(false))
    loop: ‘{{ list_of_sequence }}’

Clever. Does it create 99 local directories, or just the next number that doesn’t exist?

Walter

it create only next number , which doesn’t exist
the powerful line is :-

when: not (res.changed | d(false))

Clever indeed. Nice work.

Walter