iosansiblenexusansible-facts

Append local date_time to a file name


Ansible Experts, I am trying to create a play-book, that retrieves current configuration of Cisco routers and switches, then to save it to a file. I also need the file name to append with local datetime. Like ansible_date_time field in facts, i did not see any in routers/switches gather_facts data. So i tried to get this information from local host where i run ansible. As it is not included in the host inventory file, i had to add a localhost. The problem i face is, i am successfully getting date_time from ansible_facts, however this registered value seems to be not working with filename. I am guessing it is due to the "when" statement in my tasks. Not sure.

TASK [Display Ansible date_time fact and register] *******************************************************************************************
skipping: [LAB_TKYAT07EXTRT03]
skipping: [LAB_TKYAT07EXTRT04]
ok: [localhost] => {
    "msg": "2021-06-18"
}
skipping: [LAB_TKYAT07SPN01]
skipping: [LAB_TKYAT07SPN02]
skipping: [LAB_TKYAT07LF01]
skipping: [LAB_TKYAT07LF02]
skipping: [LAB_TKYAT07LF03]
skipping: [LAB_TKYAT07LF04]

My output filename looks "LAB_TKYAT07LF01{'skip_reason': u'Conditional result was False', 'skipped': True, 'changed': False}_showrunn.txt"

--- 
- 
  connection: network_cli
  gather_facts: false
  hosts: LAB_ALL

  tasks:   
    - 
      name: gather facts from apps
      setup:
        gather_subset:
          - hardware
      when: ansible_connection == 'local'   
      tags: linux

    - 
      name: "Display Ansible date_time fact and register"
      debug: 
        msg: "{{ ansible_date_time.date }}"
      when: ansible_connection == 'local'  
      register: currenttime
      tags: linux

    - 
      name: "retrieve show runn all from Nexus "
      nxos_command: 
        commands: 
        - command: show runn all
      register: nexus_output
      when: ansible_network_os == 'nxos'
    
    - 
      name: "Save Nexus output to outputs folder"
      copy:
        content: "{{ nexus_output.stdout[0] }}"
        dest: "./outputs/{{ inventory_hostname }}{{ currenttime }}_showrunn.txt"
      when: ansible_network_os == 'nxos'   

    - 
      name: "retrieve show runn all from Routers "
      ios_command: 
        commands: 
        - command: show runn all
      register: ios_output
      when: ansible_network_os == 'ios'

    - 
      name: "Save IOS output to outputs folder"
      copy:
        content: "{{ ios_output.stdout[0] }}"
        dest: "./outputs/{{ inventory_hostname }}{{ currenttime }}_showrunn.txt"
      when: ansible_network_os == 'ios'  

Solution

  • Change your second task to:

    - 
      name: "Display Ansible date_time fact and register"
      delegate_to: localhost
      run_once: yes
      set_fact: 
        currenttime: "{{ ansible_date_time.date }}"
      tags: linux
    

    Although you are right: this would not explain why the following task are being skipped.

    You can try to figure out which value is returned for a given hosts, using the setup plugin:

    ansible -m setup my-nxos-target
    

    Note that the docs would mention some cisco.nxos.nxos or cisco.ios.ios instead.

    See: