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

Add default gateway functionnality #99

Merged
merged 1 commit into from
Dec 31, 2023

Conversation

freeeflyer
Copy link

No description provided.

@freeeflyer freeeflyer changed the title Add defautl gateway functionnality Add default gateway functionnality Dec 20, 2023
@freeeflyer
Copy link
Author

Hi.
This PR adds the possibility to select the default gateway.

internet_gw

Here is my test playbook

#!/usr/bin/env ansible-playbook
- name: "pfSense test"
  hosts: pfsense


  tasks:
    - name: Debug coucou
      ansible.builtin.debug:
        msg: "test coucou"
      tags: never
        
  ############# READ
    - name: Read default GW 1
      pfsensible.core.pfsense_default_gateway:
        state: "read"
      register: out

    - name: Show default GW 1
      ansible.builtin.debug:
        msg: "defaultgw4:  {{ out.defaultgw4 }} |  defaultgw6:  {{ out.defaultgw6 }} |  debug:  {{ out.debug }}"

  ############# REMOVE
    - name: Remove default GW
      pfsensible.core.pfsense_default_gateway:
        state: "absent"
      register: out

    - name: Show output of remove GW 
      ansible.builtin.debug:
        var: out.commands

    - name: Read default GW 2
      pfsensible.core.pfsense_default_gateway:
        state: "read"
      register: out

    - name: Show default GW 2
      ansible.builtin.debug:
        msg: |
          defaultgw4:  {{ out.defaultgw4 }}
          defaultgw6:  {{ out.defaultgw6 }}

    - name: Pause
      ansible.builtin.pause:
        prompt: "Press enter to continue"

  ############# AUTOMATIC
    - name: Set default GW to Automatic
      pfsensible.core.pfsense_default_gateway:
        state: "present"
        name: "Automatic"

    - name: Read default GW 3
      pfsensible.core.pfsense_default_gateway:
        state: "read"
      register: out

    - name: Show default GW 3
      ansible.builtin.debug:
        msg: |
          defaultgw4:  {{ out.defaultgw4 }}
          defaultgw6:  {{ out.defaultgw6 }}

    - name: Pause
      ansible.builtin.pause:
        prompt: "Press enter to continue"

  ############# INTERNET_GW
    - name: Set default GW to INTERNET_GW
      pfsensible.core.pfsense_default_gateway:
        state: "present"
        name: "INTERNET_GW"
      register: out

    - name: Show output of set GW 
      ansible.builtin.debug:
        var: out.commands

    - name: Read default GW 3
      pfsensible.core.pfsense_default_gateway:
        state: "read"
      register: out

    - name: Show default GW 3
      ansible.builtin.debug:
        msg: |
          defaultgw4:  {{ out.defaultgw4 }}
          defaultgw6:  {{ out.defaultgw6 }}

    - name: Pause
      ansible.builtin.pause:
        prompt: "Press enter to continue"

  ############# Bogus gateway should fail
    - name: Set default GW to Bogus
      pfsensible.core.pfsense_default_gateway:
        state: "present"
        name: "BOGUS_GW"

I hope it helps.

Regards;

Nicolas

Copy link
Contributor

@opoplawski opoplawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the submission. Can you address the test failures and other review comments. Thanks.

plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
@opoplawski opoplawski added the enhancement New feature or request label Dec 23, 2023
@opoplawski opoplawski added this to the 0.6.0 milestone Dec 23, 2023
@freeeflyer
Copy link
Author

freeeflyer commented Dec 23, 2023

Sorry for the mistakes.. I don't PR often..

@opoplawski
Copy link
Contributor

Sorry for the mistakes.. I don't PR often..

No need to apologize, everyone has to start somewhere. Thank you for working on this.

plugins/module_utils/default_gateway.py Outdated Show resolved Hide resolved
plugins/module_utils/default_gateway.py Outdated Show resolved Hide resolved
Comment on lines 38 to 39
choices: [ "present", "absent", "read" ]
default: read
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, read is new, and not something I've seen used in ansible modules before. Can you point to any similar usage?

I'm also leaning more towards simply dropping state altogether and just using the None option to express that.

Finally, we generally go with lowercase options so lets use automatic and none.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted that too.. but what to do if you just want the current default gateway ?
Also I have to rework my code. I just realized there was an issue with dynamic gateways..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's just not really how ansible works. You declare the state that you want. If you don't want to change anything, don't use this task.

Copy link
Author

@freeeflyer freeeflyer Dec 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I get it right..

  • No more "state"
  • just "name" with "none" for absent
  • no read
  • lowercase

I could implement the fact that it accepts no option at all and return both v4 and v6 default gateway..

I implemented a find_default_gateway method because I needed it.. I feel it would be better placed in the pfsense class .. thus I did it.

Ok done !

Copy link
Contributor

@opoplawski opoplawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checks also found some trailing whitespace to clean up. Otherwise I think we are close. Thanks for working on this.

plugins/module_utils/default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Outdated Show resolved Hide resolved
plugins/module_utils/default_gateway.py Outdated Show resolved Hide resolved
plugins/modules/pfsense_default_gateway.py Show resolved Hide resolved
Copy link
Contributor

@opoplawski opoplawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I have a couple fixes to add and then I'll merge. Thanks.

@freeeflyer
Copy link
Author

Hello. Thanks.
What will you fix ? I'm curious :)
Happy new year !

@opoplawski
Copy link
Contributor

Change "inet4" to "inet" and remove the unused debug return.

@opoplawski opoplawski merged commit 20e6831 into pfsensible:master Dec 31, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants