KATELLO FOREMAN AND MODULES FOR WRITING ANSIBLE · Collection on Ansible Galaxy RPM on...

Post on 27-Jun-2020

6 views 0 download

transcript

WRITING ANSIBLEWRITING ANSIBLEMODULES FORMODULES FORFOREMAN ANDFOREMAN AND

KATELLOKATELLO

1

$ WHOAMI$ WHOAMI

Evgeni Golov

Senior Software Engineer at Red Hat

ex-Consultant at Red Hat

Debian and Grml Developer

♥ FOSS ♥

♥ automation ♥

2

WTF?!WTF?!15 minute version of 45 minute talkhow to best automate Foreman/Katellousing Ansiblespoiler: command: hammer is not theanswer!

3

WHY NOT WHY NOT XX?!?! by Thomas

Krahn ( ) is probably the oldestWell maintainedSupports only Foreman

Upstream Ansible foreman and katellomodules

Deprecated since Ansible 2.8"one" module for everything

ansible-module-foreman

@Nosmoht

4

FOREMAN ANSIBLEFOREMAN ANSIBLEMODULESMODULES

Started June 2017 as a repository to clean upupstream modulesOne module per Foreman entity or actionExtensive test-suiteAbstraction framework for common tasks(connect, search, create, update, delete)

5

FOREMAN ANSIBLEFOREMAN ANSIBLEMODULESMODULES

Initially, we still used nailgunnailgun releases are Satellite versionspeci�cPlugins not in Satellite are not supportedDoesn't work without Katello installed

Recent switch to apypieConsumes the apidoc.json publishedby Foreman / apipie-rails

Migration quite easy thanks to the existingframework and tests

6

FOREMAN ANSIBLEFOREMAN ANSIBLEMODULES - STATSMODULES - STATS43 🌟 on GitHub24 Contributors (8 Red Hat, 7 ATIX)8 new Contributors in 2019

7

8

9

FOREMAN ANSIBLEFOREMAN ANSIBLEMODULES - OUTLOOKMODULES - OUTLOOK

Collection on Ansible GalaxyRPM on yum.theforeman.org

10

LET'S WRITE ALET'S WRITE AMODULE!MODULE!

11

UNDER THE HOODUNDER THE HOODMost modules manage objects/entities in

Foreman1. Find an existing entity2. Compare existing entity with the data

provided by the user3. Save the entity

We have a framework to support that

12

First a wrapper around AnsibleModule:from ansible.module_utils.foreman_helper import ForemanEntityApypieAnsibleModule module = ForemanEntityApypieAnsibleModule( argument_spec=dict(name=dict(required=True)))

13

Load user provided parameters and connect tothe API:

entity_dict = module.clean_params() module.connect()

14

Find the entity and ensure it looks like the userwanted:

entity = module.find_resource_by_name('architectures', name=entity_dict['name'], failsafe=True) changed = module.ensure_resource_state('architectures', entity_dict, entity, name_map) module.exit_json(changed=changed)

15

Translate Ansible params to Foreman APIparams:

name_map = { 'name': 'name' }

16

from ansible.module_utils.foreman_helper import ForemanEntityApypieAnsibleModule name_map = { 'name': 'name' } module = ForemanEntityApypieAnsibleModule( argument_spec=dict(name=dict(required=True))) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('architectures', name=entity_dict['name'], failsafe=True) changed = module.ensure_resource_state('architectures', entity_dict, entity, name_map) module.exit_json(changed=changed)

17

if not module.desired_absent: if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = module.find_resources_by_title('operatingsystems', entity_dict['operatingsystems'], thin=True)

18

if not module.desired_absent: if 'operatingsystems' in entity_dict: search_list = ["title~{}".format(title) for title in entity_dict['operatingsystems']] entity_dict['operatingsystems'] = module.find_resources('operatingsystems', search_list, thin=True)

19