| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: discovery_rule |
| 25 |
version_added: 3.5.0 |
| 26 |
short_description: Manage Host Discovery Rules |
| 27 |
description: |
| 28 |
- Manage Host Discovery Rules |
| 29 |
author: |
| 30 |
- "Jeffrey van Pelt (@Thulium-Drake)" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Name of the Discovery Rule |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
search: |
| 38 |
description: |
| 39 |
- Expression to match newly discovered hosts with |
| 40 |
- Required if I(state=present) |
| 41 |
type: str |
| 42 |
hostgroup: |
| 43 |
description: |
| 44 |
- Hostgroup to assign hosts to |
| 45 |
- Required if I(state=present) |
| 46 |
type: str |
| 47 |
hostname: |
| 48 |
description: |
| 49 |
- Hostname to assign to discovered host(s) |
| 50 |
- When matching multiple hosts, must provide unique hostnames for each of the discovered hosts |
| 51 |
type: str |
| 52 |
enabled: |
| 53 |
description: |
| 54 |
- Enable or disable the rule |
| 55 |
type: bool |
| 56 |
priority: |
| 57 |
description: |
| 58 |
- Priority of the rule |
| 59 |
type: int |
| 60 |
max_count: |
| 61 |
description: |
| 62 |
- Maximum amount of hosts to provision with the rule |
| 63 |
- 0 means no limit |
| 64 |
type: int |
| 65 |
extends_documentation_fragment: |
| 66 |
- theforeman.foreman.foreman |
| 67 |
- theforeman.foreman.foreman.entity_state |
| 68 |
- theforeman.foreman.foreman.taxonomy |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = ''' |
| 72 |
- name: 'Ensure Discovery Rule' |
| 73 |
theforeman.foreman.discovery_rule: |
| 74 |
username: 'admin' |
| 75 |
password: 'secret_password' |
| 76 |
server_url: 'https://foreman.example.com' |
| 77 |
name: 'my-first-disco' |
| 78 |
search: 'mac = bb:bb:bb:bb:bb:bb' |
| 79 |
hostgroup: 'RedHat7-Base' |
| 80 |
hostname: 'servera' |
| 81 |
max_count: 1 |
| 82 |
organizations: |
| 83 |
- 'MyOrg' |
| 84 |
locations: |
| 85 |
- 'DC1' |
| 86 |
|
| 87 |
- name: 'Remove Discovery Rule' |
| 88 |
theforeman.foreman.discovery_rule: |
| 89 |
username: 'admin' |
| 90 |
password: 'secret_password' |
| 91 |
server_url: 'https://foreman.example.com' |
| 92 |
name: 'my-first-disco' |
| 93 |
state: 'absent' |
| 94 |
''' |
| 95 |
|
| 96 |
RETURN = ''' |
| 97 |
entity: |
| 98 |
description: Final state of the affected entities grouped by their type. |
| 99 |
returned: success |
| 100 |
type: dict |
| 101 |
contains: |
| 102 |
discovery_rules: |
| 103 |
description: List of discovery rules. |
| 104 |
type: list |
| 105 |
elements: dict |
| 106 |
''' |
| 107 |
|
| 108 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule |
| 109 |
|
| 110 |
|
| 111 |
class ForemanDiscoveryRuleModule(ForemanTaxonomicEntityAnsibleModule): |
| 112 |
pass |
| 113 |
|
| 114 |
|
| 115 |
def main(): |
| 116 |
module = ForemanDiscoveryRuleModule( |
| 117 |
foreman_spec=dict( |
| 118 |
name=dict(required=True), |
| 119 |
search=dict(), |
| 120 |
hostgroup=dict(type='entity'), |
| 121 |
hostname=dict(), |
| 122 |
max_count=dict(type='int'), |
| 123 |
hosts_limit=dict(type='int', invisible=True, flat_name='max_count'), |
| 124 |
priority=dict(type='int'), |
| 125 |
enabled=dict(type='bool'), |
| 126 |
), |
| 127 |
required_if=[ |
| 128 |
['state', 'present', ['hostgroup', 'search']], |
| 129 |
], |
| 130 |
required_plugins=[('discovery', ['*'])], |
| 131 |
) |
| 132 |
|
| 133 |
with module.api_connection(): |
| 134 |
entity = module.lookup_entity('entity') |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if entity and 'hosts_limit' in entity: |
| 139 |
entity['max_count'] = entity.pop('hosts_limit') |
| 140 |
|
| 141 |
module.run() |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
main() |