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

23 views
Skip to first unread message

javed khan Siddque

unread,
Jan 27, 2023, 8:32:38 AM1/27/23
to Ansible Project
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 }}"

Rowe, Walter P. (Fed)

unread,
Jan 27, 2023, 8:36:19 AM1/27/23
to ansible...@googlegroups.com
You are asking us to solve your problem. You aren't asking us a question about ansible itself.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c364f32a-b419-4313-a9d5-8085384f658an%40googlegroups.com.

javed khan Siddque

unread,
Jan 27, 2023, 10:23:27 AM1/27/23
to Ansible Project
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 }}'


Rowe, Walter P. (Fed)

unread,
Jan 27, 2023, 10:25:49 AM1/27/23
to ansible...@googlegroups.com
Clever. Does it create 99 local directories, or just the next number that doesn't exist?


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

javed khan Siddque

unread,
Jan 27, 2023, 10:31:33 AM1/27/23
to Ansible Project
it create only next number , which doesn't exist
the powerful line is :-

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

Rowe, Walter P. (Fed)

unread,
Jan 27, 2023, 10:36:25 AM1/27/23
to ansible...@googlegroups.com
Clever indeed. Nice work.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
Reply all
Reply to author
Forward
0 new messages