Ansible - Check if string exists in file

前端 未结 4 1903
再見小時候
再見小時候 2021-01-11 09:48

I\'m very new to Ansible

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

I want to check is a user has access to a server. this can be

4条回答
  •  無奈伤痛
    2021-01-11 10:21

    It's a tricky one. the lineinfile module is specifically intended for modifying the content of a file, but you can use it for a validation check as well.

    - name: find
      lineinfile: 
        dest: /etc/passwd
        line: "user"
      check_mode: yes
      register: presence
      failed_when: presence.changed
    

    check_mode ensures it never updates the file. register saves the variable as noted. failed_when allows you to set the failure condition i.e. by adding the user because it was not found in the file.

    There are multiple iterations of this that you can use based on what you want the behavior to be. lineinfile docs particular related to state and regexp should allow you to determine whether or not presence or absence is failure etc, or you can do the not presence.changed etc.


提交回复
热议问题