bash - 尝试列出主机上的所有用户时出错
问题描述
我正在尝试获取我在主机上创建的所有用户。当我在终端上运行以下命令时,我得到了机器上的所有用户。
sudo getent passwd {1000..6000} | cut -d":" -f1
但是,当我尝试使用 ansible 运行它时,出现错误。我试过用双引号括起来,转义括号,将输出管道传输到 cat 等,但没有任何效果。
---
- name: "run commands"
become: true
gather_facts: no
hosts: all
tasks:
- name: list all users
shell: getent passwd {1000..6000} | cut -d":" -f1
register: getent
- debug: var=getent.stdout_lines
解决方案
请注意,默认情况下,Ansible 使用,正如命令概要/bin/sh
中所指出的那样。
它几乎与ansible.builtin.command
/bin/sh
模块完全相同,但通过远程节点上的shell ( ) 运行命令。
来源:https ://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#synopsis
但sh
不会解释像{0..10}
.
有两种方法可以克服这个问题:
seq
而是 使用:- shell: getent passwd $(seq 1000 6000) | cut -d":" -f1 register: getent
- 通过以下方式指定
shell
您希望它执行的任务bash
:- shell: getent passwd {1000..6000} | cut -d":" -f1 register: getent args: executable: /bin/bash
推荐阅读
- amazon-web-services - 在我的免费等级账户中为未使用的弹性 IP 计费,即使我没有在我的账户中看到任何分配的弹性 IP
- android - Android CarrierMessagingService 收不到短信事件
- javascript - 展开/折叠引导 Vue.js 表中的所有操作
- css - 媒体最小宽度不能按指定的顺序正常工作
- javascript - 使用返回字符串的函数定义 Kendo UI 网格列模板
- elasticsearch - 如何根据过滤数据对弹性搜索中的数据进行排序
- python - 从鱼眼相机到真实平面图像的目标转换,无需校准
- python - Python 线程 - 多线程崩溃
- python - 冻结 keras 层不会改变 sumarry 可训练参数
- jquery - 在拖放功能中设置容器,以便将文件拖到桌面(谷歌浏览器)