Ah Ok, i see.
Using a string manipulating module like replace is inherently fragile,
so whatever works now may break tomorrow (as you pointed out).
Since your input is yaml, I would look into another approach, like
lookup the file content, parse it as yaml, change the required
keys/values, and then write the file back.
This way is more robust in terms of input (the ordering/comments/etc
won't matter), and also output, as that will be valid yaml.

On Wed, 19 May 2021 at 09:25, Manisha <singhmanisha012...@gmail.com> wrote:
>
> Hi Dick,
>
> Thank you for the response.
>
> as you see the commented code where i was trying use the after: 
> 'payroll_emp.*' it does not do anything and i use the replace without any 
> guard then all the id: xxxxx gets change with id: 2000
> *****************************
> ---
> - hosts: localhost
>   connection: local
>   gather_facts: no
>
>   tasks:
>     - name: Read in the animals.yml vars file.
>       include_vars: sample_file.yml
>
>     - name: debug the output for tiers.
>       debug:
>         msg: "{{ Company }}"
>
>     # - name: replace id for payroll_emp
>     #   replace:
>     #     path: sample_file.yml
>     #     regexp: 'id:.*'
>     #     replace: 'id: 2000'
>     #     after: 'payroll_emp:.*'
>
>     - name: replace id for payroll_emp
>       replace:
>         path: sample_file.yml
>         regexp: 'id:.*'
>         replace: 'id: 2000'
> ****************************************
>
>
> On Wednesday, May 19, 2021 at 12:41:14 PM UTC+5:30 dick....@geant.org wrote:
>>
>> What does your ansible task look like?
>>
>> On Wed, 19 May 2021 at 08:41, Manisha <singhmani...@gmail.com> wrote:
>> >
>> > Hi All,
>> >
>> > I am try to figure out the regex exp that will catch a key in a yaml file 
>> > but i am not getting the way to pass through. I hope that you expertise on 
>> > this can help me.
>> >
>> > Problem Statement: I have a yaml file (which will be dynamic), where i 
>> > need to replace a specific key value.
>> >
>> > Example: in the below yaml file i only need to update "id" value which is 
>> > under payroll_emp in the all the file.
>> >
>> > If i am using regex: id:.* this will catch all the id in the file.
>> >
>> > Help Needed: if there a way that i can have a regex to match the id under 
>> > payroll_emp and change the value id: 1000 to id: 2000 .
>> >
>> > NOTE: we can't use the after and before parameter as this file will be 
>> > dynamic its not sure what will be before and after id
>> >
>> > ---
>> > Company:
>> > Country:
>> > states:
>> > - employee:
>> > Internal_1:
>> > payroll_emp:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1000
>> > CWF:
>> > on-shore:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1001
>> > Internal_2:
>> > payroll_emp:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1000
>> > CWF:
>> > on-shore:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1001
>> > Internal_3:
>> > payroll_emp:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1000
>> > CWF:
>> > on-shore:
>> > key_1: val_1
>> > key_2: val_2
>> > id: 1001
>> >
>> > --
>> > 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 ansible-proje...@googlegroups.com.
>> > To view this discussion on the web visit 
>> > https://groups.google.com/d/msgid/ansible-project/fb7f5ff5-f12d-4ea9-a4f2-ad591d2d5aa1n%40googlegroups.com.
>>
>>
>>
>> --
>> Dick Visser
>> Trust & Identity Service Operations Manager
>> GÉANT
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/7ba33283-cdd6-4b37-852c-42c5b5c90b79n%40googlegroups.com.



-- 
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL8fbwMfN52PJdfzOGc1R9qGrosTrrm4e9A6cg_ymeJxOP8zhg%40mail.gmail.com.

Reply via email to