Code Monkey home page Code Monkey logo

ansible-netplan's Introduction

Table of Contents generated with DocToc

ansible-netplan

An Ansible role to manage Netplan

Requirements

You probably want to run the role with become: true

Role Variables

defaults/main.yml

Dependencies

Example Playbook

The following is a trivial example of a playbook that sets a single network interface. See defaults/main.yml for a full list of values that can be set for this role.

---
- hosts: ...your hosts...
  any_errors_fatal: true
  roles:
    - role: mrlesmithjr.netplan
      become: yes
      # This role will do nothing unless netplan_enabled is true.
      netplan_enabled: true
      
      # This should point to an existing netplan configuration file 
      # on your system which this role will overwrite, 
      # or to a nonexistent file which netplan is aware of.
      #
      # The default is /etc/netplan/config.yaml.
      netplan_config_file: /etc/netplan/my-awesome-netplan.yaml
      
      # Ubuntu 18.04, for example, defaults to using networkd.
      netplan_renderer: networkd
      # Simple network configuration to add a single network interface.
      # Configuration defined bellow will be written to the file defined
      # above in `netplan_config_file`.
      netplan_configuration:
        network:
          version: 2
          ethernets:
            enp28s0f7:
              addresses:
                - 10.11.12.99/24

Using vaulted variables

Vault encrypted variables need to be defined outside the netplan_configuration variable to be evaluated.

netplan_configuration:
  network:
    version: 2
    tunnels:
      wg_test:
        mode: wireguard
        key: "{{ my_wireguard_private_key }}"
      ....

my_wireguard_private_key: !vault |
          31366530666465373834386563636465636135323562303866363333333865376330303130363162
          ....

License

MIT

Author Information

Larry Smith Jr.

Buy Me A Coffee

ansible-netplan's People

Contributors

artivis avatar cormoran96 avatar fbourqui avatar iamzili avatar jsquyres avatar lanefu avatar mabasic avatar memberit avatar mrlesmithjr avatar mtze avatar mwellnitz avatar nkakouros avatar robinzoncruzo avatar whalecoiner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ansible-netplan's Issues

Netplan configuration missing on reboot

For some reason after I run this role , the configuration file is applied correctly, but not saved to etc/netplan.

Also my default 00-installer-config.yaml file is deleted. So, when I reboot the machine I have no connectivity.

Any clues?

What is the default behavior of this role?

Add versioning

The update of the template file in a recent commit broke my playbooks. It would be nice to have versions to avoid suck breakages during updates.

License

Hi, could you please add a LICENSE file to the repo?

Thanks :)

Multiple DNS Servers as variable

Describe the bug
Hi! I want to add multiple IP addresses and nameservers via variables

To Reproduce

cat group_vars/all.yaml
dnsserver:
  - 208.67.222.222
  - 208.67.220.220

playbook:

      netplan_configuration: '{
        "network": {
          "ethernets": {
            "{{ ipv4_default_interface }}": {
              "addresses": [ "{{ ipv4_default_address }}/24" ],
              "dhcp4": false,
              "gateway4": "172.21.10.254",
              "nameservers": {
                "addresses": "{{ dnsserver }}"
              }
            }
          }
        }
      }'

the best that it does is this:

       nameservers:
            addresses: '[''208.67.222.222'', ''208.67.220.220'']'

all those single quotes should be gone

Expected behavior
create netplan.yaml with those nameservers

What if the interface is from a variable?

for example, my network interface name is from {{ ansible_default_ipv4.interface }}, but it couldn't be a dictionary key in yaml, it will render as a plain text '{{ ansible_default_ipv4.interface }}': in netplan config.yaml file.

netplan will go to Error in network definition /etc/netplan/config.yaml line 5 column 4: Invalid name '{{ ansible_default_ipv4.interface }}'

nplan causes installation failure on Ubuntu 23.10

nplan, referenced as a package to install by this role, no longer exists in Ubuntu Mantic 23.10. As such the role installation fails.

In an earlier LTS version (22.04) apt-cache showpkg nplan shows that nplan is a virtual package that simply reverse provides netplan.io:

