本文介绍了Ansible - 检查文件中是否存在字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Ansible 很陌生

I'm very new to Ansible

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

Is it possible to check if a string exists in a file using Ansible.

我想检查用户是否有权访问服务器.这可以在服务器上使用 cat/etc/passwd | 来完成.grep 用户名

I want to check is a user has access to a server.this can be done on the server using cat /etc/passwd | grep username

但如果用户不在,我希望 Ansible 停止.

but I want Ansible to stop if the user is not there.

我尝试使用 lineinfile 但似乎无法让它返回.

I have tried to use the lineinfile but can't seem to get it to return.

代码

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

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

The code above adds user to the file if he is not there. All i want to do is check. I don't want to modify the file in any way, is this possible

谢谢.

推荐答案

我可能 注册并评估一个变量.

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

The following simple playbook works for me:

- 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 - 检查文件中是否存在字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:49