SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Create Your Own Module
Yan Kurniawan, March 2015
EXTENDING ANSIBLE
ABOUT ME
• Cloud Engineer at Flux7 (http://flux7.com), a US-based AWS
Consulting Partner
• Writer of “Ansible for AWS” book
http://leanpub.com/ansible-for-aws
ANSIBLE MODULES
• "batteries-included" philosophy
Useful out-of-the-box modules
• Over 200 core modules available
http://docs.ansible.com/modules_by_category.html
ANSIBLE MODULE
PUPPET MODULE
PUPPET BUILT-IN RESOURCE TYPES
augeas
computer
cron
exec
file
filebucket
group
host
interface
k5login
macauthorization
mailalias
maillist
mcx
mount
nagios_command
nagios_contact
nagios_contactgroup
nagios_host
nagios_hostdepende
ncy
nagios_hostescalatio
n
nagios_hostextinfo
nagios_hostgroup
nagios_service
nagios_servicedepen
dency
nagios_serviceescala
tion
nagios_serviceextinf
o
nagios_servicegroup
nagios_timeperiod
notify
package
resources
router
schedule
scheduled_task
selboolean
selmodule
service
ssh_authorized_key
sshkey
stage
tidy
user
vlan
yumrepo
zfs
zone
Zpool
ANSIBLE CLOUD MODULES
• Amazon (AWS)
• Azure
• Digital Ocean
• Docker
• Google
• Linode
• Openstack
• Rackspace
• VMWare
AWS MODULES
• CloudFormation
• EC2
• VPC
• Security Groups
• AMI
• Auto Scaling Group
• ELB
• CloudWatch
• RDS
• S3
• Route53
EC2 MODULE
• create, terminate, start or stop an AWS EC2 instance
• http://docs.ansible.com/ec2_module.html
EXAMPLE – LAUNCH AWS EC2
- name: launch EC2 instance
ec2:
region: ap-southeast-2
key_name: mykey
instance_type: t2.micro
image: ami-xxxxxx
wait: yes
group: mysecgroup
instance_tags:
Name: test01
env: test
WRITING CUSTOM MODULE
• Any language (Python, Bash, C++, PHP, clojure, Ruby, etc); the only
requirement is being able to read/write files and write to stdout
• Output of the module should be in JSON format
• Recommended: Python
 Boilerplate available; reduce the amount of code required
 Arguments are handled automatically
 Output is automatically converted to JSON
 You can learn from Ansible core modules
https://github.com/ansible/ansible-modules-core
 Chance to contribute to the Ansible project
DIRECTORY STRUCTURE
group_vars/
library/ put custom modules here
roles/
playbook.yml
THE BOILERPLATE
def main():
module = AnsibleModule(
argument_spec = dict(
arg1 = dict(choices=SOME_CHOICES),
arg2 = dict(required=False)
),
)
# Your code here
# import module snippets
from ansible.module_utils.basic import *
main()
DEMO
• A simple module to return Amazon EC2 instance id(s)
using tags as input argument.
• Save the module as instance_lookup in
library/ directory
• Use the return value to start/stop/terminate
instance(s) using Ansible ec2 module.
CHOICES
AWS_REGIONS = ['ap-northeast-1',
'ap-southeast-1',
'ap-southeast-2',
'eu-central-1',
'eu-west-1',
'sa-east-1',
'us-east-1',
'us-west-1',
'us-west-2']
HANDLING EXCEPTION
import sys
try:
from boto.ec2 import connect_to_region
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
ARGUMENTS PARSING
def main():
module=AnsibleModule(
argument_spec=dict(
region=dict(choices=AWS_REGIONS),
tags=dict(default=None, type='dict'),
)
)
params = module.params
region = params['region']
tags = params['tags']
SOME CODES
if region:
try:
ec2 = connect_to_region(region)
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg=str(e))
else:
module.fail_json(msg="region must be specified")
instance_ids = []
for tag, value in tags.iteritems():
for instance in ec2.get_only_instances
(filters={'tag:' + tag: value}):
instance_ids.append(instance.id)
BOTO LIBRARY
get_only_instances
http://boto.readthedocs.org/en/latest/ref/ec2.htm
l#boto.ec2.connection.EC2Connection.get_only_inst
ances
HANDLING RETURN
module.exit_json(changed=False,
instance_ids=instance_ids)
THE PLAYBOOK
- name: get instance id
instance_lookup:
region: "{{ region }}”
tags:
Name: test01
register: instanceid
- name: start instance
ec2:
region: "{{ region }}"
instance_ids: "{{ instanceid.instance_ids }}"
state: running
wait: yes
when: instanceid is defined
MORE RESOURCES
• Module development guideline
http://docs.ansible.com/developing_modules.html
• Boto reference
https://boto.readthedocs.org/en/latest/
Source code available at http://git.io/pFEJ
THANK YOU

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

DevOps with Ansible
DevOps with AnsibleDevOps with Ansible
DevOps with Ansible
 
Ansible API
Ansible APIAnsible API
Ansible API
 
03 ansible towerbestpractices-nicholas
03 ansible towerbestpractices-nicholas03 ansible towerbestpractices-nicholas
03 ansible towerbestpractices-nicholas
 
Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015
 
Packer, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec AzurePacker, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec Azure
 
Using Ansible at Scale to Manage a Public Cloud
Using Ansible at Scale to Manage a Public CloudUsing Ansible at Scale to Manage a Public Cloud
Using Ansible at Scale to Manage a Public Cloud
 
Server Check.in case study - Drupal and Node.js
Server Check.in case study - Drupal and Node.jsServer Check.in case study - Drupal and Node.js
Server Check.in case study - Drupal and Node.js
 
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 TaiwanAutomating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
 
