Professional Documents
Culture Documents
Creating Reusable Code
Creating Reusable Code
Ansible Code
Ivan Pepelnjak (ip@ipSpace.net)
Network Architect
ipSpace.net AG
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Creating Reusable Ansible Code
Challenges:
• Create reusable bits of code
• Use the same modules in multiple projects
Ansible Includes
• Playbook-level includes (include plays or whole playbooks)
• Play-level includes (include tasks)
Ansible Roles
• Include tasks, templates, plugins
• Specify default variables
• Overwrite variables
Further options
• Plugins
• Jinja2 macros
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Ansible Include Functionality
Include command
• Playbook-level: include plays
• Play-level: include tasks
Features
• Specify included file name as an expression
• Specify variables (parameters) for included tasks/plays
• Use looping construct (with_*) on included tasks for complex loops
- hosts: all
tasks:
- template: src=ios/common.j2 dest=configs/{{inventory_hostname}}.txt
name: create configuration for IOS devices
when: ansible_os == 'ios'
- hosts: all
tasks:
- template: src=nxos/common.j2 dest=configs/{{inventory_hostname}}.txt
name: create configuration for Nexus devices
when: ansible_os == 'nxos'
- hosts: all
tasks:
- template: src=ios/common.j2 dest=configs/{{inventory_hostname}}.txt
when: ansible_os == 'ios'
setup-directory.yml
---
- hosts: localhost Included file contains
name: Create directory one or more plays
tasks:
- file: path=configs state=absent
when: cleanup is defined
- file: path=configs state=directory
setup-directory.yml
---
- hosts: localhost
name: Create directory
tasks:
- file: path=configs state=absent Directory is removed
when: cleanup is defined (cleanup is defined)
- file: path=configs state=directory
ios-config.yml
Included file contains list of tasks
---
- template: template path based on included file path
src: ../ios/common.j2
dest: configs/{{inventory_hostname}}.txt
name: create configuration for IOS devices
Output files that are normally on remote host are relative to the original
directory
• Destination in template module
• Files managed with file module or changed with lineinfile or blockinfile
module
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Includes and Loops
Sad fact
• Ansible loops repeat a single task
• Loops don’t work with blocks
• You must use includes to loop over more than one task
How-to:
• Specify with_* iterator on an include directive
• Included task list is executed multiple times
• Use item variable in included tasks like you would in a single task
- hosts: nxos
tasks:
- nxos_command:
commands: ping {{item}}
provider: "{{cli}}"
with_items: "{{ping_target}}"
register: result
failed_when: "not 'icmp_seq' in result.stdout[0]"
- assert:
that: result.stdout[0] is defined
msg: Ping command crashed
- assert:
that: "'!!' in result.stdout[0]"
msg: "Cannot ping {{item}} from {{ansible_host}}"
- include: exec/{{ansible_os}}-ping.yml
with_items: targets
OS-specific ping command is
executed once for each target
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
What Are Ansible Roles?
galaxy.ansible.com/about
docs.ansible.com/ansible/playbooks_roles.html
Role-specific search paths are enabled only for tasks included from the role
roles/network_device/tasks/main.yml
--- Template fetched from role
- template: templates directory
src: {{ansible_os}}-common.j2
dest: {{configs}}/00-common-{{inventory_hostname}}.inc
name: create common device configuration
Output file is still relative to
original working directory
---
- template:
src: {{ansible_os}}-ospf.j2
dest: {{configs}}/10-ospf-{{inventory_hostname}}.inc
name: create OSPF routing process
roles/ospf_router/meta/main.yml
---
OSPF router is a network device
dependencies:
- network_device
pre_tasks:
- file: path={{configs}} state=directory
roles:
- ospf_router
post_tasks:
- assemble:
src: "{{configs}}"
dest: "configs/{{inventory_hostname}}.cfg"