Ansible:从输出中提取特定信息

Ansible:从输出中提取特定信息

我正在尝试从剧本的输出中获取一些信息并将其保存在文件中以供审查。tor.j2只是配置中预期行的列表:日志记录、ntp、生成树等。

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

运行详细代码后,-vvv最后一项是差异:“更新”。有没有办法获取此信息,以便我可以保存它以供审查?

输出(为简洁起见,已缩短):

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

谢谢您的指导。

答案1

简而言之(未经测试)。您可以调整日志文件的任何格式。

---
- 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

相关内容