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

[Question] How to make DHCP lease entry as static #336

Open
Nerus87 opened this issue Dec 29, 2024 · 6 comments
Open

[Question] How to make DHCP lease entry as static #336

Nerus87 opened this issue Dec 29, 2024 · 6 comments

Comments

@Nerus87
Copy link

Nerus87 commented Dec 29, 2024

As in the thread, how make an entry as a static DHCP lease by using API calls from Anisble?

Path: /ip/dhcp-server/lease -> make-static

@Nerus87 Nerus87 changed the title [Question] How to make DNS entry as static [Question] How to make DHCP lesse entry as static Dec 29, 2024
@Nerus87 Nerus87 changed the title [Question] How to make DHCP lesse entry as static [Question] How to make DHCP lease entry as static Dec 29, 2024
@felixfontein
Copy link
Collaborator

You should be able to use the api_info module (https://docs.ansible.com/ansible/devel/collections/community/routeros/api_info_module.html with include_dynamic=true) and some Jinja to find the id of the entry you want to convert, and then use the api module (https://docs.ansible.com/ansible/devel/collections/community/routeros/api_module.html) to call make-static for that entry.

@Nerus87
Copy link
Author

Nerus87 commented Dec 30, 2024

I was able to find an entry by the host-name but only via SSH, in the current API host-name not existing.

#- name: Get {{ container_name }} mac-address
#  community.network.routeros_command:
#    commands: :put [/ip/dhcp-server/lease/ get ([find where host-name=srv-test]) mac-address]
#    register: mac_address
#  when: ansible_network_os == 'community.network.routeros'

@felixfontein
Copy link
Collaborator

Have you tried the unfiltered option of community.routeros.api_info?

@Nerus87
Copy link
Author

Nerus87 commented Dec 31, 2024

Have you tried the unfiltered option of community.routeros.api_info?

Yeah, this was a reason, sorry for not responding asap but still fighting with scripting.
I was able to find it based on host-name (it was a mess and I spent the whole day to find the reason).
So if you even have a problem with dashes in names use this site to check your query and save my time: https://mixedanalytics.com/tools/jmespath-expression-tester/

- name: Get DHCP lease list
  community.routeros.api_info:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: ip dhcp-server lease
    include_dynamic: true
    unfiltered: true
    hide_defaults: false
  register: leases

- name: Filter leases list and set lease id
  set_fact:
    lease_id: "{{ leases.result | json_query(query) }}"
  vars:
    query: "[?\"host-name\"== '{{ container_name }}'].\".id\""

I found a way how to change but it is not an "idempotent" (ansible detecting change even if it wasn't) way:

- name: Make DHCP lease static
  community.routeros.api:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: "ip dhcp-server lease"
    cmd: "make-static numbers={{ lease_id }}"

- name: Set DHCP lease to static and set IP with time
  community.routeros.api_find_and_modify:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: ip dhcp-server lease
    find:
      host-name: '{{ container_name }}'
    values:
      address: '{{ address }}'
      lease-time: '{{ dhcp_lease_time }}'

@felixfontein
Copy link
Collaborator

Why don't you use the restrict option of community.routeros.api_info to only return the entries where host-name equals container_name?

- name: Get DHCP lease list
  community.routeros.api_info:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: ip dhcp-server lease
    include_dynamic: true
    unfiltered: true
    hide_defaults: false
    restrict:
      - field: host-name
        values:
          - "{{ container_name }}"
  register: leases

Regarding the api task: why don't you only run it when the entry is actually dynamic?

- name: Make DHCP lease static
  community.routeros.api:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: "ip dhcp-server lease"
    cmd: "make-static numbers={{ leases.result[0]['.id'] }}"
  when: leases.result[0].dynamic

@Nerus87
Copy link
Author

Nerus87 commented Jan 1, 2025

Why don't you use the restrict option of community.routeros.api_info to only return the entries where host-name equals container_name?

- name: Get DHCP lease list
  community.routeros.api_info:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: ip dhcp-server lease
    include_dynamic: true
    unfiltered: true
    hide_defaults: false
    restrict:
      - field: host-name
        values:
          - "{{ container_name }}"
  register: leases

"msg": "restrict: the field "host-name" does not exist for this path"
As I understand this was not a part of 'ip dhcp-server lease'

Regarding the api task: why don't you only run it when the entry is actually dynamic?

- name: Make DHCP lease static
  community.routeros.api:
    hostname: "{{ hostname }}"
    password: "{{ password }}"
    username: "{{ username }}"
    path: "ip dhcp-server lease"
    cmd: "make-static numbers={{ leases.result[0]['.id'] }}"
  when: leases.result[0].dynamic

I missed the dynamic field, thanks for pointing it out.

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