Adding Netbox
This commit is contained in:
13
roles/ansible-network.cisco_ios/includes/args_adapter.yaml
Normal file
13
roles/ansible-network.cisco_ios/includes/args_adapter.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# Playbook to ingest config manager args into ios provider specific args
|
||||
#
|
||||
|
||||
- name: convert config_manager_text
|
||||
set_fact:
|
||||
ios_config_text: "{{ config_manager_text }}"
|
||||
when: config_manager_text is defined
|
||||
|
||||
- name: convert config_manager_file
|
||||
set_fact:
|
||||
ios_config_file: "{{ config_manager_file }}"
|
||||
when: config_manager_file is defined
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
- name: validate ios_checkpoint_filename is defined
|
||||
fail:
|
||||
msg: "missing required var: ios_checkpoint_filename"
|
||||
when: ios_checkpoint_filename is undefined
|
||||
|
||||
- name: get current files on disk
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove old checkpoint file (if necessary)
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_checkpoint_filename }}"
|
||||
when: ios_checkpoint_filename in ios_dir_listing.stdout
|
||||
|
||||
# copy the current running-config to the local flash disk on the target device.
|
||||
# This will be used both for restoring the current config if a failure happens
|
||||
# as well as performing a configuration diff once the new config has been
|
||||
# loaded.
|
||||
- name: create a checkpoint of the current running-config
|
||||
ios_command:
|
||||
commands:
|
||||
- command: "copy running-config flash:{{ ios_checkpoint_filename }}"
|
||||
prompt: ["\\? "]
|
||||
answer: "{{ ios_checkpoint_filename }}"
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
- name: validate ios_checkpoint_filename is defined
|
||||
fail:
|
||||
msg: "missing required var: ios_checkpoint_filename"
|
||||
when: ios_checkpoint_filename is undefined
|
||||
|
||||
- name: get current files on disk
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove checkpoint file from remote device
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_checkpoint_filename }}"
|
||||
when: ios_checkpoint_filename in ios_dir_listing.stdout
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
- name: validate ios_checkpoint_filename is defined
|
||||
fail:
|
||||
msg: "missing required var: ios_checkpoint_filename"
|
||||
when: ios_checkpoint_filename is undefined
|
||||
|
||||
- name: get current files on disk
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: verify checkpoint file exists
|
||||
fail:
|
||||
msg: "missing checkpoint file {{ ios_checkpoing_filename }}"
|
||||
when: ios_checkpoint_filename not in ios_dir_listing.stdout
|
||||
|
||||
- name: checkpoint configuration restore pre hook
|
||||
include_tasks: "{{ ios_checkpoint_restore_pre_hook }}"
|
||||
when: ios_checkpoint_restore_pre_hook is defined
|
||||
|
||||
- name: restore checkpoint configuration
|
||||
cli:
|
||||
command: "config replace flash:/{{ ios_checkpoint_filename }} force"
|
||||
register: ios_rollback_results
|
||||
|
||||
- name: checkpoint configuration restore post hook
|
||||
include_tasks: "{{ ios_checkpoint_restore_post_hook }}"
|
||||
when: ios_checkpoint_restore_post_hook is defined
|
||||
|
||||
- name: remove checkpoint file from remote device
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_checkpoint_filename }}"
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
- name: validate ios_config_text is defined
|
||||
fail:
|
||||
msg: "missing required arg: ios_config_text"
|
||||
when: ios_config_text is undefined
|
||||
|
||||
- name: set the ios_config_temp_file name
|
||||
set_fact:
|
||||
ios_config_temp_file: "tmp_ansible"
|
||||
|
||||
- name: create temp working dir
|
||||
tempfile:
|
||||
state: directory
|
||||
register: ios_config_temp_dir
|
||||
|
||||
- name: write the config text to disk
|
||||
copy:
|
||||
content: "{{ ios_config_text }}"
|
||||
dest: "{{ ios_config_temp_dir.path }}/{{ ios_config_temp_file }}"
|
||||
|
||||
- name: get current list of files on remote device
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove temporary files from target device
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_config_temp_file }}"
|
||||
when: ios_config_temp_file in ios_dir_listing.stdout
|
||||
|
||||
- name: enable the ios scp server
|
||||
cli:
|
||||
command: "{{ line }}"
|
||||
loop:
|
||||
- configure terminal
|
||||
- ip scp server enable
|
||||
- end
|
||||
loop_control:
|
||||
loop_var: line
|
||||
|
||||
- name: copy configuration to device
|
||||
net_put:
|
||||
src: "{{ ios_config_temp_dir.path }}/{{ ios_config_temp_file }}"
|
||||
dest: "flash:/{{ ios_config_temp_file }}"
|
||||
changed_when: false
|
||||
|
||||
- name: merge with current active configuration
|
||||
cli:
|
||||
command: "{{ line }}"
|
||||
loop:
|
||||
- "copy flash:/{{ ios_config_temp_file }} force"
|
||||
- "delete /force flash:/{{ ios_config_temp_file }}"
|
||||
loop_control:
|
||||
loop_var: line
|
||||
|
||||
- name: remove local temp working dir
|
||||
file:
|
||||
path: "{{ ios_config_temp_dir.path }}"
|
||||
state: absent
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
- name: validate ios_config_text is defined
|
||||
fail:
|
||||
msg: "missing required arg: ios_config_text"
|
||||
when: ios_config_text is undefined
|
||||
|
||||
- name: set the ios_config_temp_file name
|
||||
set_fact:
|
||||
ios_config_temp_file: "tmp_ansible"
|
||||
|
||||
- name: create temp working dir
|
||||
tempfile:
|
||||
state: directory
|
||||
register: ios_config_temp_dir
|
||||
|
||||
- name: write the config text to disk
|
||||
copy:
|
||||
content: "{{ ios_config_text }}"
|
||||
dest: "{{ ios_config_temp_dir.path }}/{{ ios_config_temp_file }}"
|
||||
|
||||
- name: get current list of files on remote device
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove temporary files from target device
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_config_temp_file }}"
|
||||
when: ios_config_temp_file in ios_dir_listing.stdout and ios_config_remove_temp_files
|
||||
|
||||
- name: enable the ios scp server
|
||||
cli:
|
||||
command: "{{ line }}"
|
||||
loop:
|
||||
- configure terminal
|
||||
- ip scp server enable
|
||||
- end
|
||||
loop_control:
|
||||
loop_var: line
|
||||
|
||||
- name: copy configuration to device
|
||||
net_put:
|
||||
src: "{{ ios_config_temp_dir.path }}/{{ ios_config_temp_file }}"
|
||||
dest: "flash:/{{ ios_config_temp_file }}"
|
||||
changed_when: false
|
||||
|
||||
- name: replace current active configuration
|
||||
cli:
|
||||
command: "{{ line }}"
|
||||
loop:
|
||||
- "config replace flash:/{{ ios_config_temp_file }} force"
|
||||
- "delete /force flash:/{{ ios_config_temp_file }}"
|
||||
loop_control:
|
||||
loop_var: line
|
||||
|
||||
- name: remove local temp working dir
|
||||
file:
|
||||
path: "{{ ios_config_temp_dir.path }}"
|
||||
state: absent
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
# this block is responsible for loading the configuration on to the target
|
||||
# device line by line from config model.
|
||||
- name: load configuration onto target device
|
||||
block:
|
||||
- name: load configuration lines into target device
|
||||
block:
|
||||
- name: extract banners from configs if present
|
||||
extract_banners:
|
||||
config: "{{ ios_config_text }}"
|
||||
register: result
|
||||
|
||||
- name: load configuration lines into target device except banner
|
||||
cli_config:
|
||||
config: "{{ result['config'] }}"
|
||||
register: ios_config_output
|
||||
|
||||
- name: enter configuration mode
|
||||
cli:
|
||||
command: "configure terminal"
|
||||
|
||||
- name: load banner lines into target device
|
||||
cli_command:
|
||||
command: "{{ item }}"
|
||||
sendonly: true
|
||||
with_items: "{{ result['banners'] }}"
|
||||
register: banner_config_output
|
||||
|
||||
- name: exit configuration mode
|
||||
cli:
|
||||
command: end
|
||||
|
||||
rescue:
|
||||
- name: exit configuration mode
|
||||
cli:
|
||||
command: end
|
||||
|
||||
- name: set host failed
|
||||
fail:
|
||||
msg: "error loading configuration lines"
|
||||
when: not ansible_check_mode
|
||||
34
roles/ansible-network.cisco_ios/includes/init.yaml
Normal file
34
roles/ansible-network.cisco_ios/includes/init.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
---
|
||||
- name: set role basic facts
|
||||
set_fact:
|
||||
ansible_network_ios_path: "{{ role_path }}"
|
||||
ansible_network_ios_version: "v2.7.0"
|
||||
|
||||
- name: display the role version to stdout
|
||||
debug:
|
||||
msg: "ansible_network.cisco_ios version is {{ ansible_network_ios_version }}"
|
||||
|
||||
- name: validate ansible_network_os == 'ios'
|
||||
fail:
|
||||
msg: "expected ansible_network_os to be `ios`, got `{{ ansible_network_os }}`"
|
||||
when: ansible_network_os != 'ios'
|
||||
|
||||
- name: validate ansible_connection == 'network_cli'
|
||||
fail:
|
||||
msg: "expected ansible_network to be `network_cli`, got `{{ ansible_connection }}`"
|
||||
when: ansible_connection != 'network_cli'
|
||||
|
||||
- name: Validate we have required installed version of dependent roles
|
||||
verify_dependent_role_version:
|
||||
role_path: "{{ role_path }}"
|
||||
depends_map:
|
||||
- name: 'ansible-network.network-engine'
|
||||
version: "{{ ios_network_engine_req_ver_override }}"
|
||||
when: ios_dependent_role_check is defined and ios_dependent_role_check
|
||||
and ios_network_engine_req_ver_override is defined
|
||||
|
||||
- name: Validate we have required installed version of dependent roles from meta
|
||||
verify_dependent_role_version:
|
||||
role_path: "{{ role_path }}"
|
||||
when: ios_dependent_role_check is defined and ios_dependent_role_check
|
||||
and ios_network_engine_req_ver_override is not defined
|
||||
25
roles/ansible-network.cisco_ios/includes/run_cli.yaml
Normal file
25
roles/ansible-network.cisco_ios/includes/run_cli.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
- name: run cli command pre hook
|
||||
include_tasks: "{{ ios_run_cli_command_pre_hook }}"
|
||||
when: ios_run_cli_command_pre_hook is defined and ios_run_cli_command_pre_hook
|
||||
|
||||
- name: run command and parse output
|
||||
cli:
|
||||
command: "{{ ios_command }}"
|
||||
parser: "{{ parser }}"
|
||||
engine: "{{ ios_parser_engine | default(None) }}"
|
||||
name: "{{ ios_name | default(None) }} "
|
||||
with_first_found:
|
||||
- files:
|
||||
- "{{ ios_parser }}"
|
||||
paths:
|
||||
- "{{ playbook_dir }}/parser_templates/ios"
|
||||
- "~/.ansible/ansible_network/parser_templates/ios"
|
||||
- "/etc/ansible/ansible_network/parser_templates/ios"
|
||||
- "{{ role_path }}/parser_templates"
|
||||
loop_control:
|
||||
loop_var: parser
|
||||
|
||||
- name: run cli command post hook
|
||||
include_tasks: "{{ ios_run_cli_command_post_hook }}"
|
||||
when: ios_run_cli_command_post_hook is defined and ios_run_cli_command_post_hook
|
||||
102
roles/ansible-network.cisco_ios/includes/wrapper.yaml
Normal file
102
roles/ansible-network.cisco_ios/includes/wrapper.yaml
Normal file
@@ -0,0 +1,102 @@
|
||||
---
|
||||
- name: initialize function
|
||||
include_tasks: includes/init.yaml
|
||||
|
||||
- name: validate ios_config_includes is defined
|
||||
fail:
|
||||
msg: "missing required arg: ios_config_includes"
|
||||
when: ios_config_includes is undefined
|
||||
|
||||
- name: set ios checkpoint filename
|
||||
set_fact:
|
||||
ios_checkpoint_filename: "chk_ansible"
|
||||
|
||||
# initiate creating a checkpoint of the existing running-config
|
||||
- name: create checkpoint of current configuration
|
||||
include_tasks: "{{ role_path }}/includes/checkpoint/create.yaml"
|
||||
|
||||
- name: configure the target device
|
||||
block:
|
||||
# iterate over the set of includes to configure the device
|
||||
- name: iterate over configuration tasks
|
||||
include_tasks: "{{ task }}"
|
||||
loop: "{{ ios_config_includes }}"
|
||||
loop_control:
|
||||
loop_var: task
|
||||
|
||||
rescue:
|
||||
# since the host has failed during the configuration load, the role by
|
||||
# default will initiate a restore sequence. the restore sequence will
|
||||
# load the previous running-config with the replace option enabled.
|
||||
- name: display message
|
||||
debug:
|
||||
msg: "error configuring device, starting rollback"
|
||||
when: ios_config_rollback_enabled
|
||||
|
||||
- name: initiate configuration rollback
|
||||
include_tasks: "{{ role_path }}/includes/checkpoint/restore.yaml"
|
||||
|
||||
- name: display message
|
||||
debug:
|
||||
msg: "successfully completed configuration rollback"
|
||||
when: ios_config_rollback_enabled
|
||||
|
||||
- name: fail host due to config load error
|
||||
fail:
|
||||
msg: "error loading configuration onto target device"
|
||||
|
||||
- name: set the ios_active_config fact
|
||||
set_fact:
|
||||
ios_active_config: "cfg_ansible"
|
||||
|
||||
# check if any reminents are left over from a previous run and remove them
|
||||
# prior to starting the configuration tasks.
|
||||
- name: check if stale temporarary files exist on target device
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove temporary files from target device
|
||||
cli:
|
||||
command: "delete /force flash:/{{ ios_active_config }}"
|
||||
when: ios_active_config in ios_dir_listing.stdout
|
||||
|
||||
# copy the updated running-config to the local flash device to be used to
|
||||
# generate a configuration diff between the before and after
|
||||
# running-configurations.
|
||||
- name: copy running-config to active config
|
||||
ios_command:
|
||||
commands:
|
||||
- command: "copy running-config flash:{{ ios_active_config }}"
|
||||
prompt: ["\\? "]
|
||||
answer: "{{ ios_active_config }}"
|
||||
|
||||
# generate the configuration diff and display the diff to stdout. only set
|
||||
# changed if there are lines in the diff that have changed
|
||||
- name: generate ios diff
|
||||
cli:
|
||||
command: "show archive config differences flash:{{ ios_checkpoint_filename }} flash:{{ ios_active_config }}"
|
||||
register: ios_config_diff
|
||||
changed_when: "'No changes were found' not in ios_config_diff.stdout"
|
||||
|
||||
- name: display config diff
|
||||
debug:
|
||||
msg: "{{ ios_config_diff.stdout.splitlines() }}"
|
||||
when: not ansible_check_mode
|
||||
|
||||
# refresh the list of files currently on the target network device flash
|
||||
# drive and remote all temp files
|
||||
- name: update local directory listing
|
||||
cli:
|
||||
command: dir
|
||||
register: ios_dir_listing
|
||||
|
||||
- name: remove remote temp files from flash
|
||||
cli:
|
||||
command: "delete /force flash:/{{ filename }}"
|
||||
loop:
|
||||
- "{{ ios_active_config }}"
|
||||
- "{{ ios_checkpoint_filename }}"
|
||||
loop_control:
|
||||
loop_var: filename
|
||||
when: filename in ios_dir_listing.stdout
|
||||
Reference in New Issue
Block a user