On Wed, 21 Dec 2022 16:30:03 +0500
farrukh ahmed <[email protected]> wrote:

> blockinfile:
> path: "{{ini_file_path}}" # set in defaults
> marker: ";# {mark} ANSIBLE MANAGED BLOCK ###"
> block: "{{ item.param }}"
> loop:
> - { param: "display_errors = {{service_configurations.display_errors |
> d(omit) }}"}
> - { param: "memory_limit = {{service_configurations.memory_limit | d(omit)
> }}M"}
> when: item.param is not search(omit)

The *marker* must be unique for each block. Otherwise the the blocks
will overwrite each other. For this purpose it would be practical to
change the structure of the iterated data. For example, the
simplified playbook below

  shell> cat pb.yml
  - hosts: localhost
    tasks:
      - blockinfile:
          path: /tmp/test.ini
          marker: "# {mark} {{ item.param }}"
          block: |
            {{ item.param }} = {{ item.value }}
        loop:
          - param: 'display_errors'
            value: "{{ conf.errors }}"
          - param: 'memory_limit'
            value: "{{ conf.limit }}"

shell> ansible-playbook pb.yml -e '{"conf":{"errors":"On","limit":"128"}}'

works as expected and creates the file

  shell> cat /tmp/test.ini 
  # BEGIN display_errors
  display_errors = On
  # END display_errors
  # BEGIN memory_limit
  memory_limit = 128
  # END memory_limit

Fit the data and *marker* to your needs.

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

Attachment: pgpcmZo_YKoO_.pgp
Description: OpenPGP digital signature

Reply via email to