| 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: host |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Hosts |
| 27 |
description: |
| 28 |
- Create, update, and delete Hosts |
| 29 |
author: |
| 30 |
- "Bernhard Hopfenmueller (@Fobhep) ATIX AG" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Fully Qualified Domain Name of host |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
hostgroup: |
| 38 |
description: |
| 39 |
- Title of related hostgroup |
| 40 |
- "Example: A child hostgroup I(bar) within a parent hostgroup I(foo) would have the title I(foo/bar)." |
| 41 |
required: false |
| 42 |
type: str |
| 43 |
location: |
| 44 |
description: |
| 45 |
- Name of related location |
| 46 |
required: false |
| 47 |
type: str |
| 48 |
organization: |
| 49 |
description: |
| 50 |
- Name of related organization |
| 51 |
required: false |
| 52 |
type: str |
| 53 |
build: |
| 54 |
description: |
| 55 |
- Whether or not to setup build context for the host |
| 56 |
type: bool |
| 57 |
required: false |
| 58 |
enabled: |
| 59 |
description: |
| 60 |
- Include this host within reporting |
| 61 |
type: bool |
| 62 |
required: false |
| 63 |
managed: |
| 64 |
description: |
| 65 |
- Whether a host is managed or unmanaged. |
| 66 |
- Forced to true when I(build=true) |
| 67 |
type: bool |
| 68 |
required: false |
| 69 |
ip: |
| 70 |
description: |
| 71 |
- IP address of the primary interface of the host. |
| 72 |
type: str |
| 73 |
required: false |
| 74 |
mac: |
| 75 |
description: |
| 76 |
- MAC address of the primary interface of the host. |
| 77 |
- Please include leading zeros and separate nibbles by colons, otherwise the execution will not be idempotent. |
| 78 |
- Example EE:BB:01:02:03:04 |
| 79 |
type: str |
| 80 |
required: false |
| 81 |
comment: |
| 82 |
description: |
| 83 |
- Comment about the host. |
| 84 |
type: str |
| 85 |
required: false |
| 86 |
owner: |
| 87 |
description: |
| 88 |
- Owner (user) of the host. |
| 89 |
- Users are looked up by their C(login). |
| 90 |
- Mutually exclusive with I(owner_group). |
| 91 |
type: str |
| 92 |
required: false |
| 93 |
owner_group: |
| 94 |
description: |
| 95 |
- Owner (user group) of the host. |
| 96 |
- Mutually exclusive with I(owner). |
| 97 |
type: str |
| 98 |
required: false |
| 99 |
provision_method: |
| 100 |
description: |
| 101 |
- The method used to provision the host. |
| 102 |
- I(provision_method=bootdisk) is only available if the bootdisk plugin is installed. |
| 103 |
choices: |
| 104 |
- 'build' |
| 105 |
- 'image' |
| 106 |
- 'bootdisk' |
| 107 |
type: str |
| 108 |
required: false |
| 109 |
image: |
| 110 |
description: |
| 111 |
- The image to use when I(provision_method=image). |
| 112 |
- The I(compute_resource) parameter is required to find the correct image. |
| 113 |
type: str |
| 114 |
required: false |
| 115 |
compute_attributes: |
| 116 |
description: |
| 117 |
- Additional compute resource specific attributes. |
| 118 |
- When this parameter is set, the module will not be idempotent. |
| 119 |
- When you provide a I(cluster) here and I(compute_resource) is set, the cluster id will be automatically looked up. |
| 120 |
type: dict |
| 121 |
required: false |
| 122 |
interfaces_attributes: |
| 123 |
description: |
| 124 |
- Additional interfaces specific attributes. |
| 125 |
version_added: 1.5.0 |
| 126 |
required: false |
| 127 |
type: list |
| 128 |
elements: dict |
| 129 |
suboptions: |
| 130 |
mac: |
| 131 |
description: |
| 132 |
- MAC address of interface. Required for managed interfaces on bare metal. |
| 133 |
- Please include leading zeros and separate nibbles by colons, otherwise the execution will not be idempotent. |
| 134 |
- Example EE:BB:01:02:03:04 |
| 135 |
- You need to set one of I(identifier), I(name) or I(mac) to be able to update existing interfaces and make execution idempotent. |
| 136 |
type: str |
| 137 |
ip: |
| 138 |
description: |
| 139 |
- IPv4 address of interface |
| 140 |
type: str |
| 141 |
ip6: |
| 142 |
description: |
| 143 |
- IPv6 address of interface |
| 144 |
type: str |
| 145 |
type: |
| 146 |
description: |
| 147 |
- Interface type. |
| 148 |
type: str |
| 149 |
choices: |
| 150 |
- 'interface' |
| 151 |
- 'bmc' |
| 152 |
- 'bond' |
| 153 |
- 'bridge' |
| 154 |
name: |
| 155 |
description: |
| 156 |
- Interface's DNS name |
| 157 |
- You need to set one of I(identifier), I(name) or I(mac) to be able to update existing interfaces and make execution idempotent. |
| 158 |
type: str |
| 159 |
subnet: |
| 160 |
description: |
| 161 |
- IPv4 Subnet name |
| 162 |
type: str |
| 163 |
subnet6: |
| 164 |
description: |
| 165 |
- IPv6 Subnet name |
| 166 |
type: str |
| 167 |
domain: |
| 168 |
description: |
| 169 |
- Domain name |
| 170 |
- Required for primary interfaces on managed hosts. |
| 171 |
type: str |
| 172 |
identifier: |
| 173 |
description: |
| 174 |
- Device identifier, e.g. eth0 or eth1.1 |
| 175 |
- You need to set one of I(identifier), I(name) or I(mac) to be able to update existing interfaces and make execution idempotent. |
| 176 |
type: str |
| 177 |
managed: |
| 178 |
description: |
| 179 |
- Should this interface be managed via DHCP and DNS smart proxy and should it be configured during provisioning? |
| 180 |
type: bool |
| 181 |
primary: |
| 182 |
description: |
| 183 |
- Should this interface be used for constructing the FQDN of the host? |
| 184 |
- Each managed hosts needs to have one primary interface. |
| 185 |
type: bool |
| 186 |
provision: |
| 187 |
description: |
| 188 |
- Should this interface be used for TFTP of PXELinux (or SSH for image-based hosts)? |
| 189 |
- Each managed hosts needs to have one provision interface. |
| 190 |
type: bool |
| 191 |
execution: |
| 192 |
description: |
| 193 |
- Should this interface be used for Remote Execution? |
| 194 |
- Each managed hosts should have one remote execution interface. |
| 195 |
type: bool |
| 196 |
username: |
| 197 |
description: |
| 198 |
- Username for BMC authentication. |
| 199 |
- Only for BMC interfaces. |
| 200 |
type: str |
| 201 |
password: |
| 202 |
description: |
| 203 |
- Password for BMC authentication. |
| 204 |
- Only for BMC interfaces. |
| 205 |
type: str |
| 206 |
provider: |
| 207 |
description: |
| 208 |
- Interface provider, e.g. IPMI. |
| 209 |
- Only for BMC interfaces. |
| 210 |
type: str |
| 211 |
choices: |
| 212 |
- 'IPMI' |
| 213 |
- 'Redfish' |
| 214 |
- 'SSH' |
| 215 |
virtual: |
| 216 |
description: |
| 217 |
- Alias or VLAN device |
| 218 |
type: bool |
| 219 |
tag: |
| 220 |
description: |
| 221 |
- VLAN tag, this attribute has precedence over the subnet VLAN ID. |
| 222 |
- Only for virtual interfaces. |
| 223 |
type: str |
| 224 |
mtu: |
| 225 |
description: |
| 226 |
- MTU, this attribute has precedence over the subnet MTU. |
| 227 |
type: int |
| 228 |
attached_to: |
| 229 |
description: |
| 230 |
- Identifier of the interface to which this interface belongs, e.g. eth1. |
| 231 |
- Only for virtual interfaces. |
| 232 |
type: str |
| 233 |
mode: |
| 234 |
description: |
| 235 |
- Bond mode of the interface. |
| 236 |
- Only for bond interfaces. |
| 237 |
type: str |
| 238 |
choices: |
| 239 |
- 'balance-rr' |
| 240 |
- 'active-backup' |
| 241 |
- 'balance-xor' |
| 242 |
- 'broadcast' |
| 243 |
- '802.3ad' |
| 244 |
- 'balance-tlb' |
| 245 |
- 'balance-alb' |
| 246 |
attached_devices: |
| 247 |
description: |
| 248 |
- Identifiers of attached interfaces, e.g. ['eth1', 'eth2']. |
| 249 |
- For bond interfaces those are the slaves. |
| 250 |
- Only for bond and bridges interfaces. |
| 251 |
type: list |
| 252 |
elements: str |
| 253 |
bond_options: |
| 254 |
description: |
| 255 |
- Space separated options, e.g. miimon=100. |
| 256 |
- Only for bond interfaces. |
| 257 |
type: str |
| 258 |
compute_attributes: |
| 259 |
description: |
| 260 |
- Additional compute resource specific attributes for the interface. |
| 261 |
- When this parameter is set, the module will not be idempotent. |
| 262 |
- When you provide a I(network) here and I(compute_resource) is set, the network id will be automatically looked up. |
| 263 |
- On oVirt/RHV I(cluster) is required in the hosts I(compute_attributes) for the lookup to work. |
| 264 |
type: dict |
| 265 |
extends_documentation_fragment: |
| 266 |
- theforeman.foreman.foreman |
| 267 |
- theforeman.foreman.foreman.entity_state |
| 268 |
- theforeman.foreman.foreman.host_options |
| 269 |
- theforeman.foreman.foreman.nested_parameters |
| 270 |
- theforeman.foreman.foreman.operatingsystem |
| 271 |
''' |
| 272 |
|
| 273 |
EXAMPLES = ''' |
| 274 |
- name: "Create a host" |
| 275 |
theforeman.foreman.host: |
| 276 |
username: "admin" |
| 277 |
password: "changeme" |
| 278 |
server_url: "https://foreman.example.com" |
| 279 |
name: "new_host" |
| 280 |
hostgroup: my_hostgroup |
| 281 |
state: present |
| 282 |
|
| 283 |
- name: "Create a host with build context" |
| 284 |
theforeman.foreman.host: |
| 285 |
username: "admin" |
| 286 |
password: "changeme" |
| 287 |
server_url: "https://foreman.example.com" |
| 288 |
name: "new_host" |
| 289 |
hostgroup: my_hostgroup |
| 290 |
build: true |
| 291 |
state: present |
| 292 |
|
| 293 |
- name: "Create an unmanaged host" |
| 294 |
theforeman.foreman.host: |
| 295 |
username: "admin" |
| 296 |
password: "changeme" |
| 297 |
server_url: "https://foreman.example.com" |
| 298 |
name: "new_host" |
| 299 |
managed: false |
| 300 |
state: present |
| 301 |
|
| 302 |
- name: "Create a VM with 2 CPUs and 4GB RAM" |
| 303 |
theforeman.foreman.host: |
| 304 |
username: "admin" |
| 305 |
password: "changeme" |
| 306 |
server_url: "https://foreman.example.com" |
| 307 |
name: "new_host" |
| 308 |
compute_attributes: |
| 309 |
cpus: 2 |
| 310 |
memory_mb: 4096 |
| 311 |
state: present |
| 312 |
|
| 313 |
- name: "Create a VM and start it after creation" |
| 314 |
theforeman.foreman.host: |
| 315 |
username: "admin" |
| 316 |
password: "changeme" |
| 317 |
server_url: "https://foreman.example.com" |
| 318 |
name: "new_host" |
| 319 |
compute_attributes: |
| 320 |
start: "1" |
| 321 |
state: present |
| 322 |
|
| 323 |
- name: "Create a VM on specific ovirt network" |
| 324 |
theforeman.foreman.host: |
| 325 |
username: "admin" |
| 326 |
password: "changeme" |
| 327 |
server_url: "https://foreman.example.com" |
| 328 |
name: "new_host" |
| 329 |
interfaces_attributes: |
| 330 |
- type: "interface" |
| 331 |
compute_attributes: |
| 332 |
name: "nic1" |
| 333 |
network: "969efbe6-f9e0-4383-a19a-a7ee65ad5007" |
| 334 |
interface: "virtio" |
| 335 |
state: present |
| 336 |
|
| 337 |
- name: "Create a VM with 2 NICs on specific ovirt networks" |
| 338 |
theforeman.foreman.host: |
| 339 |
username: "admin" |
| 340 |
password: "changeme" |
| 341 |
server_url: "https://foreman.example.com" |
| 342 |
name: "new_host" |
| 343 |
interfaces_attributes: |
| 344 |
- type: "interface" |
| 345 |
primary: true |
| 346 |
compute_attributes: |
| 347 |
name: "nic1" |
| 348 |
network: "969efbe6-f9e0-4383-a19a-a7ee65ad5007" |
| 349 |
interface: "virtio" |
| 350 |
- type: "interface" |
| 351 |
name: "new_host_nic2" |
| 352 |
managed: true |
| 353 |
compute_attributes: |
| 354 |
name: "nic2" |
| 355 |
network: "969efbe6-f9e0-4383-a19a-a7ee65ad5008" |
| 356 |
interface: "e1000" |
| 357 |
state: present |
| 358 |
|
| 359 |
- name: "Delete a host" |
| 360 |
theforeman.foreman.host: |
| 361 |
username: "admin" |
| 362 |
password: "changeme" |
| 363 |
server_url: "https://foreman.example.com" |
| 364 |
name: "new_host" |
| 365 |
state: absent |
| 366 |
''' |
| 367 |
|
| 368 |
RETURN = ''' |
| 369 |
entity: |
| 370 |
description: Final state of the affected entities grouped by their type. |
| 371 |
returned: success |
| 372 |
type: dict |
| 373 |
contains: |
| 374 |
hosts: |
| 375 |
description: List of hosts. |
| 376 |
type: list |
| 377 |
elements: dict |
| 378 |
''' |
| 379 |
|
| 380 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 381 |
ensure_puppetclasses, |
| 382 |
interfaces_spec, |
| 383 |
ForemanEntityAnsibleModule, |
| 384 |
HostMixin, |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
def ensure_host_interfaces(module, entity, interfaces): |
| 389 |
scope = {'host_id': entity['id']} |
| 390 |
|
| 391 |
current_interfaces = module.list_resource('interfaces', params=scope) |
| 392 |
current_interfaces_ids = {x['id'] for x in current_interfaces} |
| 393 |
expected_interfaces_ids = set() |
| 394 |
|
| 395 |
for interface in interfaces: |
| 396 |
if 1 == len(current_interfaces) == len(interfaces): |
| 397 |
existing_interface = current_interfaces[0] |
| 398 |
else: |
| 399 |
for possible_identifier in ['identifier', 'name', 'mac']: |
| 400 |
if possible_identifier in interface: |
| 401 |
unique_identifier = possible_identifier |
| 402 |
break |
| 403 |
else: |
| 404 |
unique_identifier = None |
| 405 |
warning_msg = "The provided interface definition has no unique identifier and thus cannot be matched against existing interfaces. " \ |
| 406 |
"This will always create a new interface and might not be the desired behaviour." |
| 407 |
module.warn(warning_msg) |
| 408 |
|
| 409 |
existing_interface = next((x for x in current_interfaces if unique_identifier and x.get(unique_identifier) == interface[unique_identifier]), None) |
| 410 |
|
| 411 |
if 'mac' in interface: |
| 412 |
interface['mac'] = interface['mac'].lower() |
| 413 |
|
| 414 |
|
| 415 |
if existing_interface is not None and 'attached_devices' in existing_interface: |
| 416 |
existing_interface['attached_devices'] = existing_interface['attached_devices'].split(',') |
| 417 |
|
| 418 |
updated_interface = (existing_interface or {}).copy() |
| 419 |
updated_interface.update(interface) |
| 420 |
|
| 421 |
module.ensure_entity('interfaces', updated_interface, existing_interface, params=scope, state='present', |
| 422 |
foreman_spec=module.foreman_spec['interfaces_attributes']['foreman_spec']) |
| 423 |
|
| 424 |
if 'id' in updated_interface: |
| 425 |
expected_interfaces_ids.add(updated_interface['id']) |
| 426 |
|
| 427 |
for leftover_interface in current_interfaces_ids - expected_interfaces_ids: |
| 428 |
module.ensure_entity('interfaces', {}, {'id': leftover_interface}, params=scope, state='absent', |
| 429 |
foreman_spec=module.foreman_spec['interfaces_attributes']['foreman_spec']) |
| 430 |
|
| 431 |
|
| 432 |
class ForemanHostModule(HostMixin, ForemanEntityAnsibleModule): |
| 433 |
pass |
| 434 |
|
| 435 |
|
| 436 |
def main(): |
| 437 |
module = ForemanHostModule( |
| 438 |
foreman_spec=dict( |
| 439 |
name=dict(required=True), |
| 440 |
hostgroup=dict(type='entity'), |
| 441 |
location=dict(type='entity'), |
| 442 |
organization=dict(type='entity'), |
| 443 |
enabled=dict(type='bool'), |
| 444 |
managed=dict(type='bool'), |
| 445 |
build=dict(type='bool'), |
| 446 |
ip=dict(), |
| 447 |
mac=dict(), |
| 448 |
comment=dict(), |
| 449 |
owner=dict(type='entity', resource_type='users', flat_name='owner_id'), |
| 450 |
owner_group=dict(type='entity', resource_type='usergroups', flat_name='owner_id'), |
| 451 |
owner_type=dict(invisible=True), |
| 452 |
provision_method=dict(choices=['build', 'image', 'bootdisk']), |
| 453 |
image=dict(type='entity', scope=['compute_resource']), |
| 454 |
compute_attributes=dict(type='dict'), |
| 455 |
interfaces_attributes=dict(type='nested_list', foreman_spec=interfaces_spec, ensure=True), |
| 456 |
), |
| 457 |
mutually_exclusive=[ |
| 458 |
['owner', 'owner_group'] |
| 459 |
], |
| 460 |
required_by=dict( |
| 461 |
image=('compute_resource',), |
| 462 |
), |
| 463 |
) |
| 464 |
|
| 465 |
|
| 466 |
if '.' not in module.foreman_params['name']: |
| 467 |
module.fail_json(msg="The hostname must be FQDN") |
| 468 |
|
| 469 |
if not module.desired_absent: |
| 470 |
if 'build' in module.foreman_params and module.foreman_params['build']: |
| 471 |
|
| 472 |
if 'managed' in module.foreman_params and not module.foreman_params['managed']: |
| 473 |
module.warn("when 'build'=True, 'managed' is ignored and forced to True") |
| 474 |
module.foreman_params['managed'] = True |
| 475 |
elif 'build' not in module.foreman_params and 'managed' in module.foreman_params and not module.foreman_params['managed']: |
| 476 |
|
| 477 |
module.foreman_params['build'] = False |
| 478 |
|
| 479 |
if 'mac' in module.foreman_params: |
| 480 |
module.foreman_params['mac'] = module.foreman_params['mac'].lower() |
| 481 |
|
| 482 |
if 'owner' in module.foreman_params: |
| 483 |
module.foreman_params['owner_type'] = 'User' |
| 484 |
elif 'owner_group' in module.foreman_params: |
| 485 |
module.foreman_params['owner_type'] = 'Usergroup' |
| 486 |
|
| 487 |
with module.api_connection(): |
| 488 |
entity = module.lookup_entity('entity', params={'show_hidden_parameters': True}) |
| 489 |
|
| 490 |
if not module.desired_absent: |
| 491 |
module.auto_lookup_entities() |
| 492 |
|
| 493 |
if 'image' in module.foreman_params: |
| 494 |
if 'compute_attributes' not in module.foreman_params: |
| 495 |
module.foreman_params['compute_attributes'] = {} |
| 496 |
module.foreman_params['compute_attributes']['image_id'] = module.foreman_params['image']['uuid'] |
| 497 |
|
| 498 |
if 'compute_resource' in module.foreman_params: |
| 499 |
compute_resource = module.foreman_params['compute_resource'] |
| 500 |
cluster = None |
| 501 |
if 'compute_attributes' in module.foreman_params: |
| 502 |
if 'cluster' in module.foreman_params['compute_attributes']: |
| 503 |
cluster = module.find_cluster(module.foreman_params['compute_attributes']['cluster'], compute_resource) |
| 504 |
module.foreman_params['compute_attributes']['cluster'] = cluster['_api_identifier'] |
| 505 |
|
| 506 |
if 'volumes_attributes' in module.foreman_params['compute_attributes']: |
| 507 |
for volume in module.foreman_params['compute_attributes']['volumes_attributes'].values(): |
| 508 |
if 'storage_pod' in volume: |
| 509 |
storage_pod = module.find_storage_pod(volume['storage_pod'], compute_resource, cluster) |
| 510 |
volume['storage_pod'] = storage_pod['name'] |
| 511 |
if 'storage_domain' in volume: |
| 512 |
storage_domain = module.find_storage_domain(volume['storage_domain'], compute_resource, cluster) |
| 513 |
volume['storage_domain'] = storage_domain['id'] |
| 514 |
|
| 515 |
if 'interfaces_attributes' in module.foreman_params: |
| 516 |
for interface in module.foreman_params['interfaces_attributes']: |
| 517 |
if 'compute_attributes' in interface and 'network' in interface['compute_attributes']: |
| 518 |
network = module.find_network(interface['compute_attributes']['network'], compute_resource, cluster) |
| 519 |
interface['compute_attributes']['network'] = network['id'] |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if entity and 'interfaces_attributes' in module.foreman_params: |
| 524 |
interfaces = module.foreman_params.pop('interfaces_attributes') |
| 525 |
else: |
| 526 |
interfaces = None |
| 527 |
|
| 528 |
expected_puppetclasses = module.foreman_params.pop('puppetclasses', None) |
| 529 |
|
| 530 |
entity = module.run() |
| 531 |
|
| 532 |
if not module.desired_absent: |
| 533 |
if 'environment_id' in entity: |
| 534 |
ensure_puppetclasses(module, 'host', entity, expected_puppetclasses) |
| 535 |
if interfaces is not None: |
| 536 |
ensure_host_interfaces(module, entity, interfaces) |
| 537 |
|
| 538 |
|
| 539 |
if __name__ == '__main__': |
| 540 |
main() |