Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dhcpd.conf template does not render #48

Closed
ahgraber opened this issue Feb 21, 2022 · 9 comments
Closed

dhcpd.conf template does not render #48

ahgraber opened this issue Feb 21, 2022 · 9 comments

Comments

@ahgraber
Copy link

ahgraber commented Feb 21, 2022

I'm working through your netboot solution, and I have an interesting issue where dhcpd.conf.j2 does not render properly (no variables are substituted). All of the other templates work just fine, and the variables used in dhcpd.conf.j2 are available if I run a debug step:

# all variables are present with correct values
- debug:
    var: '{{item}}'
  with_items:
    - ansible_default_ipv4.address
    - ansible_default_ipv4.broadcast
    - ansible_default_ipv4.gateway
    - ansible_default_ipv4.netmask
    - ansible_default_ipv4.network

# no substitution occurs
- name: Render DHCP config
  template:
    src: dhcpd.conf.j2
    dest: "{{ role_path }}/files/dhcp/dhcpd.conf"
    mode: 0644

# substitution occurs correctly
- name: Render GRUB config
  template:
    src: grub.cfg.j2
    dest: "{{ role_path }}/files/data/boot/grub/grub.cfg"
    mode: 0644

Do you have any thoughts?

@khuedoan
Copy link
Owner

khuedoan commented Feb 22, 2022

What is the path of your DHCP config file, is it metal/roles/pxe_server/files/data/pxe-config/dhcpd.conf, can you post the full file in that path?

Also can you post a link to your code, I don't see it on your profile.

@ahgraber
Copy link
Author

Full path is as you expected metal/roles/pxe_server/files/data/pxe-config/dhcpd.conf

Hadn't pushed my code previously as I was just trying to get it to work. Just pushed to metal branch

A bit more background -- previously I had been trying to run this on an Ubuntu VM I'm using as a jump box so it's in the same subnet as my lab. The dhcpd.conf does not render on the VM. However, if I try on my local machine, it does render correctly. It's weird because the grub.cfg template works fine on the VM.
Is there a dependency I might be missing on the VM?

dhcpd.conf with missing substitutions:

subnet {{ ansible_default_ipv4.network }} netmask {{ ansible_default_ipv4.netmask }} {
    option routers {{ ansible_default_ipv4.gateway }};
    range {{ ansible_default_ipv4.network | ansible.netcommon.ipmath(1) }} {{ ansible_default_ipv4.broadcast | ansible.netcommon.ipmath(-1) }};

    class "pxeclients" {
        match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
        next-server {{ ansible_default_ipv4.address }};

        if option architecture-type = 00:07 {
            filename "grubx64.efi";
        }
    }
}

grub.cfg with successful substitutions:

menuentry 'ubuntu-20.04.3-live-server-amd64 (autoinstall)' --id=autoinstall {
    echo "Loading Kernel..."
    linux vmlinuz \
        ip=dhcp \
        url=http://10.2.1.100/iso/ubuntu-20.04.3-live-server-amd64.iso autoinstall \
        ds="nocloud-net;seedfrom=http://10.2.1.100/" root=/dev/ram0 cloud-config-url=/dev/null
    echo "Loading Ram Disk..."
    initrd initrd
}

@khuedoan
Copy link
Owner

Does running the playbook with -vvv show any other related info?

@khuedoan
Copy link
Owner

It also renders fine on my machine.

@ahgraber
Copy link
Author

Does running the playbook with -vvv show any other related info?

Not really. I compared the task for dhcpd.conf to the task for grub.cfg and the logs look pretty much identical aside from the different paths.

@khuedoan
Copy link
Owner

run this on an Ubuntu VM I'm using as a jump box

Is it a clean VM? I can't replicate the issue so the VM might be the problem.

@ahgraber
Copy link
Author

Is it a clean VM?

That's a fair point. I can definitely try a different one.

A side question --
My lab subnet is 10.2.0.0/16. I have my router set up as a DHCP server for the subnet, but only service addresses in range 10.2.1.100 - 10.2.1.200.
Will running the additional dhcp server for a different IP range (10.2.118.0/24) in the lab subnet cause problems? If so, how would you advise setting things up?

Thanks for your time!

@khuedoan
Copy link
Owner

The current setup is tested on a flat 192.168.1.0/24 network, which is the most common case, but you can always customize the parameters of the DHCP config if needed. If you worry about the DHCP server you can add a handler for Ansible to turn off the PXE server after the machines are booted (in my case the IP range is the same with my router so it doesn't matter much)

@khuedoan
Copy link
Owner

Seem like you got it working ahgraber/homelab-infra@426ff06, closing.

zanehala added a commit to zanehala/homelab that referenced this issue Mar 25, 2023
retX0 pushed a commit to retX0/homelab that referenced this issue Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants