Create folder using command line

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:
     - company_folder_name: /ibm
       department:
            - it
            - logistic
     - company_folder_name: /hp
       department:
     - company_folder_name: /dell
       department:
            - it

Playbook:

## 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 }}"

Do not use shell when you have native modules

ansible can create whole trees using the file module

example:

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

‪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:
     - company_folder_name: /ibm
       department:
            - it
            - logistic
     - company_folder_name: /hp
       department:
     - company_folder_name: /dell
       department:
            - it

Playbook:

## 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 }}"

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....

companies:
     - company_folder_name: /ibm
       department:
            - it
            - logistic
     - company_folder_name: /hp
       department:
     - company_folder_name: /dell
       department:
            - it
## 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 }}"

Try this

    - name: create company folders
      shell: 'hadoop fs -mkdir {{ item.0.company_folder_name }}/{{ item.1 }}'
      register: result
      ignore_errors: yes
      loop: "{{ lookup('subelements',
                       companies,
                       'department',
                       {'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,

  -vlado