apt-cache showpkg nplan
Package: nplan
Versions:

Reverse Depends:
  netplan.io,nplan 0.34~
  netplan.io,nplan 0.34~
  netplan.io,nplan 0.34~
  netplan.io,nplan 0.34~
Dependencies:
Provides:
Reverse Provides:
netplan.io 0.106.1-7ubuntu0.22.04.2 (= )
netplan.io 0.104-0ubuntu2 (= )

It would appear (to my untrained eye) that all nplan does is make sure the already requested netplan.io is installed.

Given that nplan is appears suplerflous even on pre-23.10 versions, could it simply be removed?

How to configure IPs on multiple servers

Hi,
The example says

- hosts: ...your hosts...
...
      netplan_configuration:
...
                - 10.11.12.99/24

Its not obvious to me from the role how it decerns which of the hosts (plural) a given IP should apply to.
I'm going to try using host_vars to set it that way but i wonder if thats correct given the example in README.

Backup not done and some lint issues

Describe the bug
I notice that in my deployments backup are not done.

I identify 2 problems with that:

  1. I only set netplan_backup_existing but is not considered to launch tasks/existing.yml
  2. Lint where not good so backup was not done

Expected behavior
When only setting netplan_backup_existing (and not netplan_remove_existing), backup should be done.

I could make a PR with:

tasks/netplan.yml

- import_tasks: existing.yml
  when:
    - netplan_remove_existing or netplan_backup_existing

instead of

- import_tasks: existing.yml
  when:
    - netplan_remove_existing

And about lint, in tasks/existing.yml, add spaces before/after brackets for src: "{{ item['path'] }}" and dest: "{{ item['path'] }}.bk".

While reading this task, I also notice that removing tack does not have conditional about netplan_remove_existing, so maybe useful to add it:

- name: Removing Existing Configurations
  file:
    path: "{{ item['path'] }}"
    state: absent
  become: true
  with_items: "{{ _netplan_configs['files'] }}"
  when:
    - netplan_remove_existing
    - item['path'] != netplan_config_file
    - netplan_configuration != []

Netplan role removes configurations by default

Describe the bug
If we are using default variables, this role remove all existing netplan configuration on server and add empty ansible-config.yaml configuration

To Reproduce
Steps to reproduce the behavior:

  1. do not reassign any netplan variables
  2. run playbook with this role
  3. $ ls /etc/netplan # on your server
  4. will see /etc/netplan/ansible-config.yaml
  5. $ cat /etc/netplan/ansible-config.yaml
  6. will see it's empty

Expected behavior
Existing netplan configuration exists in /etc/netplan

Screenshots
telegram-cloud-photo-size-2-5280494078494103384-x
telegram-cloud-photo-size-2-5280494078494103386-m

Desktop (please complete the following information):

  • OS: ubuntu 22.04

Additional context
This happens because your default netplan_configuration asssigned as {} (object), but in task conditions it's expected to be not an array []

Question about changing ip addresses

If I change ip addresses the ip address i'm currently accessing the machine on changes, is there a theory on what we can do to handle this. Perhaps a wait flag etc

Is it currently possible to use the role for multiple netplan files? If not, can I request that feature

Is your feature request related to a problem? Please describe.
When I define the role multipe times, it results in only the final configuration in the end.

Describe the solution you'd like
I would like the following example to result in 2 netplan configuration files, both applied (Is that possible? I am fairly new to netplan).

Describe alternatives you've considered
For now, I will have to merge them into a single file, overwriting older configs. My use case is that I move around to multiple different hotspots, different networks, etc. I want my devices to connect ot the services running within a VM where allowed by the network. On a new network, I would rerun the playbook.

Additional context
Here is the example config.

