Score:1

Ansible: Pulling a specifc piece of information from an output

ba flag

I'm trying to grab some information from the output of a playbook and save in a file for review. tor.j2 is just a list of expected lines in the config: logging , ntp, spanning-tree, etc.

 tasks:
 - name: Check the config 
     nxos_config:
       src: ./tor.j2
       defaults: true
     check_mode: yes

After running the code with a -vvv verbosity the last item in there is the differences: "updates". Is there any way to grab this information so I can then save it in order to review?

Output (shortened for brevity):

changed: [switch1] => {     
  "ansible_facts": 
    "discovered_interpreter_python": "/usr/bin/python" 
…
  "updates": [
    "feature ntp"
     ] }

Thanks for any guidance.

Score:0
fr flag

In a nutshell (untested). You can adapt to any format you want for the log file.

---
- name: Check config and log potential updates
  hosts: all
  
  tasks:
    - name: Compare target config with existing one
      cisco.nxos.nxos_config:
        src: ./tor.j2
        defaults: true
      check_mode: yes
      register: config_check

    - name: Log potential updates to a local file
      ansible.builtin.lineinfile:
        path: /tmp/config_differences.csv
        line: '"{{ inventory_hostname }}","{{ config_check.updates | to_json }}"'
      # Make sure we don't run into a write concurrency problem
      throttle: 1
      delegate_to: localhost
JonC avatar
ba flag
Thanks for the example, I should get a chance to try this out and will let you know.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.