Thanks @utoddl for your time and reply.

when:  item.line is not search(omit)

This solution actually worked for me.

Thanks & regards,

FARRUKH
On Friday, August 5, 2022 at 5:22:03 PM UTC+5 [email protected] wrote:

> That "when:" should be
>
>     when: item.line is not search(omit)
>
> On Friday, August 5, 2022 at 8:03:34 AM UTC-4 Todd Lewis wrote:
>
>>     - name: "Update Redis Parameters in {{ redis_config_path }}"
>>       lineinfile:
>>         path: "{{ redis_config_path }}"
>>         backrefs: yes
>>         regexp: "{{ item.regexp }}"
>>         line: "{{ item.line }}"
>>       when: item.line is match(omit)
>>       with_items:
>>         - { regexp: '^maxmemory',     line: "maxmemory {{ MAX_MEM | d(omit) 
>> }}mb" }
>>         - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
>> | d(omit) }}" }
>>         - { regexp: '^tcp-backlog',   line: "tcp-backlog {{ TCP_BACKLOG | 
>> d(omit) }}" }
>>         - { regexp: '^maxclients',    line: "maxclients {{ MAX_CLIENTS | 
>> d(omit) }}" }
>>         - { regexp: '^timeout',       line: "timeout {{ TIMEOUT }}" }
>>
>>
>>
>> On 8/5/22 7:31 AM, farrukh ahmed wrote:
>>
>> Thanks for your reply Vladimir Botka.
>>
>> This is my actual case: 
>>
>>  defaults > main.yml
>> ---
>> # defaults file for redis_configs
>> redis_config_path: /etc/redis/redis.conf
>>
>> # Default Params Values
>> MAX_MEM: 3000
>> TCP_KEEPALIVE: 0
>> TCP_BACKLOG: 511
>> MAX_CLIENTS: 15000
>> TIMEOUT: 1500
>>
>>
>> And this is: 
>>
>> Tasks > main.yml
>>
>> ---
>>
>> - name: "Update Redis Parameters in {{ redis_config_path }}"
>>   lineinfile:
>>     path: "{{ redis_config_path }}"
>>   backrefs: yes
>>   regexp: "{{ item.regexp }}"
>>   line: "{{ item.line }}"
>>   with_items:
>>     - { regexp: '^maxmemory', line: "maxmemory {{ MAX_MEM }}mb", when: {{ 
>> MAX_MEM }} is defined } 
>>     - { regexp: '^tcp-keepalive', line: "tcp-keepalive {{ TCP_KEEPALIVE 
>> }}", when: {{ TCP_KEEPALIVE }} is defined }
>>     - { regexp: '^tcp-backlog', line: "tcp-backlog {{ TCP_BACKLOG }}", 
>> when: {{ TCP_BACKLOG }} is defined}
>>     - { regexp: '^maxclients', line: "maxclients {{ MAX_CLIENTS }}", 
>> when: {{ MAX_CLIENTS }} is defined}
>>     - { regexp: '^timeout', line: "timeout {{ TIMEOUT }}" } 
>>
>>
>> I want this to check if each variable inside an item is defined then make 
>> the changes to the line in the file, otherwise skip only those items which 
>> variables is not defined.
>> For Instance, If I comment out the variable MAX_MEM from the defaults > 
>> main.yml. like below:
>>
>> # Default Params Values
>> ##MAX_MEM: 3000
>> TCP_KEEPALIVE: 0
>> TCP_BACKLOG: 511
>> MAX_CLIENTS: 15000
>> TIMEOUT: 1500  
>>
>> Then the execution should skip changes to the line where MAX_MEM is 
>> undefined, and the rest lines should be changed as they defined.
>>
>> Is it possible?
>>
>>
>> On Friday, August 5, 2022 at 2:18:05 PM UTC+5 [email protected] wrote:
>>
>>> On Thu, 4 Aug 2022 23:43:59 -0700 (PDT) 
>>> farrukh ahmed <[email protected]> wrote: 
>>>
>>> > with_items: 
>>> > - { regexp: '^text1', line: "text1 = {{ VAR1 }}", when: VAR1 is 
>>> defined } 
>>> > - { regexp: '^text2', line: "text2 = {{ VAR2 }}" } 
>>> > - { regexp: '^text3', line: "text3 = {{ VAR3 }}" } 
>>>
>>> Set *default* to avoid errors, add attribute *def*, and select lines. 
>>> For example, 
>>>
>>> shell> cat pb.yml 
>>> --- 
>>> - hosts: localhost 
>>> gather_facts: false 
>>> vars: 
>>> lines: 
>>> - regexp: '^text1' 
>>> line: 'text1 = {{ VAR1|d("undef") }}' 
>>> def: '{{ VAR1 is defined }}' 
>>> - regexp: '^text2' 
>>> line: 'text2 = {{ VAR2|d("undef") }}' 
>>> def: '{{ VAR2 is defined }}' 
>>> - regexp: '^text3' 
>>> line: 'text3 = {{ VAR3|d("undef") }}' 
>>> def: '{{ VAR3 is defined }}' 
>>> tasks: 
>>> - debug: 
>>> var: item 
>>> loop: "{{ lines|selectattr('def') }}" 
>>>
>>> gives 
>>>
>>> shell> ansible-playbook pb.yml -e VAR1=test 
>>>
>>> PLAY [localhost] 
>>> ********************************************************************* 
>>>
>>> TASK [debug] 
>>> ********************************************************************* 
>>> ok: [localhost] => (item={'regexp': '^text1', 'line': 'text1 = test', 
>>> 'def': True}) => ansible_loop_var: item item: 
>>> def: true 
>>> line: text1 = test 
>>> regexp: ^text1 
>>>
>>> -- 
>>> Vladimir Botka 
>>>
>>
>>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2cf96c01-f0fc-4ed5-afdb-4329530ae06en%40googlegroups.com.

Reply via email to