ansible: Manage multiple netplan configurations
authorKienan Stewart <kstewart@efficios.com>
Wed, 25 Sep 2024 16:19:13 +0000 (12:19 -0400)
committerKienan Stewart <kstewart@efficios.com>
Wed, 25 Sep 2024 16:19:13 +0000 (12:19 -0400)
Change-Id: I8e3b59f1fafcaf1c94440b1e334026baa4fa4d00
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
automation/ansible/roles/netplan/defaults/main.yml
automation/ansible/roles/netplan/handlers/main.yml [new file with mode: 0644]
automation/ansible/roles/netplan/tasks/config.yml [new file with mode: 0644]
automation/ansible/roles/netplan/tasks/main.yml
automation/ansible/roles/netplan/templates/netplan.yaml.j2 [new file with mode: 0644]

index cb810d84888c4ac316ab785a331b6f42e69681fe..81552ea81e15f8c22567c9b598f5c1562fe40d41 100644 (file)
@@ -1,4 +1,20 @@
 ---
 netplan_manage: false
+
+# 0 or more non default config files
+# E.g.
+# netplan_configs:
+#   - name: example
+#     # filename: example  # don't add '.yaml', defautls to name
+#     # state: present
+#     # dependencies: []  # list of packages to install to support this configuration
+#     content:
+#       network:
+#         version: 2
+#         ethernets:
+#           eth1:
+#             addresses:
+#               ...
+netplan_configs: []
 netplan_content: ''
 netplan_apply: true
diff --git a/automation/ansible/roles/netplan/handlers/main.yml b/automation/ansible/roles/netplan/handlers/main.yml
new file mode 100644 (file)
index 0000000..82755b7
--- /dev/null
@@ -0,0 +1,6 @@
+---
+
+- name: Apply netplan
+  when: netplan_apply
+  ansible.builtin.command:
+    argv: ['netplan', 'apply']
diff --git a/automation/ansible/roles/netplan/tasks/config.yml b/automation/ansible/roles/netplan/tasks/config.yml
new file mode 100644 (file)
index 0000000..8cb63e4
--- /dev/null
@@ -0,0 +1,22 @@
+---
+- when: item.dependencies|default([])
+  ansible.builtin.package:
+    name: "{{item.dependencies}}"
+    install_recommends: false
+
+- when: state == 'present'
+  ansible.builtin.template:
+    src: "netplan.yaml.j2"
+    dest: "{{filename}}"
+    mode: '0600'
+    owner: 'root'
+    group: 'root'
+  notify:
+    - 'Apply netplan'
+
+- when: state != 'present'
+  ansible.builtin.file:
+    path: "{{filename}}"
+    state: "{{state}}"
+  notify:
+    - 'Apply netplan'
index 902a1e127f1a89519a18c2c6a0a775aea87f647d..43476d310da5d5bdf7d815211ae6f82f08061152 100644 (file)
@@ -5,13 +5,15 @@
       ansible.builtin.apt:
         name: netplan.io
     - name: Update netplan default.yaml
+      when: netplan_content
       ansible.builtin.copy:
         content: "{{netplan_content}}"
         dest: /etc/netplan/default.yaml
         owner: root
         group: root
         mode: '0600'
-      register: netplan_default_yaml
+      notify:
+        - 'Apply netplan'
     - name: Disable ifupdown on Debian
       when: ansible_distribution == "Debian"
       block:
             name: systemd-networkd
             enabled: true
             state: started
-    - name: Apply netplan
-      when: netplan_default_yaml.changed and netplan_apply
-      ansible.builtin.command:
-        argv: ['netplan', 'apply']
+    - name: Manage non-default netplan configurations
+      ansible.builtin.include_tasks:
+        file: config.yml
+      vars:
+        "filename": "/etc/netplan/{{item.filename|default(item.name)}}.yaml"
+        "netplan_content": "{{item.content}}"
+        "state": "{{item.state|default('present')}}"
+      loop: "{{netplan_configs}}"
diff --git a/automation/ansible/roles/netplan/templates/netplan.yaml.j2 b/automation/ansible/roles/netplan/templates/netplan.yaml.j2
new file mode 100644 (file)
index 0000000..73f71c5
--- /dev/null
@@ -0,0 +1 @@
+{{netplan_content | to_nice_yaml }}
This page took 0.025753 seconds and 4 git commands to generate.