Ansible-检查文件中是否存在字符串 [英] Ansible - Check if string exists in file

查看:1508
本文介绍了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 username

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