ansible-netplan icon indicating copy to clipboard operation
ansible-netplan copied to clipboard

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

Open dnk8n opened this issue 3 years ago • 2 comments

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)

dnk8n avatar Nov 02 '21 11:11 dnk8n

Maybe I am mistaken. But my understanding is that if I define a static network that does not fall within the private IP range of the connected router I can be cut off from the internet. This is why I keep an additional dhcp4 supplied connection, then use that route to determine the range my private ip should exist on.

If I should switch back to a previously setup network, then I wouldn't have need to rerun the playbook (if my expectations are correct).

Currently because everything is overwritten in one file, I would need to rerun to reconfigure to any network with different private IP range.

I am sorry if I am not speaking in the correct terms, I am new to this subject area.

dnk8n avatar Nov 02 '21 11:11 dnk8n

I think there is a lot wrong with my previous conversation. To summarize. I have a VM running services that other users connected to the same network should access. However often this network can change. My hope is to find a solution where I can run a script (I was hoping this module would be useful here) to reconfigure the network to create a static IP at a predefined location (without disrupting outward connection).

i.e. If I am on my hotspot for internet, the correct range is 192.168.43.0/24 and when I am on home network, the correct range is 192.168.0.0/24. I would want to configure static IPs at 192.168.43.242 and 192.168.0.242 respectively (depending on the underlying connection, manually rerunning a netplan upon change of network if necessary). Hope that makes sense.

dnk8n avatar Nov 03 '21 13:11 dnk8n

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 14 '24 09:01 stale[bot]