Ansible - Check if string exists in file

前端 未结 4 1922
再見小時候
再見小時候 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:14

    I am using the following approach, using only a grep -q and a registered variable.

    Upside is that it's simple, downside is that you have a FAILED line in your output. YMMV.

    - name: Check whether foobar is defined in /bar/baz
      command:
        cmd: 'grep -q foobar /bar/baz'
      register: foobar_in_barbaz
      changed_when: false
      ignore_errors: true
    
    
    - when: not foobar_in_barbaz.failed
      name: Do something when foobar is in /bar/baz
        ....
    
    
    - when: foobar_in_barbaz.failed
      pause:
        seconds: 1
        content: |
          You do not seem to have a foobar line in /bar/baz
          If you add it, then magic stuff will happen!
    

提交回复
热议问题