- name: Setup server to reserve predefined private IP
  hosts: all
  become: yes
  pre_tasks:
    - name: Set facts
      set_fact:
        lokal_server_interface: "{{ lookup('env', 'LOKAL_SERVER_INTERFACE') }}"
        lokal_server_gateway: "{{ lookup('env', 'LOKAL_SERVER_GATEWAY') }}"
        lokal_server_ip: "{{ lookup('env', 'LOKAL_SERVER_IP') }}"
  roles:
    - role: mrlesmithjr.netplan
      netplan_enabled: true
      netplan_config_file: /etc/netplan/lokal-{{ lokal_server_gateway }}.yaml
      netplan_renderer: networkd
      netplan_configuration: '{
        "network": {
          "ethernets": {
            "{{ lokal_server_interface }}": {
              "addresses": ["{{ lokal_server_ip }}/24"],
              "gateway4": "{{ lokal_server_gateway }}",
              "nameservers": {
                "addresses": ["1.1.1.1", "1.0.0.1"]
              }
            }
          }
        }
      }'
    - role: mrlesmithjr.netplan
      netplan_enabled: true
      netplan_config_file: /etc/netplan/lokal-dhcp.yaml
      netplan_renderer: networkd
      netplan_configuration: '{
        "network": {
          "ethernets": {
            "{{ lokal_server_interface }}": {
              "dhcp4": true,
              "nameservers": {
                "addresses": ["1.1.1.1", "1.0.0.1"]
              }
            }
          }
        }
      }'

For context, I set the supplied environment variables in the following way:

export LOKAL_SERVER_INTERFACE=$(ip route show 0.0.0.0/0 | cut -d\  -f5 | uniq)
export LOKAL_SERVER_GATEWAY=$(ip route show 0.0.0.0/0 | cut -d\  -f3 | uniq)
export LOKAL_SERVER_IP=$(readarray -d . -t ip <<< ${LOKAL_SERVER_GATEWAY}; echo ${ip[0]}.${ip[1]}.${ip[2]}.242)

Option to not create backup of /etc/netplan yml file

Is your feature request related to a problem? Please describe.
I am running the Ansible playbook with this role daily using Semaphore (an Ansible GUI). Problem is that after 365 days I get 365 backup files which I do not want or need.

Describe the solution you'd like
I would be great if the backup option could be disabled:

Describe alternatives you've considered

Additional context

Thank you very much!

Support for passing omit in netplan_configuration.

Is your feature request related to a problem? Please describe.
I would like to be able to decide at run time if a section of the configuration is to be generated. Such as including the wifi: section on some hosts with access points defined but not on hosts that dont.

Describe the solution you'd like
Removing the is defined check from the template and evaluating the maps as true or false resolves this issue.

Describe alternatives you've considered
None

Change of IP causes play to hang

Hi. I'm changing the IP and during the role execution the play hangs and ultimately fails, however the IP is changed as expected.

In the example log below I changed the VMs IP from 98 to 97:

changed: [10.0.0.98]

RUNNING HANDLER [ansible-netplan : Generating Netplan Configuration] ********************************************************************************************************
changed: [10.0.0.98]

RUNNING HANDLER [ansible-netplan : Applying Netplan Configuration] **********************************************************************************************************
fatal: [10.0.0.98]: FAILED! => {"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.0.0.98 port 22: No route to host"}

NO MORE HOSTS LEFT **********************************************************************************************************************************************************

PLAY RECAP ******************************************************************************************************************************************************************
10.0.0.98                  : ok=4    changed=2    unreachable=0    failed=1    skipped=3    rescued=0    ignored=0   

Any thoughts on how to handle this? (excuse the pun)

Cheers,

Ben

If interface name is set as {{ ansible_default_ipv4.interface }} it just writes the variable name instead of the value

Describe the bug
Hello i tried your role and have problem with getting the network device name as a variable

 netplan_renderer: networkd
 netplan_configuration:
   network:
     version: 2
     ethernets:
        "{{ ansible_default_ipv4.interface }}":

These are my first variables, but if i do it like that it doesnt fill in the variable with the network name, instead it just writes the string '{{ ansible_default_ipv4.interface }}' into the config. Is that a Problem with jinja2 templates?

Thanks for your Help!
To Reproduce
Use a variable for the default ethernet-if name instead of an actual name.

Expected behavior
I expect it to write the default interface name into the config.

