【发布时间】:2020-04-03 06:27:47
【问题描述】:
目标
- 我想创建一个可移植的角色文件夹,该文件夹利用角色目录中的 textfsm 模板。
- 目录结构
$ tree
.
├── ansible.cfg
├── hosts.ini
├── out_pb1
├── pb1.yml
├── roles
│ ├── command_facts
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ └── textfsm // I want this
│ │ └── vars
└── templates
└── textfsm // created for testing
问题
-
parse_cli_textfsm引用templates/textfsm而不是roles/command_facts/templates/textfsm - 目标是可移植性,因此不考虑指定完整路径。
尝试修复(失败)
- 执行
pb1.yml - 如果我删除了
pb1.yml目录下的templates/textfsm,就会出现如下错误。
$ ansible-playbook pb1.yml -u username -k
(omitted)
TASK [command_facts : set_fact]
fatal: [target]: FAILED! => {"msg": "unable to locate parse_cli_textfsm template: /templates/textfsm"}
- 我尝试使用系统变量
role_path,即{{ raw_output.stdout | parse_cli_textfsm('{{ role_path }}/templates/textfsm') }},但它不起作用。{{ role_path }}变成普通字符串。
$ ansible-playbook pb1.yml -u username -k
(omitted)
TASK [ixrs_facts : set_fact]
fatal: [target]: FAILED! => {"msg": "unable to locate parse_cli_textfsm template: {{ role_path }}/templates/textfsm"}
文件
roles/command_facts/task/main.yml
---
- name: show protocols all
raw: "show protocols all"
register: raw_output
- set_fact:
output: {{ raw_output.stdout | parse_cli_textfsm("/templates/textfsm") }}
- debug: var=output[::]
pb1.yml
---
- name: testing
hosts: target
gather_facts: false
roles:
- { role: command_facts }
参考
- https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#network-cli-filters
{{ output.stdout[0] | parse_cli_textfsm('path/to/fsm') }}
【问题讨论】:
标签: ansible ansible-template ansible-filter