ansible save variable to file code example

Example: save output in txt ansible

---
- hosts: switches
  gather_facts: no
  vars:
    data_dir: foo
  tasks:

  - name: run commands to gather information
    ios_command:
      commands:
      - show version
      - show running
      - show snmp user
    register: switch_config

  - name: save collected infromation in data directory
    copy:
      dest: "output.txt"
      content: |
        show version
        -----
        {{ switch_config.stdout[0] }}

        show running-config
        -----
        {{ switch_config.stdout[1] }}

        show snmp user
        -----
        {{ switch_config.stdout[2] }}

Tags:

Misc Example