collection 교체

This commit is contained in:
정훈 변
2024-02-23 16:37:40 +09:00
parent b494779b5b
commit 3fd554eee9
38862 changed files with 220204 additions and 6600073 deletions

View File

@@ -9,35 +9,36 @@ The get_path lookup plugin
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: get_path
author: Bradley Thornton (@cidrblock)
plugin_type: lookup
version_added: "1.0.0"
short_description: Retrieve the value in a variable using a path
description:
- Use a I(path) to retrieve a nested value from a I(var)
- B(get_path) is also available as a B(filter plugin) for convenience
- Using the parameters below- C(lookup('ansible.utils.get_path', var, path, wantlist))
- Use a I(path) to retrieve a nested value from a I(var)
- B(get_path) is also available as a B(filter plugin) for convenience
- Using the parameters below- C(lookup('ansible.utils.get_path', var, path, wantlist))
options:
var:
description: The variable from which the value should be extracted.
description:
- The variable from which the value should be extracted.
type: raw
required: True
path:
description: >
The I(path) in the I(var) to retrieve the value of.
The I(path) needs to a be a valid jinja path.
description:
- The I(path) in the I(var) to retrieve the value of.
- The I(path) needs to a be a valid jinja path.
type: str
required: True
wantlist:
description: >
If set to C(True), the return value will always be a list.
This can also be accomplished using C(query) or C(q) instead of C(lookup).
U(https://docs.ansible.com/ansible/latest/plugins/lookup.html).
description:
- If set to C(True), the return value will always be a list.
- This can also be accomplished using C(query) or C(q) instead of C(lookup).
- U(https://docs.ansible.com/ansible/latest/plugins/lookup.html).
type: bool
notes:
@@ -49,11 +50,11 @@ EXAMPLES = r"""
b:
c:
d:
- 0
- 1
- 0
- 1
e:
- True
- False
- true
- false
- name: Retrieve a value deep inside a using a path
ansible.builtin.set_fact:
@@ -133,9 +134,9 @@ EXAMPLES = r"""
rekeyed:
by_name: "{{ interfaces.gathered|ansible.builtin.rekey_on_member('name') }}"
loop:
- by_name['Ethernet1/1'].description
- by_name['Ethernet1/2'].description|upper
- by_name['Ethernet1/3'].description|default('')
- by_name['Ethernet1/1'].description
- by_name['Ethernet1/2'].description|upper
- by_name['Ethernet1/3'].description|default('')
# TASK [Get the description of several interfaces] ******************
@@ -145,7 +146,6 @@ EXAMPLES = r"""
# msg: CONFIGURED BY ANSIBLE
# ok: [nxos101] => (item=by_name['Ethernet1/3'].description|default('')) => changed=false
# msg: ''
"""
RETURN = """
@@ -157,12 +157,11 @@ RETURN = """
from ansible.errors import AnsibleLookupError
from ansible.plugins.lookup import LookupBase
from ansible_collections.ansible.utils.plugins.module_utils.common.get_path import (
get_path,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
AnsibleArgSpecValidator,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.get_path import get_path
class LookupModule(LookupBase):
@@ -171,9 +170,9 @@ class LookupModule(LookupBase):
keys = ["var", "path"]
terms = dict(zip(keys, terms))
terms.update(kwargs)
aav = AnsibleArgSpecValidator(
data=terms, schema=DOCUMENTATION, name="get_path"
)
schema = [v for k, v in globals().items() if k.lower() == "documentation"]
aav = AnsibleArgSpecValidator(data=terms, schema=schema[0], name="get_path")
valid, errors, updated_data = aav.validate()
if not valid:
raise AnsibleLookupError(errors)

View File

@@ -9,53 +9,56 @@ The index_of lookup plugin
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: index_of
author: Bradley Thornton (@cidrblock)
plugin_type: lookup
version_added: "1.0.0"
short_description: Find the indices of items in a list matching some criteria
description:
- This plugin returns the indices of items matching some criteria in a list.
- When working with a list of dictionaries, the key to evaluate can be specified.
- B(index_of) is also available as a B(filter plugin) for convenience.
- Using the parameters below- C(lookup('ansible.utils.index_of', data, test, value, key, fail_on_missing, wantlist)).
- This plugin returns the indices of items matching some criteria in a list.
- When working with a list of dictionaries, the key to evaluate can be specified.
- B(index_of) is also available as a B(filter plugin) for convenience.
- Using the parameters below- C(lookup('ansible.utils.index_of', data, test, value, key, fail_on_missing, wantlist)).
options:
data:
description: A list of items to enumerate and test against.
description:
- A list of items to enumerate and test against.
type: list
elements: raw
required: True
test:
description: >
The name of the test to run against the list, a valid jinja2 test or ansible test plugin.
Jinja2 includes the following tests U(http://jinja.palletsprojects.com/templates/#builtin-tests).
An overview of tests included in ansible U(https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html).
description:
- The name of the test to run against the list, a valid jinja2 test or ansible test plugin.
- Jinja2 includes the following tests U(http://jinja.palletsprojects.com/templates/#builtin-tests).
- An overview of tests included in ansible U(https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html).
type: str
required: True
value:
description: >
The value used to test each list item against.
Not required for simple tests (eg: C(true), C(false), C(even), C(odd))
May be a C(string), C(boolean), C(number), C(regular expression) C(dict) and so on, depending on the B(test) used.
description:
- The value used to test each list item against.
- Not required for simple tests (e.g. C(true), C(false), C(even), C(odd))
- May be a C(string), C(boolean), C(number), C(regular expression) C(dict) and so on, depending on the B(test) used.
type: raw
key:
description: >
When the data provided is a list of dictionaries, run the test against this dictionary key.
When using a I(key), the I(data) must only contain dictionaries.
See I(fail_on_missing) below to determine the behaviour when the I(key) is missing from a dictionary in the I(data).
description:
- When the data provided is a list of dictionaries, run the test against this dictionary key.
- When using a I(key), the I(data) must only contain dictionaries.
- See I(fail_on_missing) below to determine the behaviour when the I(key) is missing from a dictionary in the I(data).
type: str
fail_on_missing:
description: When provided a list of dictionaries, fail if the key is missing from one or more of the dictionaries.
description:
- When provided a list of dictionaries, fail if the key is missing from one or more of the dictionaries.
type: bool
wantlist:
description: >
When only a single entry in the I(data) is matched, the index of that entry is returned as an integer.
If set to C(True), the return value will always be a list, even if only a single entry is matched.
This can also be accomplished using C(query) or C(q) instead of C(lookup).
U(https://docs.ansible.com/ansible/latest/plugins/lookup.html)
description:
- When only a single entry in the I(data) is matched, the index of that entry is returned as an integer.
- If set to C(True), the return value will always be a list, even if only a single entry is matched.
- This can also be accomplished using C(query) or C(q) instead of C(lookup).
- U(https://docs.ansible.com/ansible/latest/plugins/lookup.html)
type: bool
notes:
@@ -67,9 +70,9 @@ EXAMPLES = r"""
- ansible.builtin.set_fact:
data:
- 1
- 2
- 3
- 1
- 2
- 3
- name: Find the index of 2
ansible.builtin.set_fact:
@@ -137,14 +140,14 @@ EXAMPLES = r"""
- ansible.builtin.set_fact:
data:
- name: sw01.example.lan
type: switch
- name: rtr01.example.lan
type: router
- name: fw01.example.corp
type: firewall
- name: fw02.example.corp
type: firewall
- name: sw01.example.lan
type: switch
- name: rtr01.example.lan
type: router
- name: fw01.example.corp
type: firewall
- name: fw02.example.corp
type: firewall
- name: Find the index of all firewalls using the type key
ansible.builtin.set_fact:
@@ -175,7 +178,7 @@ EXAMPLES = r"""
msg: "The device named {{ data[item].name }} is a {{ data[item].type }}"
loop: "{{ lookup('ansible.utils.index_of', data, 'regex', expression, 'name') }}"
vars:
expression: '\.corp$' # ends with .corp
expression: '\.corp$'
# TASK [Find the index of all devices with a .corp name] *********************
# ok: [nxos101] => (item=2) =>
@@ -209,10 +212,10 @@ EXAMPLES = r"""
vars:
found: []
ip: '192.168.101.'
address: "{{ lookup('ansible.utils.index_of', item.1.ipv4|d([]), 'search', ip, 'address', wantlist=True) }}"
address: "{{ lookup('ansible.utils.index_of', item.1.ipv4 | d([]), 'search', ip, 'address', wantlist=True) }}"
entry:
- interface_idx: "{{ item.0 }}"
address_idxs: "{{ address }}"
- interface_idx: "{{ item.0 }}"
address_idxs: "{{ address }}"
when: address
# TASK [debug] ***************************************************************
@@ -243,8 +246,8 @@ EXAMPLES = r"""
interface:
- config:
description: configured by Ansible - 1
enabled: True
loopback-mode: False
enabled: true
loopback-mode: false
mtu: 1024
name: loopback0000
type: eth
@@ -253,18 +256,18 @@ EXAMPLES = r"""
subinterface:
- config:
description: subinterface configured by Ansible - 1
enabled: True
enabled: true
index: 5
index: 5
- config:
description: subinterface configured by Ansible - 2
enabled: False
enabled: false
index: 2
index: 2
- config:
description: configured by Ansible - 2
enabled: False
loopback-mode: False
enabled: false
loopback-mode: false
mtu: 2048
name: loopback1111
type: virt
@@ -273,12 +276,12 @@ EXAMPLES = r"""
subinterface:
- config:
description: subinterface configured by Ansible - 3
enabled: True
enabled: true
index: 10
index: 10
- config:
description: subinterface configured by Ansible - 4
enabled: False
enabled: false
index: 3
index: 3
@@ -317,12 +320,11 @@ RETURN = """
from ansible.errors import AnsibleLookupError
from ansible.plugins.lookup import LookupBase
from ansible_collections.ansible.utils.plugins.module_utils.common.index_of import (
index_of,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
AnsibleArgSpecValidator,
)
from ansible_collections.ansible.utils.plugins.plugin_utils.index_of import index_of
class LookupModule(LookupBase):
@@ -338,9 +340,9 @@ class LookupModule(LookupBase):
]
terms = dict(zip(keys, terms))
terms.update(kwargs)
aav = AnsibleArgSpecValidator(
data=terms, schema=DOCUMENTATION, name="index_of"
)
schema = [v for k, v in globals().items() if k.lower() == "documentation"]
aav = AnsibleArgSpecValidator(data=terms, schema=schema[0], name="index_of")
valid, errors, updated_data = aav.validate()
if not valid:
raise AnsibleLookupError(errors)

View File

@@ -9,35 +9,37 @@ The to_paths lookup plugin
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: to_paths
author: Bradley Thornton (@cidrblock)
plugin_type: lookup
version_added: "1.0.0"
short_description: Flatten a complex object into a dictionary of paths and values
description:
- Flatten a complex object into a dictionary of paths and values.
- Paths are dot delimited whenever possible.
- Brackets are used for list indices and keys that contain special characters.
- B(to_paths) is also available as a filter plugin.
- Using the parameters below- C(lookup('ansible.utils.to_paths', var, prepend, wantlist))
- Flatten a complex object into a dictionary of paths and values.
- Paths are dot delimited whenever possible.
- Brackets are used for list indices and keys that contain special characters.
- B(to_paths) is also available as a filter plugin.
- Using the parameters below- C(lookup('ansible.utils.to_paths', var, prepend, wantlist))
options:
var:
description: The value of I(var) will be used.
description:
- The value of I(var) will be used.
type: raw
required: True
prepend:
description: Prepend each path entry. Useful to add the initial I(var) name.
description:
- Prepend each path entry. Useful to add the initial I(var) name.
type: str
required: False
wantlist:
description: >
If set to I(True), the return value will always be a list.
This can also be accomplished using C(query) or B(q) instead of C(lookup).
U(https://docs.ansible.com/ansible/latest/plugins/lookup.html)
description:
- If set to I(True), the return value will always be a list.
- This can also be accomplished using C(query) or B(q) instead of C(lookup).
- U(https://docs.ansible.com/ansible/latest/plugins/lookup.html)
type: bool
notes:
@@ -52,11 +54,11 @@ EXAMPLES = r"""
b:
c:
d:
- 0
- 1
- 0
- 1
e:
- True
- False
- true
- false
- ansible.builtin.set_fact:
paths: "{{ lookup('ansible.utils.to_paths', a) }}"
@@ -93,7 +95,7 @@ EXAMPLES = r"""
accept: "application/yang.data+json"
url_password: password
url_username: admin
validate_certs: False
validate_certs: false
register: result
delegate_to: localhost
@@ -115,7 +117,6 @@ EXAMPLES = r"""
# interfaces.interface[0].ethernet.state.counters['in-jabber-frames']: '0'
# interfaces.interface[0].ethernet.state.counters['in-mac-control-frames']: '0'
# <...>
"""
RETURN = """
@@ -128,12 +129,11 @@ RETURN = """
from ansible.errors import AnsibleLookupError
from ansible.plugins.lookup import LookupBase
from ansible_collections.ansible.utils.plugins.module_utils.common.to_paths import (
to_paths,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
AnsibleArgSpecValidator,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.to_paths import to_paths
class LookupModule(LookupBase):
@@ -142,9 +142,9 @@ class LookupModule(LookupBase):
keys = ["var", "prepend"]
terms = dict(zip(keys, terms))
terms.update(kwargs)
aav = AnsibleArgSpecValidator(
data=terms, schema=DOCUMENTATION, name="to_paths"
)
schema = [v for k, v in globals().items() if k.lower() == "documentation"]
aav = AnsibleArgSpecValidator(data=terms, schema=schema[0], name="to_paths")
valid, errors, updated_data = aav.validate()
if not valid:
raise AnsibleLookupError(errors)

View File

@@ -5,67 +5,67 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: validate
author: Ganesh Nalawade (@ganeshrn)
plugin_type: lookup
version_added: "1.0.0"
short_description: Validate data with provided criteria
description:
- Validate I(data) with provided I(criteria) based on the validation I(engine).
- Validate I(data) with provided I(criteria) based on the validation I(engine).
options:
data:
type: raw
description:
- Data that will be validated against I(criteria).
- This option represents the value that is passed to the lookup plugin as the first argument.
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')),
in this case C(config_data) represents this option.
- For the type of I(data) that represents this value refer to the documentation of individual validate plugins.
- Data that will be validated against I(criteria).
- This option represents the value that is passed to the lookup plugin as the first argument.
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')),
in this case C(config_data) represents this option.
- For the type of I(data) that represents this value refer to the documentation of individual validate plugins.
required: True
criteria:
type: raw
description:
- The criteria used for validation of value that represents I(data) options.
- This option represents the second argument passed in the lookup plugin
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')),
in this case the value of C(config_criteria) represents this option.
- For the type of I(criteria) that represents this value refer to the documentation of individual
validate plugins.
- The criteria used for validation of value that represents I(data) options.
- This option represents the second argument passed in the lookup plugin
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')),
in this case the value of C(config_criteria) represents this option.
- For the type of I(criteria) that represents this value refer to the documentation of individual
validate plugins.
required: True
engine:
type: str
description:
- The name of the validate plugin to use.
- This option can be passed in lookup plugin as a key, value pair.
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')), in
this case the value C(ansible.utils.jsonschema) represents the engine to be use for data validation.
If the value is not provided the default value that is C(ansible.utils.jsonschema) will be used.
- The value should be in fully qualified collection name format that is
C(<org-name>.<collection-name>.<validate-plugin-name>).
- The name of the validate plugin to use.
- This option can be passed in lookup plugin as a key, value pair.
For example C(lookup(config_data, config_criteria, engine='ansible.utils.jsonschema')), in
this case the value C(ansible.utils.jsonschema) represents the engine to be use for data validation.
If the value is not provided the default value that is C(ansible.utils.jsonschema) will be used.
- The value should be in fully qualified collection name format that is
C(<org-name>.<collection-name>.<validate-plugin-name>).
default: ansible.utils.jsonschema
notes:
- For the type of options I(data) and I(criteria) refer to the individual validate plugin
documentation that is represented in the value of I(engine) option.
- For additional plugin configuration options refer to the individual validate plugin
documentation that is represented by the value of I(engine) option.
- The plugin configuration option can be either passed as C(key=value) pairs within lookup plugin
or task or environment variables.
- The precedence the validate plugin configurable option is the variable passed within lookup plugin
as C(key=value) pairs followed by task variables followed by environment variables.
- For the type of options I(data) and I(criteria) refer to the individual validate plugin
documentation that is represented in the value of I(engine) option.
- For additional plugin configuration options refer to the individual validate plugin
documentation that is represented by the value of I(engine) option.
- The plugin configuration option can be either passed as C(key=value) pairs within lookup plugin
or task or environment variables.
- The precedence the validate plugin configurable option is the variable passed within lookup plugin
as C(key=value) pairs followed by task variables followed by environment variables.
"""
EXAMPLES = r"""
- name: set facts for data and criteria
ansible.builtin.set_fact:
data: "{{ lookup('ansible.builtin.file', './validate/data/show_interfaces_iosxr.json')}}"
criteria: "{{ lookup('ansible.builtin.file', './validate/criteria/jsonschema/show_interfaces_iosxr.json')}}"
data: "{{ lookup('ansible.builtin.file', './validate/data/show_interfaces_iosxr.json') }}"
criteria: "{{ lookup('ansible.builtin.file', './validate/criteria/jsonschema/show_interfaces_iosxr.json') }}"
- name: validate data in json format using jsonschema with lookup plugin by passing plugin configuration variable as key/value pairs
ansible.builtin.set_fact:
data_criteria_checks: "{{ lookup(data, criteria, engine='ansible.utils.jsonschema', draft='draft7') }}"
data_criteria_checks: "{{ lookup('ansible.utils.validate', data, criteria, engine='ansible.utils.jsonschema', draft='draft7') }}"
- name: validate data in json format using jsonschema with lookup plugin by passing plugin configuration variable as task variable
ansible.builtin.set_fact:
@@ -84,15 +84,12 @@ RETURN = """
from ansible.errors import AnsibleError, AnsibleLookupError
from ansible.module_utils._text import to_text
from ansible.plugins.lookup import LookupBase
from ansible_collections.ansible.utils.plugins.plugin_utils.base.validate import (
_load_validator,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.utils import (
to_list,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
check_argspec,
)
from ansible_collections.ansible.utils.plugins.module_utils.common.utils import to_list
from ansible_collections.ansible.utils.plugins.plugin_utils.base.validate import _load_validator
ARGSPEC_CONDITIONALS = {}
@@ -103,25 +100,26 @@ class LookupModule(LookupBase):
if len(terms) < 2:
raise AnsibleLookupError(
"missing either 'data' or 'criteria' value in lookup input,"
" refer ansible.utils.validate lookup plugin documentation for details"
" refer ansible.utils.validate lookup plugin documentation for details",
)
params = {"data": terms[0], "criteria": terms[1]}
if kwargs.get("engine"):
params.update({"engine": kwargs["engine"]})
schema = [v for k, v in globals().items() if k.lower() == "documentation"]
valid, argspec_result, updated_params = check_argspec(
DOCUMENTATION,
"validate lookup",
schema=schema[0],
name="validate lookup",
schema_conditionals=ARGSPEC_CONDITIONALS,
**params
**params,
)
if not valid:
raise AnsibleLookupError(
"{argspec_result} with errors: {argspec_errors}".format(
argspec_result=argspec_result.get("msg"),
argspec_errors=argspec_result.get("errors"),
)
),
)
validator_engine, validator_result = _load_validator(
@@ -134,22 +132,20 @@ class LookupModule(LookupBase):
if validator_result.get("failed"):
raise AnsibleLookupError(
"validate lookup plugin failed with errors: {validator_result}".format(
validator_result=validator_result.get("msg")
)
validator_result=validator_result.get("msg"),
),
)
try:
result = validator_engine.validate()
except AnsibleError as exc:
raise AnsibleLookupError(
to_text(exc, errors="surrogate_then_replace")
)
raise AnsibleLookupError(to_text(exc, errors="surrogate_then_replace"))
except Exception as exc:
raise AnsibleLookupError(
"Unhandled exception from validator '{validator}'. Error: {err}".format(
validator=updated_params["engine"],
err=to_text(exc, errors="surrogate_then_replace"),
)
),
)
return to_list(result.get("errors", []))