Ansible create directories for each environment -
using below snippet of ansible code, trying create multiple subdirectories per variables stored in vars/main.yml. goal every user per env, attempting create multiple sub folders
eventually, folders should created like
/home/testuser1/subdir1 /home/testuser1/subdir2 /home/testuser2/subdir3/subdir4 /home/testuser2/subdir5/subdir6 /home/testuser3/subdir7 /home/testuser3/subdir8 /home/testuser4 - testuser4 not have subfolders, assume ansible not take action
playbook file:
--- - hosts: user: test sudo: yes vars_files: - defaults/main.yml tasks: - name: create directories each env file: path="{{ home }}/{{ item.0 }}{{ item.1.name }}/{{item.0.subdir}}" owner="{{ item.0 }}{{ item.1.name }}" group="{{ item.0 }}{{ item.1.name }}" mode=755 state=directory with_subelements: - "{{ env }}" - "{{ userdir }}" - "{{ subdirs }}"
vars/main.yml
env: - test - qa userdir: - name: user1 subdir: - subdir1 - subdir2 - name: user2 subdirs: - subdir3/subdir4 - subdir5/subdir6 - name: user3 subdirs: - subdir7 - subdir8 - name: user4 subdirs: - []
however, after execution of playbook, getting strange error. sure wrong in above code, couldnt move further. appreciate if experts can throw light
task: [create directories each env] **************************** fatal: [localhost] => subelements lookup expects list of 2 items, first dict or list, , second string fatal: hosts have failed -- aborting
as error message tells, with_subelements
expects string, not var 2nd element. string must property in each element of first dict/list. more problematic is, 3rd element not supported.
the best loop on users , directories , make environments static, because there no solution have nested loops:
- name: create directories test env file: path="{{ home }}/test{{ item.0.name }}/{{item.1}}" owner="test{{ item.0.name }}" group="test{{ item.0.name }}" mode=755 state=directory with_subelements: - userdir - subdirs - name: create directories qa env file: path="{{ home }}/qa{{ item.0.name }}/{{item.1}}" owner="qa{{ item.0.name }}" group="qa{{ item.0.name }}" mode=755 state=directory with_subelements: - userdir - subdirs
in ansible 2.0 (again) possible have nested loops using with_items
include
statement. able this:
- include: create_directories.yml env={{ item }} with_items: env
and in file create_directories.yml
1 task:
- name: create directories {{ env }} env file: path="{{ home }}/{{ env }}{{ item.0.name }}/{{item.1}}" owner="{{ env }}{{ item.0.name }}" group="{{ env }}{{ item.0.name }}" mode=755 state=directory with_subelements: - userdir - subdirs
i don't know when ansible 2.0 released try code directly github. have v2_final branch, appears devel branch has more recent code.
Comments
Post a Comment