【问题标题】:Ansible - Append local date_time to a file nameAnsible - 将本地日期时间附加到文件名
【发布时间】:2021-06-18 04:09:44
【问题描述】:

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

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

【问题讨论】:

    标签: ios ansible nexus ansible-facts


    【解决方案1】:

    将您的第二个任务更改为:

    - 
      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.nxoscisco.ios.ios

    见:

    【讨论】:

    • 感谢您的回复。我已经尝试过了,在任务“TASK [将 Nexus 输出保存到输出文件夹]”中出现了如下致命错误:致命:[LAB_TKYAT07SPN01]:失败! => {"msg": "该任务包含一个带有未定义变量的选项。错误是:'currenttime' is undefined
    • 我还必须删除“运行一次:真”。因为在第一台主机上跳过了播放,不幸的是我的路由器。我还添加了一个调试“当前时间:在您提出的任务之后,我能够看到日期。
    • 对。我已经编辑了我的答案:尝试使用 run_once 和 delegate_to,删除时间。这应该确保为非本地主机目标设置当前时间事实。实际上,run_once 不是必需的,尽管多次获取日期是没有意义的。
    • 谢谢你。这就像一个冠军。对我来说,一个要点是,如果在具有“何时”条件的任务中注册了一个变量,则该变量将不会在另一个具有“何时”条件的任务中定义。
    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多