Parameterising the NIC device name

Any ideas how to parameterise the network device name? Eg, I have:

netplan_configuration:
   network:
      version: 2
      ethernets:
         eth0:
            addresses:
                - "{{ ip }}{{ sm }}"
              gateway4: "{{ gw }}"
              nameservers:
                search: "{{ search_domains }}"
                addresses: "{{ dns_servers }}"

I want:

netplan_configuration:
   network:
      version: 2
      ethernets:
         "{{ nic_device }}":
            addresses:
                - "{{ ip }}{{ sm }}"
              gateway4: "{{ gw }}"
              nameservers:
                search: "{{ search_domains }}"
                addresses: "{{ dns_servers }}"

But this errors:
Error in network definition: Invalid name '{{ nic_device }}

Create a backup

Describe the solution you'd like

It would be nice to have an option to create a backup before changing

And Thank for your awesome work!

Maintained-by-ansible comment

Is your feature request related to a problem? Please describe.
I'd like the possibility to ad a 'do not change this file' warning at the top of the generated netplan files.

Describe the solution you'd like
I would suggest just putting this snippet at the top of the template:

{% if netplan_managed_comment is not none %}
# {{ netplan_managed_comment }}
{% endif %}

By default this variable could either be empty, a reference to ansible_managed, or a custom string.
I implemented this idea in my personal fork already.

Describe alternatives you've considered
I could run a separate task after this role to add the comment, but that would lead to more cutter and filechanges.

Additional context
I think this feature is quite self-explanatory ๐Ÿ˜…

filter to_nice_yaml

Hi
Thanks for your ansible roles.

The filter to_nice_yaml used in template/etc/netplan/config.yaml, doesn't respect the order of the netplan_configuration because of dictionnay like it's said here.

when my netplan_configuration is like following :

netplan_configuration:
  network:
    version: 2
    rendered: networkd
  
    ethernets:
      ens2:
        addresses:
          - 192.168.122.61/24
        nameservers:
          - 127.0.0.53
        gateway4: 192.168.122.1
      ens6:
        dhcp4: no
      ens8:
        dhcp4: no
      ens9:
        dhcp4: no
      ens10:
        dhcp4: no
  
    bonds:
      bond0:
        addresses: 
          - 172.17.1.2/24
        interfaces:
          - ens6
          - ens10
        parameters:
          mode: active-backup
          primary: ens6
      data:
         interfaces:
           - ens8
         parameters:
           mode: active-backup
           primary: ens8
    bridges:
      mgmt: 
       interfaces:
         - ens9
  
    vlans:
      data.5:
        accept-ra: no
        id: 5
        link: data

when playbook is applied, the /etc/netplan/config.yaml is like this :

network:
    bonds:
        bond0:
            addresses:
            - 172.17.1.2/24
            interfaces:
            - ens6
            - ens10
            parameters:
                mode: active-backup
                primary: ens6
        data:
            interfaces:
            - ens8
            parameters:
                mode: active-backup
                primary: ens8
    bridges:
        mgmt:
            interfaces:
            - ens9
    ethernets:
        ens10:
            dhcp4: false
        ens2:
            addresses:
            - 192.168.122.61/24
            gateway4: 192.168.122.1
            nameservers:
            - 127.0.0.53
        ens6:
            dhcp4: false
        ens8:
            dhcp4: false
        ens9:
            dhcp4: false
    rendered: networkd
    version: 2
    vlans:
        data.5:
            accept-ra: false
            id: 5
            link: data

which makes netplan generates an error : Error in network definition //etc/netplan/config.yaml line 6 column 14: bond0: interface ens6 is not defined Because ethernet is not defined before bridge.

Do you encontered this issue ?

Provide a facility to skip the `netplan apply` call

There are situations when netplan apply needs to be run at the same time as another task. One specific example is when setting up BGP routing with FRR/Quagga. In this case, we would like to have this role setup the netplan config, but not run netplan apply. We will create a post-task in our playbook that will execute netplan apply && service frr restart in an async manner. But we need a facility to tell this role that we do not want it to run the apply after making changes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.