我正在尝试在HDFS存储上创建文件夹结构。我有3个主文件夹,每个文件夹都有子文件夹。
我在更新子文件夹时遇到问题-我期望将执行以下命令
但是,执行以下命令(我使用debug msg来打印第二个失败的任务的命令) 看来我需要在作为字典的部门上运行循环
ok: [10.201.51.16] => (item={u'department': [u'it', u'logistic'], u'company_folder_name': u'/ibm'}) => {
"msg": "hadoop fs -mkdir /company//ibm/[u'it', u'logistic']"
}
ok: [10.201.51.16] => (item={u'department': None, u'company_folder_name': u'/hp'}) => {
"msg": "hadoop fs -mkdir /company//hp/"
}
ok: [10.201.51.16] => (item={u'department': [u'it'], u'company_folder_name': u'/dell'}) => {
"msg": "hadoop fs -mkdir /company//dell/[u'it']"
谢谢。
配置文件:
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 }}"
## 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 }}"
答案 0 :(得分:0)
创建公司基础目录的第一个任务是正确的选择。
对于下一个,我们首先在列表中选择具有非空departement
字段的对象。这是通过下面的selectattr
filter
然后,我们使用subelements
lookup在此列表上循环,以便为每个公司/部门组合获得一个迭代。
以下结果手册:
---
- hosts: localhost
gather_facts: false
vars:
companies:
- company_folder_name: /ibm
department:
- it
- logistic
- company_folder_name: /hp
department:
- company_folder_name: /dell
department:
- it
tasks:
- name: create company folders
debug:
msg: "hadoop fs -mkdir {{ item.company_folder_name }}"
with_items: "{{ companies }}"
- name: create department folders
debug:
msg: "hadoop fs -mkdir {{ item.0.company_folder_name }}/{{ item.1 }}"
with_subelements:
- "{{ companies | selectattr('department') | list }}"
- department
哪个给:
PLAY [localhost] ***************************************************************************************************************************************
TASK [create company folders] **************************************************************************************************************************
ok: [localhost] => (item={'company_folder_name': '/ibm', 'department': ['it', 'logistic']}) => {
"msg": "hadoop fs -mkdir /ibm"
}
ok: [localhost] => (item={'company_folder_name': '/hp', 'department': None}) => {
"msg": "hadoop fs -mkdir /hp"
}
ok: [localhost] => (item={'company_folder_name': '/dell', 'department': ['it']}) => {
"msg": "hadoop fs -mkdir /dell"
}
TASK [create department folders] ***********************************************************************************************************************
ok: [localhost] => (item=[{'company_folder_name': '/ibm'}, 'it']) => {
"msg": "hadoop fs -mkdir /ibm/it"
}
ok: [localhost] => (item=[{'company_folder_name': '/ibm'}, 'logistic']) => {
"msg": "hadoop fs -mkdir /ibm/logistic"
}
ok: [localhost] => (item=[{'company_folder_name': '/dell'}, 'it']) => {
"msg": "hadoop fs -mkdir /dell/it"
}
PLAY RECAP *********************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0