# Create folder using command line

**URL:** <https://forum.ansible.com/t/create-folder-using-command-line/32962>\
**Category:** Ansible Project\
**Created:** [May 18, 2020, 6:39pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962 "2020-05-18T18:39:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [May 18, 2020, 6:39pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962/1 "2020-05-18T18:39:22Z")

</div>

Hi, I’m trying to create folder structure on HDFS storage. I have 3 main folders and each has subfolders.

Thank you.

**Configuration file:**

```auto
companies:
     - company_folder_name: /ibm
       department:
            - it
            - logistic
     - company_folder_name: /hp
       department:
     - company_folder_name: /dell
       department:
            - it

```

**Playbook:**

```auto
## Createing the main folders - working fine ##
   - name: create company folders
     shell: hadoop fs -mkdir {{ item.company_folder_name }}
     register: result
     ignore_errors: yes
     with_items:
            - "{{ companies }}"

## Create subfolders - Not working ##
   - name: create department folders
     shell: hadoop fs -mkdir {{ item.company_folder_name }}/{{item.department}}
     register: result
     ignore_errors: yes
     with_items:
            - "{{ companies }}"

```

---

<div class="post-metadata">

**Author:** ![Dan\_Idar](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dan\_Idar](https://forum.ansible.com/u/Dan_Idar)\
**Post date:** [May 18, 2020, 8:37pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962/2 "2020-05-18T20:37:47Z")

</div>

Do not use shell when you have native modules

ansible can create whole trees using the file module

example:

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [May 18, 2020, 9:13pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962/3 "2020-05-18T21:13:00Z")

</div>

Please note that i’m creating the folders on HDFS Storage so i must use shell

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 18, 2020, 9:24pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962/4 "2020-05-18T21:24:04Z")

</div>

‪On Mon, 18 May 2020 at 20:39, ‫דודו דודו‬‎ \<dudu.confirm@gmail.com\> wrote:‬

> Hi, I’m trying to create folder structure on HDFS storage. I have 3 main folders and each has subfolders.
> 
> Thank you.
> 
> Configuration file:
> 
> companies:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /ibm  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- it  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- logistic  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /hp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /dell  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- it
> 
> Playbook:
> 
> ## Createing the main folders - working fine ##  
> &nbsp;&nbsp;&nbsp;- name: create company folders  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: hadoop fs -mkdir {{ item.company\_folder\_name }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ companies }}"
> 
> ## Create subfolders - Not working ##  
> &nbsp;&nbsp;&nbsp;- name: create department folders  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: hadoop fs -mkdir {{ item.company\_folder\_name }}/{{item.department}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ companies }}"

You iterate over "companies" but that contains a list itself, so  
items.department won't work.

Why do you want to ignore errors BTW? they're usually a sign that  
something is wrong and needs fixing....

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 18, 2020, 9:39pm UTC](https://forum.ansible.com/t/create-folder-using-command-line/32962/5 "2020-05-18T21:39:23Z")

</div>

> companies:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /ibm  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- it  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- logistic  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /hp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- company\_folder\_name: /dell  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;department:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- it  
> ## Createing the main folders - working fine ##  
> &nbsp;&nbsp;&nbsp;- name: create company folders  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: hadoop fs -mkdir {{ item.company\_folder\_name }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ companies }}"

Try this

&nbsp;&nbsp;&nbsp;&nbsp;- name: create company folders  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: 'hadoop fs -mkdir {{ item.0.company\_folder\_name }}/{{ item.1 }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;companies,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'department',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'skip\_missing': True}) }}"

The attribute 'department' must be a list, even an empty one 'department:  
'. If 'department' isn't a list the plugin 'subelements' will crash. Fix  
the dictionary.

HTH,

&nbsp;&nbsp;-vlado
