【问题标题】:How to change "parse_cli_textfsm" to reference template folder in role folder in Ansible?如何将“parse_cli_textfsm”更改为引用 Ansible 角色文件夹中的模板文件夹?
【发布时间】: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 }

参考

【问题讨论】:

    标签: ansible ansible-template ansible-filter


    【解决方案1】:

    parse_cli_textfsm('{{ role_path +“/templates/textfsm”) }}

    为我工作...

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2020-09-12
      • 2022-01-06
      相关资源
      最近更新 更多