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.

11 views
Skip to first unread message

Deepan M

unread,
Feb 25, 2019, 8:08:36 AM2/25/19
to Ansible Project

 

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.



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

James Cassell

unread,
Feb 25, 2019, 8:36:10 AM2/25/19
to Ansible List


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:-*

Deepan M

unread,
Feb 25, 2019, 10:19:02 AM2/25/19
to Ansible Project
Thank you James, its working fine.

Thanks,
Deepan M
Reply all
Reply to author
Forward
0 new messages