On Thu, 5 May 2022 19:21:47 -0700
Tony Wong <[email protected]> wrote:

> it only works for first node. When i run the playbook against 3 nodes the
> 2nd and 3rd node have this
> 
> image: graylog/graylog:4.2.5
> #image: graylog/graylog4.2.5
> image: graylog/graylog4.2.8


Quoting from *lineinfile* parameter *regexp*
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp

  For state=present, the pattern to replace if found. Only the last
  line found will be replaced.
  For state=absent, the pattern of the line(s) to remove.

Note the difference between the states *present/absent*. If *absent*
all matches are removed. If *present* only the last one is replaced.
The default state is *present*.

One of the scenarios on how to proceed might be removing all redundant
lines first. For example, given the file

shell> cat docker-compose
image: graylog/graylog4.2.5
#image: graylog/graylog4.2.5
image: graylog/graylog4.2.8


The playbook

shell> cat pb.yml
- hosts: localhost
  vars:
    versions:
      4.2.5: false
      4.2.8: true
  tasks:
    - name: Remove versions
      lineinfile:
        state: absent
        path: docker-compose
        regexp: '^\s*#*\s*image: graylog/graylog{{ item.key }}\s*$'
      loop: "{{ versions|dict2items }}"
      when: force_remove_versions|d(false)|bool
    - name: Add versions
      lineinfile:
        path: docker-compose
        regexp: '^\s*#*\s*image: graylog/graylog{{ item.key }}\s*$'
        line: '{{ hash }}image: graylog/graylog{{ item.key }}'
      loop: "{{ versions|dict2items }}"
      vars:
        hash: "{{ item.value|ternary('', '#') }}"


removes all matching lines and creates the specified ones

shell> ansible-playbook pb.yml -e force_remove_versions=true

PLAY [localhost]
***************************************************************

TASK [Remove versions]
***************************************************************
changed: [localhost] => (item={'key': '4.2.5', 'value': False})
changed: [localhost] => (item={'key': '4.2.8', 'value': True})

TASK [Add versions]
***************************************************************
changed: [localhost] => (item={'key': '4.2.5', 'value': False})
changed: [localhost] => (item={'key': '4.2.8', 'value': True})

PLAY RECAP
***************************************************************
localhost                  : ok=2    changed=2    unreachable=0
failed=0    skipped=0    rescued=0    ignored=0   


shell> cat docker-compose
#image: graylog/graylog4.2.5
image: graylog/graylog4.2.8


Set the variable *force_remove_versions* as appropriate (for the
scenario that will serve the purpose best).


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

Attachment: pgp2bEQGoB3Vu.pgp
Description: OpenPGP digital signature

Reply via email to