mrlesmithjr / ansible-netplan

Ansible role to manage Netplan
MIT License
132 stars 85 forks source link

Parameterising the NIC device name #15

Closed holmesb closed 5 years ago

holmesb commented 5 years ago

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 }}

mrlesmithjr commented 5 years ago

@holmesb That route will definitely not work but you COULD do something like below:

dns_servers:
  - 192.168.250.10
  - 192.168.250.11
netplan_configuration:
  network:
    version: 2
    ethernets: "{{ nics }}"
nics:
  eth0:
    addresses:
      - 192.168.250.10
    gateway4: 192.168.250.1
    nameservers:
      search: "{{ search_domains }}"
      addresses: "{{ dns_servers }}"
search_domains:
  - test.example.org
  - example.org

Hopefully this will help you a bit. Let me know if you have any additional questions.

holmesb commented 5 years ago

I've changed to:

netplan_configuration:
  network:
    version: 2
    ethernets: 
       nic_device:
         addresses:
          - "{{ primary_ip }}{{ primary_subnet_mask }}"
         gateway4: "{{ primary_gateway }}"
        ...
nic_device: eth0

And your template to:

  ethernets:
{{ nic_device|indent(4, true) }}:
{{ netplan_configuration['network']['ethernets']['nic_device']|to_nice_yaml|indent(6, true) }}

Many thanks

mrlesmithjr commented 5 years ago

@holmesb Glad you were able to get it sorted out!