Better Ansible Network Automation With Roles and Custom Modules
Better Ansible Network Automation With Roles and Custom Modules
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
What are we going to talk about?
• Ansible Roles vs. Modules
• What’s the difference?
• Let’s Make a Module
• Typically Python
• Self Documenting
• Idempotent
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
✓ I have contributed to Ansible
but there is much I don’t know.
Before we ✓ Ansible is evolving, true today
start… could be less true tomorrow.
✓ I always happy to know a
better way.
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
The name refers to the fictional
instantaneous hyperspace
communication system "ansible"
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Ansible Roles and Modules
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
What’s the difference?
Roles – The Instructions Modules – The Tools
• Self-contained and portable • Do One Thing Well
• Written in YAML • Python or PowerShell
• Use for common configurations • Uses APIs and/or CLI tools
• Enforcement of standards • Abstract complexity from user
• Called from a playbook • Called by an Ansible task
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s Make a Module
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s Make a Module – Python
• Create and Activate a
Python Virtual Environment
• python –m venv venv
• source venv/bin/activate
• Install Ansible
• pip install ansible
• Install other requirements
as needed
• pip install requests
• pip install pylint
https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s Make a Module – Module Sections
• Modules have distinct sections
• Sh-Bang
• DOCUMENTATION
• imports
• main()
• AnsibleModule Class
• Argument Spec
• Required If
• Supports Check Mode
• Determine State / Update if required
• Exit – with result
https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s Make a Module – Development and Testing
• Local Module Testing
• Ansible modules have an
implied JSON contract
• JSON IN → MODULE JSON OUT
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s Make a Role – Initialization
• Ansible Role Tools
• ansible-galaxy init <role-name>
<<: *login_info
https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Summing up
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
What did we talk about?
• The Difference between Ansible
Modules and Roles
• Tasks oriented
• Playbook oriented
• Consistency in Deployments
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Webinar Resource List
• Learning Labs
• Introduction to Ansible
https://developer.cisco.com/learning/modules/sdx-ansible-intro
• ACI and Ansible
https://developer.cisco.com/learning/modules/ansible-aci-intro
• DevNet Sandboxes
• ACI Always On https://snadboxapicdc.cisco.com
• Code Samples
• Ansible on DevNet’s Code Exchange
https://developer.cisco.com/codeexchange/explore/#search=ansible
• Webinar Code
https://developer.cisco.com/codeexchange/github/repo/movinalot/aci_modules
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
NetDevOps Live! Code Exchange Challenge
developer.cisco.com/codeexchange
Take what you’ve seen today
and apply to an active project.
Then submit it to Code
Exchange!
© 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public
Got more questions? Stay in touch!