-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
verify config: Playbook verify_config.yml included in site.yml #385
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe4407d
Added verify_config.yml playbook and included it in site.yml, Updated…
13deec8
fixed newline
e3a95bb
removed file that should have been ignored, and fixed .gitignore
5334dda
Merge branch 'master' into verify
edoziw 658802d
Merge branch 'master' into verify
timothystewart6 1d26a37
Merge branch 'master' into verify
timothystewart6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env/ | ||
*.log | ||
ansible.cfg | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,5 @@ custom_registries_yaml: | | |
auth: | ||
username: yourusername | ||
password: yourpassword | ||
|
||
verify_config: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
# To be ran on localhost after verify_config_gather role. | ||
# Verifies gatherd facts. | ||
- name: Collect all routing4_prefixes into a list | ||
set_fact: | ||
all_routing4_prefixes: "{{ groups['all'] | map('extract', hostvars, 'routing4_prefix') | list }}" | ||
routing4_prefix: "{{ groups['all'] | map('extract', hostvars, 'routing4_prefix') | list | first }}" | ||
|
||
- name: Ensure all hosts have the same routing4_prefix | ||
assert: | ||
that: all_routing4_prefixes | unique | length == 1 | ||
fail_msg: "Not all hosts have the same routing4_prefix." | ||
success_msg: "Using verified routing prefix {{ routing4_prefix }} across all hosts" |
Binary file added
BIN
+1.65 KB
roles/verify_config_gather/filter_plugins/__pycache__/range_to_ips.cpython-310.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import netaddr | ||
|
||
''' returns True if ip is in range | ||
examples: | ||
- see test functions below | ||
''' | ||
def test(ip, range, expected): | ||
assert netaddr_ip_in_dash_range(ip, range) == expected | ||
|
||
def test_netaddr_ip_in_dash_range(): | ||
# ipv4 | ||
test('192.168.1.1', '192.168.1.1-192.168.1.2', True) | ||
test('192.168.1.1', '192.168.1.1', True) | ||
test('192.168.1.1', '192.168.1.2-192.168.1.3', False) | ||
test('192.168.1.1', '192.168.1.2', False) | ||
|
||
# ipv6 style | ||
test('::ffff:192.168.1.1', '::ffff:192.168.1.1-::ffff:192.168.1.8', True) | ||
test('::ffff:192.168.1.1', '::ffff:192.168.1.1', True) | ||
test('::ffff:192.168.1.1', '::ffff:192.168.1.2-::ffff:192.168.1.8', False) | ||
test('::ffff:192.168.1.1', '::ffff:192.168.1.2', False) | ||
|
||
# Note I expedted true but apperently the netaddr library does not support this?? or I don't understand ipv6 :) | ||
test('::2:1', '::2:1-::2:2', False) | ||
''' | ||
todo: ? | ||
- netaddr_ip_in_dash_range('192.168.1.1', '192.168.1.0/24') => True (TODO: test, implement) | ||
- netaddr_ip_in_dash_range('192.168.99.1', '192.168.1.0/24') => False (TODO: test, implement) | ||
''' | ||
|
||
def netaddr_ip_in_dash_range(ip, range): | ||
# return False early if range is invalid | ||
if '-' not in range: | ||
ip_start = range | ||
ip_end = range | ||
else: | ||
ip_start = range.split('-')[0] | ||
ip_end = range.split('-')[1] | ||
return ip in [str(ip) for ip in netaddr.iter_iprange(ip_start, ip_end)] | ||
|
||
|
||
class FilterModule(object): | ||
''' Ansible filters. Interface to custom netaddr methods. | ||
https://pypi.org/project/netaddr/ | ||
''' | ||
|
||
def filters(self): | ||
return { | ||
'netaddr_ip_in_dash_range' : netaddr_ip_in_dash_range | ||
} | ||
|
||
if __name__ == '__main__': | ||
test_netaddr_ip_in_dash_range() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
--- | ||
# To be ran on all hosts before role verify_config. | ||
# Gathers facts and ensures they are set | ||
|
||
- name: Set routing4_prefix from regex | ||
set_fact: | ||
routing4_prefix: "{{ hostvars[inventory_hostname]['ansible_' ~ flannel_iface]['ipv4']['broadcast'] | ||
| regex_replace('\\.?255', '') }}" | ||
routing6_cidr: "{{ hostvars[inventory_hostname]['ansible_' ~ flannel_iface]['ipv6'][0]['address'] }}/{{ | ||
hostvars[inventory_hostname]['ansible_' ~ flannel_iface]['ipv6'][0]['prefix'] }}" | ||
|
||
- name: Check if fact routing4_prefix exists and is not empty | ||
assert: | ||
that: | ||
- routing4_prefix is defined | ||
- routing4_prefix is not none | ||
- routing4_prefix != '' | ||
fail_msg: >- | ||
The fact 'routing_4prefix' is not defined, is null, or is empty | ||
(based on flannel_iface: {{ flannel_iface }} ipv4 broadcast). | ||
|
||
# metal_lb_bgp_peer_address | ||
- name: Assert that metal_lb_bgp_peer_address starts with routing4_prefix, or is ipv6 | ||
assert: | ||
that: | ||
- > | ||
metal_lb_bgp_peer_address.startswith(routing4_prefix) | ||
or metal_lb_bgp_peer_address.matches('.*:.*') | ||
fail_msg: > | ||
The fact 'metal_lb_bgp_peer_address' <{{ metal_lb_bgp_peer_address }}> | ||
doesn't start with the routing prefix <{{ routing4_prefix }}> | ||
when: metal_lb_bgp_peer_address is defined | ||
|
||
# metal_lb_ip_range | ||
- name: > | ||
Assert that metal_lb_ip_range (when string) contains <ipv4>-<ipv4> | ||
and both ips start with routing4_prefix, skip any containing ':' (ipv6) | ||
assert: | ||
that: | ||
- metal_lb_ip_range | regex_search('^{{ routing4_prefix }}\.[0-9]{1,3}-{{ routing4_prefix }}\.[0-9]{1,3}$|.*:.*') | ||
fail_msg: > | ||
metal_lb_ip_range <{{ metal_lb_ip_range }}> has one or more ipv4s | ||
that don't start with the routing prefix <{{ routing4_prefix }}> | ||
when: metal_lb_ip_range is string | ||
|
||
- name: Assert that metal_lb_ip_ranges (when list) has only strings that match the regexes in the task above | ||
assert: | ||
that: | ||
- > | ||
( metal_lb_ip_range | ||
| select('match', '^{{ routing4_prefix }}\.[0-9]{1,3}-{{ routing4_prefix }}\.[0-9]{1,3}$|.*:.*') | ||
| list | length | ||
) | ||
== | ||
(metal_lb_ip_range | ||
| length | ||
) | ||
fail_msg: > | ||
metal_lb_ip_range <{{ metal_lb_ip_range }}> has one or more values with ipv4s | ||
that don't start with the routing prefix <{{ routing4_prefix }}> | ||
when: metal_lb_ip_range is not string and metal_lb_ip_range is not mapping and metal_lb_ip_range is iterable | ||
|
||
# apiserver_endpoint | ||
- name: Assert that apiserver_endpoint is not in metal_lb_ip_range (when string) using network_in_usable | ||
# For /<mask> ranges | ||
assert: | ||
that: | ||
- not ( metal_lb_ip_range | ansible.utils.network_in_usable( apiserver_endpoint )) | ||
fail_msg: "apiserver_endpoint {{ apiserver_endpoint }} cannot be in the metal_lb_ip_range {{ metal_lb_ip_range }}" | ||
success_msg: > | ||
apiserver_endpoint {{ apiserver_endpoint }} is *probably* not in the metal_lb_ip_range {{ metal_lb_ip_range }} | ||
when: metal_lb_ip_range is string | ||
|
||
- name: > | ||
Assert that apiserver_endpoint is not in metal_lb_ip_range (when string) | ||
using custom filter netaddr_ip_in_dash_range | ||
# For <ip>-<ip> ranges. Not sure this works for ipv6 | ||
assert: | ||
that: | ||
- not (apiserver_endpoint | netaddr_ip_in_dash_range(metal_lb_ip_range)) | ||
fail_msg: "apiserver_endpoint {{ apiserver_endpoint }} cannot be in the metal_lb_ip_range {{ metal_lb_ip_range }}" | ||
success_msg: > | ||
apiserver_endpoint {{ apiserver_endpoint }} is *probably* not in the metal_lb_ip_range {{ metal_lb_ip_range }} | ||
when: metal_lb_ip_range is string | ||
# *probably* in the success_msg sections of the prior two tasks because not all cases may work | ||
|
||
- name: Assert that apiserver_endpoint is not in metal_lb_ip_ranges (when list) using logic of the task above | ||
assert: | ||
that: | ||
- not (apiserver_endpoint | netaddr_ip_in_dash_range(item)) | ||
# this probably fails on an ipv6 range ''::1-::2', and on <ipv4or6>/<mask> ranges | ||
fail_msg: "apiserver_endpoint {{ apiserver_endpoint }} cannot be in the metal_lb_ip_range item {{ item }}" | ||
success_msg: > | ||
apiserver_endpoint {{ apiserver_endpoint }} is *probably* not in the metal_lb_ip_range item {{ item }} | ||
loop: "{{ metal_lb_ip_range | list }}" | ||
when: metal_lb_ip_range is not string and metal_lb_ip_range is not mapping and metal_lb_ip_range is iterable | ||
# these (when string, when list tasks) smell funny. | ||
# It seems there should be a way in one task to handle an object that is a string or a list | ||
# and loop on {{ [metal_lb_ip_range] }} or {{ metal_lb_ip_range }} respectively | ||
# when it is a string it skips each char :( | ||
# I tried the select pattern like in the task | ||
# 'assert that metal_lb_ip_ranges (when list) has only strings that match the regexes in the task above' | ||
# but it didn't work | ||
|
||
- name: Assert that apiserver_endpoint, is ipv4 and starts with routing4_prefix, or is ipv6 and is in routing6_cidr | ||
assert: | ||
that: | ||
- apiserver_endpoint is defined | ||
- apiserver_endpoint is not none | ||
- >- | ||
( apiserver_endpoint | ansible.utils.ipv4 and apiserver_endpoint.startswith(routing4_prefix) ) | ||
or | ||
( apiserver_endpoint | ansible.utils.ipv6 and apiserver_endpoint | ansible.utils.ipaddr(routing6_cidr)) | ||
fail_msg: > | ||
The fact 'apiserver_endpoint' <{{ apiserver_endpoint }}> | ||
doesn't start with the routing prefix <{{ routing4_prefix }}> | ||
or is not in the routing6_cidr <{{ routing6_cidr }}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
- name: Gather config for verify | ||
hosts: all | ||
gather_facts: true | ||
roles: | ||
- role: verify_config_gather | ||
when: verify_config is not defined or verify_config | ||
|
||
- name: Verify config | ||
hosts: localhost | ||
gather_facts: false | ||
roles: | ||
- role: verify_config | ||
when: verify_config is not defined or verify_config |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to include this binary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, I missed that, I thought
__pycache__
was in the .gitignoreI'll fix .gitignore and also
git rm --cached <the file>
then pushThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ok now.
Sorry about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!