ansible - 将键名提取到列表中
问题描述
鉴于以下情况,我想最终得到一个与这些 UID 匹配的用户名列表,例如用户:['u1']
- name: getent
hosts: all
tasks:
- name: getent
getent:
database: passwd
key: "{{ item }}"
split: ':'
with_items:
- 20001
register: users
- name: debug
debug:
msg: "{{ users.results | map(attribute='ansible_facts.getent_passwd') | list }}"
TASK [debug] *******************************************************************
ok: [localhost] => {
"users": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"ansible_facts": {
"getent_passwd": {
"u1": [ <-- I do not care about anything contained within u1!
就以上而言,我最终得到
[{u'u1': [u'x', u'20001', u'20001', u'', u'/home/u1', u'/sbin/nologin']}]
我怎样才能得到
['u1']
解决方案
这是使用JMESPath查询的解决方案:
- debug:
msg: "{{ users.results | json_query('[].ansible_facts.getent_passwd.keys(@)[]') }}"
[].ansible_facts.getent_passwd
相当于你的过滤器keys(@)
函数返回上述列表的键名[]
展平结果列表
推荐阅读
- sql - 更新行,内部加入获取顶行
- c++ - 最大面积直方图 动态规划 C++
- javascript - 使用 JavaScript 查找给定值的完整对象路径
- javascript - Highcharts 导出菜单未完全显示
- angular - Ionic 4 硬件后退按钮路由
- sql-server - 返回 XML 的交叉应用函数需要列名
- php - 如何同步和删除数据库中的条目
- c# - 在没有键条件表达式的情况下在 DynamoDb 中查询
- ruby-on-rails - 如何在慢轨 3/ruby2 应用程序中找到瓶颈?
- wordpress - 链接共享显示带有我的 wordpress 网站名称的 javascript 代码