gpt4 book ai didi

ansible - Ansible-检查文件中是否存在字符串

转载 作者:行者123 更新时间:2023-12-04 02:32:35 25 4
gpt4 key购买 nike

我对Ansible很陌生

是否可以使用Ansible检查文件中是否存在字符串。

我要检查的是用户有权访问服务器。
这可以使用cat /etc/passwd | grep username在服务器上完成

但是我希望Ansible如果用户不在那儿就停止。

我尝试使用lineinfile,但似乎无法使其返回。



 - name: find
lineinfile: dest=/etc/passwd
regexp=[user]
state=present
line="user"


上面的代码将用户添加到文件中(如果他不在的话)。我要做的就是检查。我不想以任何方式修改文件,这可能吗

谢谢。

最佳答案

我可能会register and evaluate a variable

以下简单的剧本对我有用:

- hosts: localhost
tasks:

- name: read the passwd file
shell: cat /etc/passwd
register: user_accts

- name: a task that only happens if the user exists
when: user_accts.stdout.find('hillsy') != -1
debug: msg="user hillsy exists"

关于ansible - Ansible-检查文件中是否存在字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com