Ansible4Networking  Newsletter
Monthly Newsletter

Ansible4Networking Newsletter


#networkautomation #ansible #aap #infrastructureascode?

Welcome to the Ansible4Networking monthly newsletter!

Please subscribe to this newsletter to keep up to date with the latest developments in network automation and related events. Each newsletter will provide news on the latest features and emerging topics as well as a short Ansible tutorial. This month we will look at Ansible Lightspeed developments and switch gears to the set_stats module for a tutorial.

News:

Ansible Lightspeed with IBM Watson Code Assistant is AI that is purpose-built and applied for IT automation.

Technically Speaking features captivating conversations between Chris Wright and a rotating cast of experts and industry leaders around what's on the horizon for technology.

Lightspeed Checkout an overview of Lightspeed on Red Hat Technically Speaking with Chris Wright

Learn more about Ansible Lightspeed:

https://www.redhat.com/en/blog/automa...

https://www.redhat.com/en/about/press...


Ansible Tutorial: Artifacts and set_stats

The?set_stats?module stores variable "facts" from running a playbook/job-template job in the Automation Controller database. In turn, you are then able to pass state information "artifacts" between other playbooks / job templates in the same workflow.

Scenario: The following workflow will do the following:

  1. copy the running configuration from a router
  2. set_stat the config file as a fact
  3. make a change to the router's Loopback0 interface
  4. the set_stats artifact is used to complete a diff against the intended "copied" config and the router's running config

AAP 2.4 Workflow:

No alt text provided for this image
Ansible4Networking_set_stats Workflow


This playbook "copy_config.yml" will be the first node in a the "Ansible4Networking_set_stats" Workflow. Please see the usage of the set_stats module below:

---
- name: Copy router configs and make Artifact for future (job-template) playbooks
  hosts: rtr1
  gather_facts: False


  tasks:
    - name: Copy router configs
      cisco.ios.ios_config:
        backup: true
        backup_options:
          filename: "{{inventory_hostname}}.cfg"
          dir_path: /tmp/backups
      register: config_output


    - name: remove non config lines - regexp
      ansible.builtin.lineinfile:
        path: "{{ config_output.backup_path }}"
        line: "Building configuration..."
        state: absent
      delegate_to: localhost


    - name: Cat Configs
      ansible.builtin.shell:
        cmd: "cat /tmp/backups/{{inventory_hostname}}.cfg"
      register: backups


    - name: Artifact of Config Files
      ansible.builtin.set_stats:
        data:
          configs: "{{ backups.stdout }}"

        

This playbook "make_change.yml" is the next node in the workflow. A self service "survey" is used to populate the "{{ description }}" variable.

--
- name: Make a quick change 
  hosts: rtr1
  gather_facts: False
  tasks:
  - name: Merge provided configuration with device configuration
    cisco.ios.ios_interfaces:
      config:
        - name: Loopback0
          description: "{{ description }}"-        

This playbook "read_stats.yml" is the final node in the workflow. As you recall, the configs variable was defined and saved by set_stats in the first node of the workflow. Effectively, the artifact of the router config was passed from one playbook to another so we could compare the DIFF of the running and intended configuration. In this example we made a router change on purpose but the diff would also identify any unintended changes from the backup "intended" config .

---
- name: Read in Artifact of router configs to compare (diff)
  hosts: rtr1
  gather_facts: False
  tasks:
    - name: Diff against cisco ios configuration
      cisco.ios.config:
        intended_config: "{{ configs }}"
        diff_against: intended        

The Important Output:

Survey

No alt text provided for this image
Survey

Artifacts

No alt text provided for this image
Artifacts

The playbook "read_stats.yml" output

The '--- before' DIFF is the running configuration on the router. The --- after DIFF is the backup config "intended" saved as an artifact. Since we ran a second playbook to change the router's Loopback0 description, we can subsequently glean the change "DIFF" in red "- description tutorial".

No alt text provided for this image

Complete!

No alt text provided for this image

Other Links:

Ansible4Networking Meetup

Ansible4Networking Youtube

Ansible4Networking Upcoming Event

Looks interesting, thanks for sharing!

要查看或添加评论,请登录

Tony Dubiel的更多文章

社区洞察

其他会员也浏览了