openwrt/image: proper fix for empty directories in mixins
authorChristian Pointner <equinox@realraum.at>
Fri, 20 Sep 2019 21:44:15 +0000 (23:44 +0200)
committerChristian Pointner <equinox@realraum.at>
Fri, 20 Sep 2019 21:44:15 +0000 (23:44 +0200)
ansible/host_vars/torwaechter/main.yml
ansible/roles/openwrt/image/filter_plugins/openwrt.py [new file with mode: 0644]
ansible/roles/openwrt/image/tasks/prepare.yml

index f6d4ec1..9e9e990 100644 (file)
@@ -24,9 +24,8 @@ openwrt_packages_extra:
   - lsblk
 
 openwrt_mixin:
-  ## this file will not be created because there is no file, link or content field below it
-  ## but it will force the creation of /home
-  /home/.placeholder: {}
+  /home:
+    directory:
   /run:
     link: "/var/run"
 
diff --git a/ansible/roles/openwrt/image/filter_plugins/openwrt.py b/ansible/roles/openwrt/image/filter_plugins/openwrt.py
new file mode 100644 (file)
index 0000000..8443b30
--- /dev/null
@@ -0,0 +1,22 @@
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+from ansible import errors
+
+
+def openwrt_mixin_type(data, mixin_type):
+    try:
+        return [{'key': x, 'value': data[x]} for x in data if mixin_type in data[x]]
+
+    except Exception as e:
+        raise errors.AnsibleFilterError("openwrt_mixin_type(): %s" % str(e))
+
+
+class FilterModule(object):
+
+    filter_map = {
+        'openwrt_mixin_type': openwrt_mixin_type,
+    }
+
+    def filters(self):
+        return self.filter_map
index a3ab711..f3eafc7 100644 (file)
@@ -9,46 +9,50 @@
     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: Copy mixins in place [1/3]
+- name: Create symlinks
   file:
     dest: "{{ openwrt_imgbuilder_files }}/{{ item.key }}"
     src: "{{ item.value.link }}"
     force: yes
     follow: no
     state: link
-  with_dict: "{{ openwrt_mixin }}"
-  when: '"link" in item.value'
+  loop: "{{ openwrt_mixin | openwrt_mixin_type('link') }}"
   loop_control:
     label: "{{ item.key }}"
 
-- name: Copy mixins in place [2/3]
+- 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 [3/3]
+- 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 }}"