Create folder using command line

19 views
Skip to first unread message

דודו דודו

unread,
May 18, 2020, 2:39:22 PM5/18/20
to Ansible Project

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

Dan Idar

unread,
May 18, 2020, 4:37:47 PM5/18/20
to Ansible Project
Do not use shell when you have native modules

ansible can create whole trees using the file module

example:

---
- hosts: localhost

  tasks:
    - name: create dir structure
      file:
        path: 'ibm/{{ item }}'
        state: directory
      with_items:
        - it
        - logisitic

this simple playbook will create the directoires ibm/it and ibm/logistics even if the parent folder ibm does not exist .



you can streamline this with loop control to create to loops one with the company names other with the subdirectories

דודו דודו

unread,
May 18, 2020, 5:13:00 PM5/18/20
to Ansible Project
Please note that i'm creating the folders on HDFS Storage so i must use shell 

Dick Visser

unread,
May 18, 2020, 5:24:45 PM5/18/20
to ansible...@googlegroups.com
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....

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Vladimir Botka

unread,
May 18, 2020, 5:39:41 PM5/18/20
to דודו דודו, ansible...@googlegroups.com
> 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
Reply all
Reply to author
Forward
0 new messages