Skip to content

Latest commit

 

History

History
164 lines (138 loc) · 7.79 KB

File metadata and controls

164 lines (138 loc) · 7.79 KB

Ansible Role jm1.cloudy.netplan

This role helps with managing Netplan configuration from Ansible variables. Role variable netplan_config defines a list of tasks which will be run by this role. Each task calls an Ansible module similar to tasks in roles or playbooks except that only few keywords such as when are supported. For example, to assign static ip address 192.168.0.2, DNS server 192.168.0.1 and default gateway 192.168.0.1 to network interface eth0, define variable netplan_config in group_vars or host_vars as such:

netplan_config:
- # Assign a static ip address, dns server and default gateway to network interface eth0
  ansible.builtin.copy:
    content: |
      # 2021 Jakob Meng, <[email protected]>
      network:
        version: 2
        ethernets:
          eth0:
            dhcp4: false
            dhcp6: false
            accept-ra: false
            addresses:
            - 192.168.0.2/24
            nameservers:
              addresses:
              - 192.168.0.1
            routes:
            - to: 0.0.0.0/0
              via: 192.168.0.1
    dest: /etc/netplan/99-jm1-cloudy-netplan-example.yaml
    owner: root
    group: root
    mode: u=rw,g=r,o=r

⚠️ WARNING: On systems without Netplan, a package such as netplan.io on Debian and Ubuntu has to be installed first. This role will not install Netplan because presumably additional configuration is required such as deactivating the substituted network configuration mechanism like ifupdown on Debian. For example, one may use role jm1.cloudy.packages to install Netplan and role jm1.cloudy.services to deactivate other networking services. :warning:

When this role is executed, it will run all tasks listed in netplan_config. Once all tasks have finished and if anything has changed, then the updated Netplan configuration will be applied (with netplan apply) and the system will be rebooted to apply more complex updates such as changes to the Netplan service itself.

Tested OS images

Available on Ansible Galaxy in Collection jm1.cloudy.

Requirements

This role uses module(s) from collection jm1.ansible. To install this collection you may follow the steps described in README.md using the provided requirements.yml.

Variables

Name Default value Required Description
netplan_config [] false List of tasks to run 1 2 3, e.g. to edit files in /etc/netplan/

Dependencies

None.

Example Playbook

- hosts: all
  become: true
  vars:
    # Variables are listed here for convenience and illustration.
    # In a production setup, variables would be defined e.g. in
    # group_vars and/or host_vars of an Ansible inventory.
    # Ref.:
    # https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
    # https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
    netplan_config:
    - # Assign a static ip address, dns server and default gateway to network interface eth0
      ansible.builtin.copy:
        content: |
          # 2021 Jakob Meng, <[email protected]>
          network:
            version: 2
            ethernets:
              eth0:
                dhcp4: false
                dhcp6: false
                accept-ra: false
                addresses:
                - 192.168.0.2/24
                nameservers:
                  addresses:
                  - 192.168.0.1
                routes:
                - to: 0.0.0.0/0
                  via: 192.168.0.1
        dest: /etc/netplan/99-jm1-cloudy-netplan-example.yaml
        owner: root
        group: root
        mode: u=rw,g=r,o=r
  roles:
  - name: Change Netplan configuration
    role: jm1.cloudy.netplan
    tags: ["jm1.cloudy.netplan"]

For instructions on how to run Ansible playbooks have look at Ansible's Getting Started Guide.

License

GNU General Public License v3.0 or later

See LICENSE.md to see the full text.

Author

Jakob Meng @jm1 (github, galaxy, web)

Footnotes

  1. Useful Ansible modules in this context could be blockinfile, copy, debconf, file, lineinfile and template.

  2. Tasks will be executed with jm1.ansible.execute_module which supports keyword when only.

  3. Tasks will be executed with jm1.ansible.execute_module which supports modules and action plugins only. Some Ansible modules such as ansible.builtin.meta and ansible.builtin.{include,import}_{playbook,role,tasks} are core features of Ansible, in fact not implemented as modules and thus cannot be called from jm1.ansible.execute_module. Doing so causes Ansible to raise errors such as MODULE FAILURE\nSee stdout/stderr for the exact error. In addition, Ansible does not support free-form parameters for arbitrary modules, so for example, change from - debug: msg="" to - debug: { msg: "" }.