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

**URL:** <https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587>\
**Category:** Ansible Project\
**Created:** [January 27, 2023, 1:32pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587 "2023-01-27T13:32:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 27, 2023, 1:32pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/1 "2023-01-27T13:32:38Z")

</div>

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 }}”

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 27, 2023, 1:35pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/2 "2023-01-27T13:35:58Z")

</div>

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

Walter

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 27, 2023, 3:23pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/3 "2023-01-27T15:23:27Z")

</div>

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 }}’

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 27, 2023, 3:25pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/4 "2023-01-27T15:25:30Z")

</div>

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

Walter

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 27, 2023, 3:31pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/5 "2023-01-27T15:31:33Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 27, 2023, 3:36pm UTC](https://forum.ansible.com/t/looking-for-next-integer-directory-created-if-current-already-exist/37587/6 "2023-01-27T15:36:02Z")

</div>

Clever indeed. Nice work.

Walter
