如何解决Ansible - 将本地 date_time 附加到文件名
Ansible 专家, 我正在尝试创建一个剧本,检索 Cisco 路由器和交换机的当前配置,然后将其保存到文件中。我还需要文件名附加本地日期时间。就像事实中的 ansible_date_time 字段一样,我在路由器/交换机gather_facts 数据中没有看到任何内容。因此,我尝试从运行 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'Conditional result was False','skiipped': 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'
解决方法
将您的第二个任务更改为:
-
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
。
见:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。