SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
< Ansible Modules 101 >
---------------------
 ^__^
 (oo)_______
(__) )/
||----w |
|| ||
by rainer schuettengruber
Ansible
a fictional device capable of instantaneous or faster than light
communication, that can send and recieve messages over any
distance or obstacle with no delay.
Ansible
• but no science fiction today ...
• configuration management tool
• implemented in Python
• requires ssh and Python on the target host
• comes with a plethora of modules, which form the basic building
blocks for playbooks
module processing
ansible
control host
Playbook : oracle_user.yml
---
- name: do some oracle stuff
hosts: oracleservers
gather_facts: False
tasks:
- name: create user test
become: true
become_user: oracle
oracle_user:
database_name: XE
user_name: test
password: test
#!/opt/python/bin/python
# -*- coding: utf-8 -*-
ANSIBALLZ_WRAPPER = True
import os
import os.path
import sys
import __main__
scriptdir = None
database host
oraclexe
ssh
1
2
3
4
{'changed': false, 'failed': true, 'msg': 'could not reach the host'}
5
module : oracle_user
#!/opt/python/bin/python
from ansible.module_utils.basic import AnsibleModule
import os
import cx_Oracle
def create_user(cursor, user_name, password):
create_user_statement = '''
1.process playbook
2.generate python script
3.copy python script to target host(s)
4.execute script on target host(s)
5.parse the script‘s output
expected output
{'changed': false, 'failed': true, 'msg': 'could not reach the host'}
• changed indicates that host state has been changed by the module
• failed indicates that the module has not completed successfully
• msg expand on the reason of the failure
AnsibleModule
from ansible.module_utils.basic import AnsibleModule
• deals with parameter parsing
• returns output in JSON
let‘s get playing
root@ansible:~/playbooks# cat oracle_user.yml
---
- name: do some oracle stuff
hosts: oracleservers
gather_facts: False
tasks:
- name: create user test
become: true
become_user: oracle
oracle_user:
database_name: XE
user_name: test
password: test
environment:
LD_LIBRARY_PATH: /u01/app/oracle/product/11.2.0/xe/lib
parameters
def main():
module = AnsibleModule(
argument_spec=dict(
oracle_home=dict(
type='str',
default='/u01/app/oracle/product/11.2.0/xe',
),
database_name=dict(required=True, type='str'),
user_name=dict(required=True, type='str'),
password=dict(required=True, type='str'),
),
supports_check_mode=True,
)
database_name=module.params['database_name']
user_name=module.params['user_name']
password=module.params['password']
oracle_home=module.params['oracle_home']
os.environ['ORACLE_HOME'] = oracle_home
os.environ['ORACLE_SID'] = database_name
error handling
try:
sysdba_connection = cx_Oracle.connect('/', mode=cx_Oracle.SYSDBA)
cursor = sysdba_connection.cursor()
except cx_Oracle.DatabaseError as exc:
error, = exc.args
formatted_error = error.message.replace('n', ' ')[:40]
error_msg='unable to connect to database {} due to {}'.format(
database_name,
formatted_error,
)
module.fail_json(msg=error_msg)
error handling in action
root@ansible:~/playbooks# ansible-playbook oracle_user.yml
PLAY [do some oracle stuff] ****************************************************
TASK [create schema test] ******************************************************
fatal: [oraclexe]: FAILED! => {
"changed": false,
"failed": true
}
MSG:
unable to connect to database XA due to ORA-01034: ORACLE not available ORA-2710
to retry, use: --limit @/root/playbooks/oracle_schema.retry
PLAY RECAP *********************************************************************
oraclexe : ok=0 changed=0 unreachable=0 failed=1
root@ansible:~/playbooks#
Idempotence
Operations can be applied multiple times without changing the result
beyond the initial application.
Idempotence as code
if user_exists(cursor=cursor, user_name=user_name, password=password):
module.exit_json(changed=False)
else:
if module.check_mode:
module.exit_json(changed=True)
create_user_result = create_user(
cursor=cursor,
user_name=user_name,
password=password,
)
cursor.close()
if not create_user_result['status']:
module.fail_json(msg=create_user_result['msg'])
else:
module.exit_json(changed=True, msg=create_user_result['msg'])
Idempotence in action
root@ansible:~/playbooks# ansible-playbook oracle_user.yml
PLAY [do some oracle stuff]
****************************************************
TASK [create user test]
******************************************************
ok: [oraclexe]
PLAY RECAP
*********************************************************************
oraclexe : ok=1 changed=0 unreachable=0 failed=0
root@ansible:~/playbooks#
check mode
• will not make any changes
• but reports on the possible outcome
check mode as code
supports_check_mode=True,
.
.
if user_exists(cursor=cursor, user_name=user_name, password=password):
module.exit_json(changed=False)
else:
if module.check_mode:
module.exit_json(changed=True)
create_user_result = create_user(
cursor=cursor,
user_name=user_name,
password=password,
)
cursor.close()
if not create_user_result['status']:
module.fail_json(msg=create_user_result['msg'])
else:
module.exit_json(changed=True, msg=create_user_result['msg'])
check mode in action
root@ansible:~/playbooks# ansible-playbook oracle_user.yml --check
PLAY [do some oracle stuff] ****************************************************
TASK [create user test] ********************************************************
changed: [oraclexe]
PLAY RECAP *********************************************************************
oraclexe : ok=1 changed=1 unreachable=0 failed=0
root@ansible:~/playbooks#
SQL> select username from dba_users where username='TEST';
no rows selected
SQL>
the final curtain
root@ansible:~/playbooks# ansible-playbook oracle_user.yml
PLAY [do some oracle stuff] ****************************************************
TASK [create user test] ********************************************************
changed: [oraclexe]
PLAY RECAP *********************************************************************
oraclexe : ok=1 changed=1 unreachable=0 failed=0
root@ansible:~/playbooks#
SQL> select username from dba_users where username='TEST';
USERNAME
------------------------------
TEST
SQL>
references
• Hochstein L. and Moser R.(2017). Ansible Up & Running. O‘Reilly.
• Ansible Module Development Walkthrough. [online] Available at:
http://docs.ansible.com/ansible/latest/dev_guide/developing_modules_gener
al.html
questions and answers

Más contenido relacionado

La actualidad más candente

Ansible for beginners ...?
Ansible for beginners ...?Ansible for beginners ...?
Ansible for beginners ...?shirou wakayama
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansibleOmid Vahdaty
 
AnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and TricksAnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and Tricksjimi-c
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationJohn Lynch
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to AnsibleCoreStack
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansibleGeorge Shuklin
 
Ansible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife OrchestrationAnsible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife Orchestrationbcoca
 
Ansible fest Presentation slides
Ansible fest Presentation slidesAnsible fest Presentation slides
Ansible fest Presentation slidesAaron Carey
 
V2 and beyond
V2 and beyondV2 and beyond
V2 and beyondjimi-c
 
Ansible roles done right
Ansible roles done rightAnsible roles done right
Ansible roles done rightDan Vaida
 
Automation with ansible
Automation with ansibleAutomation with ansible
Automation with ansibleKhizer Naeem
 
Ansible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupAnsible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupGreg DeKoenigsberg
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with AnsibleRayed Alrashed
 
Puppet at janrain
Puppet at janrainPuppet at janrain
Puppet at janrainPuppet
 

La actualidad más candente (20)

Ansible for beginners ...?
Ansible for beginners ...?Ansible for beginners ...?
Ansible for beginners ...?
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Ansible - Crash course
Ansible - Crash courseAnsible - Crash course
Ansible - Crash course
 
AnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and TricksAnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and Tricks
 
Django Celery
Django Celery Django Celery
Django Celery
 
Configuration Management in Ansible
Configuration Management in Ansible Configuration Management in Ansible
Configuration Management in Ansible
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to Ansible
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Ansible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife OrchestrationAnsible - Swiss Army Knife Orchestration
Ansible - Swiss Army Knife Orchestration
 
Ansible testing
Ansible   testingAnsible   testing
Ansible testing
 
Ansible fest Presentation slides
Ansible fest Presentation slidesAnsible fest Presentation slides
Ansible fest Presentation slides
 
V2 and beyond
V2 and beyondV2 and beyond
V2 and beyond
 
Ansible roles done right
Ansible roles done rightAnsible roles done right
Ansible roles done right
 
Automation with ansible
Automation with ansibleAutomation with ansible
Automation with ansible
 
Ansible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupAnsible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetup
 
Ansible - Introduction
Ansible - IntroductionAnsible - Introduction
Ansible - Introduction
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Puppet at janrain
Puppet at janrainPuppet at janrain
Puppet at janrain
 

Similar a Ansible modules 101

Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and ContainersRodolfo Carvalho
 
How to deploy docker container inside ikoula's cloud
How to deploy docker container inside ikoula's cloudHow to deploy docker container inside ikoula's cloud
How to deploy docker container inside ikoula's cloudNicolas Trauwaen
 
Ansible inside
Ansible insideAnsible inside
Ansible insideIdeato
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with ociDonghuKIM2
 
Tools for Solving Performance Issues
Tools for Solving Performance IssuesTools for Solving Performance Issues
Tools for Solving Performance IssuesOdoo
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationSuresh Kumar
 
A MySQL Odyssey - A Blackhole Crossover
A MySQL Odyssey - A Blackhole CrossoverA MySQL Odyssey - A Blackhole Crossover
A MySQL Odyssey - A Blackhole CrossoverKeith Hollman
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensNETWAYS
 
Introducing the Seneca MVP framework for Node.js
Introducing the Seneca MVP framework for Node.jsIntroducing the Seneca MVP framework for Node.js
Introducing the Seneca MVP framework for Node.jsRichard Rodger
 
Just one-shade-of-openstack
Just one-shade-of-openstackJust one-shade-of-openstack
Just one-shade-of-openstackRoberto Polli
 
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)Chu-Siang Lai
 
Vagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopVagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopLorin Hochstein
 
Ansible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL MeetupAnsible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL MeetupJeff Geerling
 
10 things I learned building Nomad packs
10 things I learned building Nomad packs10 things I learned building Nomad packs
10 things I learned building Nomad packsBram Vogelaar
 

Similar a Ansible modules 101 (20)

Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
How to deploy docker container inside ikoula's cloud
How to deploy docker container inside ikoula's cloudHow to deploy docker container inside ikoula's cloud
How to deploy docker container inside ikoula's cloud
 
Ansible inside
Ansible insideAnsible inside
Ansible inside
 
Oracle on Solaris
Oracle on SolarisOracle on Solaris
Oracle on Solaris
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with oci
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
Linux configer
Linux configerLinux configer
Linux configer
 
Tools for Solving Performance Issues
Tools for Solving Performance IssuesTools for Solving Performance Issues
Tools for Solving Performance Issues
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
A MySQL Odyssey - A Blackhole Crossover
A MySQL Odyssey - A Blackhole CrossoverA MySQL Odyssey - A Blackhole Crossover
A MySQL Odyssey - A Blackhole Crossover
 
Zabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet MensZabbix LLD from a C Module by Jan-Piet Mens
Zabbix LLD from a C Module by Jan-Piet Mens
 
Introducing the Seneca MVP framework for Node.js
Introducing the Seneca MVP framework for Node.jsIntroducing the Seneca MVP framework for Node.js
Introducing the Seneca MVP framework for Node.js
 
20120816 nodejsdublin
20120816 nodejsdublin20120816 nodejsdublin
20120816 nodejsdublin
 
Just one-shade-of-openstack
Just one-shade-of-openstackJust one-shade-of-openstack
Just one-shade-of-openstack
 
DevOps for database
DevOps for databaseDevOps for database
DevOps for database
 
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
 
Slickdemo
SlickdemoSlickdemo
Slickdemo
 
Vagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptopVagrant, Ansible, and OpenStack on your laptop
Vagrant, Ansible, and OpenStack on your laptop
 
Ansible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL MeetupAnsible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL Meetup
 
10 things I learned building Nomad packs
10 things I learned building Nomad packs10 things I learned building Nomad packs
10 things I learned building Nomad packs
 

Último

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 

Último (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

Ansible modules 101

  • 1. < Ansible Modules 101 > --------------------- ^__^ (oo)_______ (__) )/ ||----w | || || by rainer schuettengruber
  • 2. Ansible a fictional device capable of instantaneous or faster than light communication, that can send and recieve messages over any distance or obstacle with no delay.
  • 3. Ansible • but no science fiction today ... • configuration management tool • implemented in Python • requires ssh and Python on the target host • comes with a plethora of modules, which form the basic building blocks for playbooks
  • 4. module processing ansible control host Playbook : oracle_user.yml --- - name: do some oracle stuff hosts: oracleservers gather_facts: False tasks: - name: create user test become: true become_user: oracle oracle_user: database_name: XE user_name: test password: test #!/opt/python/bin/python # -*- coding: utf-8 -*- ANSIBALLZ_WRAPPER = True import os import os.path import sys import __main__ scriptdir = None database host oraclexe ssh 1 2 3 4 {'changed': false, 'failed': true, 'msg': 'could not reach the host'} 5 module : oracle_user #!/opt/python/bin/python from ansible.module_utils.basic import AnsibleModule import os import cx_Oracle def create_user(cursor, user_name, password): create_user_statement = ''' 1.process playbook 2.generate python script 3.copy python script to target host(s) 4.execute script on target host(s) 5.parse the script‘s output
  • 5. expected output {'changed': false, 'failed': true, 'msg': 'could not reach the host'} • changed indicates that host state has been changed by the module • failed indicates that the module has not completed successfully • msg expand on the reason of the failure
  • 6. AnsibleModule from ansible.module_utils.basic import AnsibleModule • deals with parameter parsing • returns output in JSON
  • 7. let‘s get playing root@ansible:~/playbooks# cat oracle_user.yml --- - name: do some oracle stuff hosts: oracleservers gather_facts: False tasks: - name: create user test become: true become_user: oracle oracle_user: database_name: XE user_name: test password: test environment: LD_LIBRARY_PATH: /u01/app/oracle/product/11.2.0/xe/lib
  • 8. parameters def main(): module = AnsibleModule( argument_spec=dict( oracle_home=dict( type='str', default='/u01/app/oracle/product/11.2.0/xe', ), database_name=dict(required=True, type='str'), user_name=dict(required=True, type='str'), password=dict(required=True, type='str'), ), supports_check_mode=True, ) database_name=module.params['database_name'] user_name=module.params['user_name'] password=module.params['password'] oracle_home=module.params['oracle_home'] os.environ['ORACLE_HOME'] = oracle_home os.environ['ORACLE_SID'] = database_name
  • 9. error handling try: sysdba_connection = cx_Oracle.connect('/', mode=cx_Oracle.SYSDBA) cursor = sysdba_connection.cursor() except cx_Oracle.DatabaseError as exc: error, = exc.args formatted_error = error.message.replace('n', ' ')[:40] error_msg='unable to connect to database {} due to {}'.format( database_name, formatted_error, ) module.fail_json(msg=error_msg)
  • 10. error handling in action root@ansible:~/playbooks# ansible-playbook oracle_user.yml PLAY [do some oracle stuff] **************************************************** TASK [create schema test] ****************************************************** fatal: [oraclexe]: FAILED! => { "changed": false, "failed": true } MSG: unable to connect to database XA due to ORA-01034: ORACLE not available ORA-2710 to retry, use: --limit @/root/playbooks/oracle_schema.retry PLAY RECAP ********************************************************************* oraclexe : ok=0 changed=0 unreachable=0 failed=1 root@ansible:~/playbooks#
  • 11. Idempotence Operations can be applied multiple times without changing the result beyond the initial application.
  • 12. Idempotence as code if user_exists(cursor=cursor, user_name=user_name, password=password): module.exit_json(changed=False) else: if module.check_mode: module.exit_json(changed=True) create_user_result = create_user( cursor=cursor, user_name=user_name, password=password, ) cursor.close() if not create_user_result['status']: module.fail_json(msg=create_user_result['msg']) else: module.exit_json(changed=True, msg=create_user_result['msg'])
  • 13. Idempotence in action root@ansible:~/playbooks# ansible-playbook oracle_user.yml PLAY [do some oracle stuff] **************************************************** TASK [create user test] ****************************************************** ok: [oraclexe] PLAY RECAP ********************************************************************* oraclexe : ok=1 changed=0 unreachable=0 failed=0 root@ansible:~/playbooks#
  • 14. check mode • will not make any changes • but reports on the possible outcome
  • 15. check mode as code supports_check_mode=True, . . if user_exists(cursor=cursor, user_name=user_name, password=password): module.exit_json(changed=False) else: if module.check_mode: module.exit_json(changed=True) create_user_result = create_user( cursor=cursor, user_name=user_name, password=password, ) cursor.close() if not create_user_result['status']: module.fail_json(msg=create_user_result['msg']) else: module.exit_json(changed=True, msg=create_user_result['msg'])
  • 16. check mode in action root@ansible:~/playbooks# ansible-playbook oracle_user.yml --check PLAY [do some oracle stuff] **************************************************** TASK [create user test] ******************************************************** changed: [oraclexe] PLAY RECAP ********************************************************************* oraclexe : ok=1 changed=1 unreachable=0 failed=0 root@ansible:~/playbooks# SQL> select username from dba_users where username='TEST'; no rows selected SQL>
  • 17. the final curtain root@ansible:~/playbooks# ansible-playbook oracle_user.yml PLAY [do some oracle stuff] **************************************************** TASK [create user test] ******************************************************** changed: [oraclexe] PLAY RECAP ********************************************************************* oraclexe : ok=1 changed=1 unreachable=0 failed=0 root@ansible:~/playbooks# SQL> select username from dba_users where username='TEST'; USERNAME ------------------------------ TEST SQL>
  • 18. references • Hochstein L. and Moser R.(2017). Ansible Up & Running. O‘Reilly. • Ansible Module Development Walkthrough. [online] Available at: http://docs.ansible.com/ansible/latest/dev_guide/developing_modules_gener al.html