将本地日期_时间附加到文件名

问题描述 投票:0回答:1

Ansible 专家, 我正在尝试创建一个剧本,它检索思科路由器和交换机的当前配置,然后将其保存到文件中。我还需要文件名附加本地日期时间。就像事实中的 ansible_date_time 字段一样,我在路由器/交换机中没有看到任何收集事实数据。所以我尝试从运行 ansible 的本地主机获取此信息。由于它不包含在主机清单文件中,因此我必须添加本地主机。 我面临的问题是,我成功地从 ansible_facts 获取了 date_time,但是这个注册值似乎不适用于文件名。我猜这是由于我的任务中的“when”语句造成的。不确定。

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]

我的输出文件名看起来是“LAB_TKYAT07LF01{'skip_reason': u'条件结果为 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'  
ios ansible nexus ansible-facts
1个回答
0
投票

将第二个任务更改为:

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

尽管你是对的:这并不能解释为什么跳过以下任务。

您可以尝试使用

setup
插件找出给定主机返回的值:

ansible -m setup my-nxos-target

请注意,文档会提到一些

cisco.nxos.nxos
cisco.ios.ios

参见:

© www.soinside.com 2019 - 2024. All rights reserved.