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/20220805111742.55c17fbf%40gmail.com.

Attachment: pgpZj2RZv0VfV.pgp
Description: OpenPGP digital signature

Reply via email to