mrlesmithjr / ansible-netplan

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

What if the interface is from a variable? #17

Closed zx1986 closed 9 months ago

zx1986 commented 4 years ago

https://github.com/mrlesmithjr/ansible-netplan/blob/a097c24e926507f3dc3a1527d290ee11af08c5a4/defaults/main.yml#L16

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

zx1986 commented 4 years ago

the workaround not working: https://github.com/ansible/ansible/issues/17324

:-(

refs: https://stackoverflow.com/questions/42555663/use-yaml-variables-to-name-keys

mrlesmithjr commented 4 years ago

@zx1986 Sorry for the late response here. But did you ever get this sorted out? I haven't personally tried this in this way.

ericsysmin commented 4 years ago

Does using "{{ }}": also fail?

holmboe commented 4 years ago

@ericsysmin yes it does.

It seems the problem is that Ansible cannot have a key as a variable.

key: value

key cannot be a variable, but value works fine.

Heniland commented 4 years ago

Hi, If you define the variable netplan_configuration in a JSON format it works. Check the template folder, the jinja2 template will convert it to YAML using to_nice_yaml data formatter.

here is a sample code that works from me.

netplan_configuration: '{
      "network": {
        "ethernets": {
          "{{ target_interface }}": {
            "addresses": [
                "{{ target_ip }}/24"
            ],
            "dhcp4": false,
            "gateway4": "{{ gateway_ip }}",
            "nameservers": {
              "addresses": [
                  "{{ gateway_ip }}"
              ]
            }
          }
       }
    }
}'
przemas75 commented 4 years ago

If anyone find it useful, here is netplan with vlan via workaround from @Heniland 👍

- hosts: masters
  any_errors_fatal: true
  roles:
    - role: mrlesmithjr.netplan
      become: yes
      netplan_enabled: true
      netplan_config_file: /etc/netplan/vlan8-vlan15-netplan.yaml
      netplan_configuration: '{
        "network": {
          "ethernets": {
            "{{ targetdev }}": {
              "addresses": [ "{{ targetip }}/24" ],
              "dhcp4": false,
              "gateway4": "172.16.8.1",
              "nameservers": {
                "addresses": [ "172.16.8.7" ]
              }
            }
          },
          "vlans": {
            "vlan{{ vlanid }}": {
              "accept-ra": "no",
              "id": "15",
              "link": "{{ targetdev }}",
              "addresses": [ "{{ k8sipt }}/{{ nprefix }}" ]
            }
          }
        }
      }'
Spirit-act commented 1 year ago

I stumbled upon this issue today and I had the same problem.

I have forked the Repo for me and added a workaround, you can see it here: https://github.com/delta-one-net/ansible-netplan/blob/master/templates/etc/netplan/config.yaml.j2#L9

If @mrlesmithjr considers this a valid solution, I'll be honored to create a pull request.

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.