mrlesmithjr / ansible-netplan

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

Multiple DNS Servers as variable #33

Closed stefanpinter closed 9 months ago

stefanpinter commented 2 years ago

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

stefanpinter commented 2 years ago

ok, I did it like this - with two variables

cat group_vars/all.yaml
dns1: "208.67.222.222"
dns2: "208.67.220.220"

playbook:

"nameservers": {
                "addresses": [ "{{ dns1 }}","{{ dns2 }}" ]
              }

i guess this should be good enough for now

stefanpinter commented 2 years ago

ok, similar problem now... some of our servers have 1 IP addresses on the interface and some do have more so this works perfectly fine:

        netplan_configuration: '{
          "network": {
            "ethernets": {
              "{{ ipv4_default_interface }}": {
                "addresses": [ "{{ ipv4_default_address }}" ],
                "dhcp4": false,
                "gateway4": "{{ ipv4_default_gateway }}",
                "nameservers": {
                  "addresses": [ "{{ dns1 }}","{{ dns2 }}" ]
                }
              }
            }
          }
        }'

how can I add ipv4_additional_address1 and ipv4_additional_address2 to this variable? this is working of course, when both additional addresses are defined

 "addresses": [ "{{ ipv4_default_address }}", "{{ ipv4_additional_address1 }}", "{{ ipv4_additional_address2 }}" ],

but what if the host doesn't have additional addresses defined? I tried it with default(Omit) and default(None) but it does weird things

i feel dumb already, please help :D

stefanpinter commented 2 years ago

I restructured the thing now and add this block to every host_vars file and don't put it in the playbook. the only thing I don't like about it, is, that the nameservers have to be set in every host_vars file

netplan_configuration:
  network:
    ethernets:
      ens18:
        addresses:
          - 10.20.30.105/24
          - 10.20.30.115/24
        dhcp4: false
        routes:
        -  to: default
           via: 10.20.30.254
        nameservers:
          addresses:
            - 208.67.222.222
            - 208.67.220.220
stale[bot] commented 9 months ago

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.