openwrt/image: proper fix for empty directories in mixins
[noc.git] / ansible / roles / openwrt / image / tasks / prepare.yml
index b6b67c5..f3eafc7 100644 (file)
@@ -1,40 +1,58 @@
 ---
 - name: Create temporary build directory
-  command: mktemp --tmpdir -d openwrt-{{ inventory_hostname }}.XXXXXX
+  tempfile:
+    state: directory
   register: tmpdir
 
-- set_fact:
-    openwrt_imgbuilder_dir:   "{{ tmpdir.stdout }}"
-    openwrt_imgbuilder_files: "{{ tmpdir.stdout }}/files"
+- name: set variables needed to build images
+  set_fact:
+    openwrt_imgbuilder_dir:   "{{ tmpdir.path }}"
+    openwrt_imgbuilder_files: "{{ tmpdir.path }}/files"
+
+- name: Create the download directory
+  file:
+    path: "{{ openwrt_download_dir }}/dl/{{ openwrt_arch }}"
+    state: directory
+    mode: '0755'
 
 - name: Create the directories for mixins
   file:
     path: "{{ item }}"
     state: directory
     mode: '0755'
-  with_items:
-    - "{{ openwrt_download_dir }}/dl/{{ openwrt_arch }}"
-    - "{{ openwrt_imgbuilder_files }}/etc/config"
-    - "{{ openwrt_mixin | map('dirname') | map('regex_replace', '^', openwrt_imgbuilder_files) | unique | list }}"
+  loop: "{{ directories | flatten | unique | map('regex_replace', '^', openwrt_imgbuilder_files) | list }}"
+  vars:
+    directories:
+      - "/etc/config"
+      - "{{ openwrt_mixin | map('dirname') | list }}"
+      - "{{ openwrt_mixin | openwrt_mixin_type('directory') | map(attribute='key') | list }}"
 
+- name: Create symlinks
+  file:
+    dest: "{{ openwrt_imgbuilder_files }}/{{ item.key }}"
+    src: "{{ item.value.link }}"
+    force: yes
+    follow: no
+    state: link
+  loop: "{{ openwrt_mixin | openwrt_mixin_type('link') }}"
+  loop_control:
+    label: "{{ item.key }}"
 
-- name: Copy mixins in place [1/2]
+- name: Copy mixins in place (from file)
   copy:
     src: "{{ item.value.file }}"
     dest: "{{ openwrt_imgbuilder_files }}/{{ item.key }}"
     mode: "{{ item.value.mode | default('0644') }}"
-  with_dict: "{{ openwrt_mixin }}"
-  when: '"file" in item.value'
+  loop: "{{ openwrt_mixin | openwrt_mixin_type('file') }}"
   loop_control:
     label: "{{ item.key }}"
 
-- name: Copy mixins in place [2/2]
+- name: Copy mixins in place (from content)
   copy:
     content: "{{ item.value.content }}"
     dest: "{{ openwrt_imgbuilder_files }}/{{ item.key }}"
     mode: "{{ item.value.mode | default('0644') }}"
-  with_dict: "{{ openwrt_mixin }}"
-  when: '"content" in item.value'
+  loop: "{{ openwrt_mixin | openwrt_mixin_type('content') }}"
   loop_control:
     label: "{{ item.key }}"
 
     trim_blocks: yes
   when: openwrt_groups is defined or openwrt_users is defined
 
-- unarchive:
+- name: extract image builder tarball
+  unarchive:
     copy: False
     src:  "{{ openwrt_download_dir }}/{{ openwrt_tarball_name }}"
     dest: "{{ openwrt_imgbuilder_dir }}"