On Mon, Feb 25, 2019, at 8:08 AM, Deepan M wrote:
>
>
>
>
> Hello,
>
> Can someone please help to solve my request?
>
> with blockinfile module, I'm looking for option to check the entries
> whether its present or not! if not then entries have to be added by play.
>
> As per below my playbook, its working partially, like its appending even if
> the entries are already present but actually i'm looking that play should
> ignore if already entries are exists and incase not present then it should
> added.
>
My guess is that it is finding the empty string marker at BOF and finding
nothing between the zero length marker and itself, this adding the block
unconditionally. You will have more luck with a non empty marker.
V/r,
James Cassell
>
>
> *Playbook:-*
> cat resolv_add.yml
> ---
> - hosts: resolv
> become: yes
> tasks:
> - name: check the presence of lines
> shell: cat /tmp/resolv.conf |egrep "Hello the world|i'm here|is it
> working"
> register: result
> ignore_errors: yes
> - debug: msg="{{result.stdout}}"
> - name: add lines below lines if not exists
> blockinfile:
> marker: ""
> block: |
> Hello the world
> i'm here
> is it working
> path: /tmp/resolv.conf
> state: present
> changed_when: result.stdout.find('exists') == -1
>
>
> *Execution Output:-*
>
> ansible-playbook resolv_add.yml
> SUDO password:
>
> PLAY [resolv]
> ************************************************************************************************************************
>
> TASK [Gathering Facts]
> ***************************************************************************************************************
> ok: [usa7061lv1771]
>
> TASK [check the presence of lines]
> ***************************************************************************************************
> changed: [usa7061lv1771]
>
> TASK [debug]
> *************************************************************************************************************************
> ok: [usa7061lv1771] => {
> "msg": "Hello the world\ni'm here\nis it working"
> }
>
> TASK [add lines below lines if not exists]
> *******************************************************************************************
> changed: [usa7061lv1771]
>
> PLAY RECAP
> ***************************************************************************************************************************
> usa7061lv1771 : ok=4 changed=2 unreachable=0
> failed=0
>
> *Remote server output:-*
> cat resolv.conf
>
> Hello the world
> i'm here
> is it working
>
> Hello the world
> i'm here
> is it working
>
> Thanks,
> Deepan M
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/1551101761.238055.1663380392.35A0365A%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.