Ansible + WordPress
Ansible + WordPressAnsible + WordPress
Ansible + WordPress
 
Configuration primer
Configuration primerConfiguration primer
Configuration primer
 
Ansible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOTAnsible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOT
 
Ansible + WordPress - WordCamp Toronto 2016
Ansible + WordPress - WordCamp Toronto 2016Ansible + WordPress - WordCamp Toronto 2016
Ansible + WordPress - WordCamp Toronto 2016
 
Ansible
AnsibleAnsible
Ansible
 
Neil Peterson - Azure CLI Deep Dive
Neil Peterson - Azure CLI Deep DiveNeil Peterson - Azure CLI Deep Dive
Neil Peterson - Azure CLI Deep Dive
 
Ansible Berlin Meetup Intro talk by @danvaida
Ansible Berlin Meetup Intro talk by @danvaidaAnsible Berlin Meetup Intro talk by @danvaida
Ansible Berlin Meetup Intro talk by @danvaida
 
Network Automation: Ansible 101
Network Automation: Ansible 101Network Automation: Ansible 101
Network Automation: Ansible 101
 
AWS CodeDeploy - basic intro
AWS CodeDeploy - basic introAWS CodeDeploy - basic intro
AWS CodeDeploy - basic intro
 
Infrastructure as Code
Infrastructure as CodeInfrastructure as Code
Infrastructure as Code
 
goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020
goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020
goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020
 
Technologies for Data Analytics Platform
Technologies for Data Analytics PlatformTechnologies for Data Analytics Platform
Technologies for Data Analytics Platform
 

Andere mochten auch

Andere mochten auch (6)

Deploying and Managing Red Hat Enterprise Linux in Amazon Web Services
Deploying and Managing Red Hat Enterprise Linux in Amazon Web ServicesDeploying and Managing Red Hat Enterprise Linux in Amazon Web Services
Deploying and Managing Red Hat Enterprise Linux in Amazon Web Services
 
AnsibleFest London 2016 - managing your cisco datacenter network with ansible
AnsibleFest London 2016 - managing your cisco datacenter network with ansibleAnsibleFest London 2016 - managing your cisco datacenter network with ansible
AnsibleFest London 2016 - managing your cisco datacenter network with ansible
 
CodeTalks Vortrag: Automatisierung mit Ansible & Jenkins @ LeanIX Enterprise ...
CodeTalks Vortrag: Automatisierung mit Ansible & Jenkins @ LeanIX Enterprise ...CodeTalks Vortrag: Automatisierung mit Ansible & Jenkins @ LeanIX Enterprise ...
CodeTalks Vortrag: Automatisierung mit Ansible & Jenkins @ LeanIX Enterprise ...
 
One tool, two fabrics: Ansible and Nexus 9000
One tool, two fabrics: Ansible and Nexus 9000One tool, two fabrics: Ansible and Nexus 9000
One tool, two fabrics: Ansible and Nexus 9000
 
Network Automation with Ansible
Network Automation with AnsibleNetwork Automation with Ansible
Network Automation with Ansible
 
The power of symmetry
The power of symmetryThe power of symmetry
The power of symmetry
 

Ähnlich wie Extending ansible

Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
Drupalcon Paris
 
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Campmodern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
Puppet
 

Ähnlich wie Extending ansible (20)

Ansible - Hands on Training
Ansible - Hands on TrainingAnsible - Hands on Training
Ansible - Hands on Training
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
Ansible Tutorial.pdf
Ansible Tutorial.pdfAnsible Tutorial.pdf
Ansible Tutorial.pdf
 
DevOps for database
DevOps for databaseDevOps for database
DevOps for database
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4
 
Ansible intro
Ansible introAnsible intro
Ansible intro
 
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
Ansible 2.0 - How to use Ansible to automate your applications in AWS.Ansible 2.0 - How to use Ansible to automate your applications in AWS.
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
 
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
Ansible 2.0 - How to use Ansible to automate your applications in AWS.Ansible 2.0 - How to use Ansible to automate your applications in AWS.
Ansible 2.0 - How to use Ansible to automate your applications in AWS.
 
Ansible on aws - Pop-up Loft Tel Aviv
Ansible on aws - Pop-up Loft Tel AvivAnsible on aws - Pop-up Loft Tel Aviv
Ansible on aws - Pop-up Loft Tel Aviv
 
Ansible & Salt - Vincent Boon
Ansible & Salt - Vincent BoonAnsible & Salt - Vincent Boon
Ansible & Salt - Vincent Boon
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
 
Ansible Introduction
Ansible IntroductionAnsible Introduction
Ansible Introduction
 
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Campmodern module development - Ken Barber 2012 Edinburgh Puppet Camp
modern module development - Ken Barber 2012 Edinburgh Puppet Camp
 
Ansible get started
Ansible get startedAnsible get started
Ansible get started
 
Development of Ansible modules
Development of Ansible modulesDevelopment of Ansible modules
Development of Ansible modules
 
Apache maven and its impact on java 9 (Java One 2017)
Apache maven and its impact on java 9 (Java One 2017)Apache maven and its impact on java 9 (Java One 2017)
Apache maven and its impact on java 9 (Java One 2017)
 
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfTop 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdf
 
AWS ElasticBeanstalk Advanced configuration
AWS ElasticBeanstalk Advanced configurationAWS ElasticBeanstalk Advanced configuration
AWS ElasticBeanstalk Advanced configuration
 
Domas monkus drupal module development
Domas monkus drupal module developmentDomas monkus drupal module development
Domas monkus drupal module development
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

Extending ansible