简体   繁体   English

Ansible:使用 CLI 列出 Jinja2 过滤器

[英]Ansible: list Jinja2 filters using the CLI

While developing with Ansible, I would like to be able to use the CLI entirely to lookup information without having to Google something every 5 minutes.在使用 Ansible 进行开发时,我希望能够完全使用 CLI 来查找信息,而不必每 5 分钟谷歌一次。

I often use the ansible-doc command to get information about built-in modules, Ansible keywords, etc. and ansible-config when I have to write an ansible.cfg file.当我必须编写ansible.cfg文件时,我经常使用ansible-doc命令来获取有关内置模块、Ansible 关键字等的信息和ansible-config Unfortunately, I cannot find anything related to Jinja2 filters.不幸的是,我找不到与 Jinja2 过滤器相关的任何内容。

Question问题

How can I get a list of all Jinja2 filters without relying solely on my memory in case I do not have access to a browser or I'm sitting a Red Hat exam?如果我无法访问浏览器或者我正在参加 Red Hat 考试,如何在不完全依赖我的记忆的情况下获得所有 Jinja2 过滤器的列表?

I've long wished that ansible-doc had a -t filters option, too, but I think that's unlikely to happen since the filters are in a unique situation given that jinja2 itself has filters then ansible layers extra filters on top of the built-in jinja2 ones using its plugins mechanism.我一直希望ansible-doc也有一个-t filters选项,但我认为这不太可能发生,因为过滤器处于一种独特的情况,因为 jinja2 本身有过滤器,然后 ansible 在内置层之上添加了额外的过滤器 -在 jinja2 中使用其插件机制。 That story only gets worse with the new ansible-collections split随着新的 ansible-collections 拆分,这个故事只会变得更糟

There are (at least) two work-arounds I can offer for the offline case:我可以为离线案例提供(至少)两种解决方法:

  1. the pydoc command pydoc 命令
  2. brute force find蛮力find

using pydoc使用 pydoc

You can identify the python interpreter that ansible, itself, is using via ansible --version and look for ansible python module location .您可以通过ansible --version识别 ansible 本身正在使用的 python 解释器,并查找ansible python module location While that path will point to the .../site-packages/ansible directory, it will still help know if ansible is in a virtualenv (as Brew does), in $HOME/.local , or against which system python version one should look虽然该路径将指向.../site-packages/ansible目录,但它仍然有助于了解 ansible 是否在 virtualenv(如 Brew 那样)、在$HOME/.local ,或者应该针对哪个系统 python 版本看

In these snippets, I'm going to be using just bare python , but in the actual case it would be the fully-qualified path to the python binary ansible uses在这些片段中,我将只使用裸python ,但在实际情况下,它将是 python 二进制 ansible 使用的完全限定路径

Then, enumerate the jinja2 filters via然后,通过枚举 jinja2 过滤器

python -m pydoc jinja2.filters

and the non-collection ansible filters by first getting the top-level package:和非集合 ansible 过滤器首先获取顶级包:

$ PAGER=cat python -m pydoc ansible.plugins.filter
Help on package ansible.plugins.filter in ansible.plugins:

NAME
    ansible.plugins.filter - # Make coding more python3-ish

PACKAGE CONTENTS
    core
    mathstuff
    urls
    urlsplit

and then iterate on the filter packages然后迭代过滤器包

for i in core mathstuff urls urlsplit; do
  python -m pydoc ansible.plugins.filter.$i
done

That technique will also print the actual python source code path at the bottom of the output, if you need to dig into the actual implementation detail for any nuance or memory-jog如果您需要深入了解任何细微差别或 memory-jog 的实际实现细节,该技术还将在输出底部打印实际的 python 源代码路径

I had some limited success using this trickery to find potential filter plugins out of the installed collections我使用这个技巧从已安装的集合中找到了潜在的过滤器插件,但取得了一些有限的成功

ansible-galaxy collection list | \
    sed 's/^/ansible_collections./; s/  *[0-9].*/.plugins.filter/'

but obviously not all of those packages are correct or even exist但显然并非所有这些包都是正确的,甚至不存在

brute force using find使用find蛮力

The newfound split into ansible-collections makes getting the whole list harder using that pydoc method, but they are still on-disk python files so you can use the path predicate to locate the ones which provide filter plugins:新发现的拆分为 ansible-collections 使得使用该 pydoc 方法更难获取整个列表,但它们仍然是磁盘上的 python 文件,因此您可以使用path谓词来定位提供过滤器插件的文件:

for p in $(ansible --version | awk -F= '/ansible .* location/{gsub(":", "\n", $2); print $2}'); do
  find ${p}* -path '*/plugins/filter/*' -name '*.py' 2>/dev/null
done

which will serve as a memory jog, a place to read the source, and targets for using the aforementioned pydoc technique, if you prefer如果您愿意,这将用作内存慢跑、读取源代码的地方以及使用上述pydoc技术的目标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM