ansibleyaml

Ansible: append a string to an existing line in a file


I am using ansible module to edit the manifest file for kube-apiserver

    - --feature-gates=AdvancedAuditing=true

I want to append new feature-gate like

    - --feature-gates=AdvancedAuditing=true,TTLAfterFinished=true

I tried many thing, one of which -

- name: append TTLAfterFinished to existing list of feature-gates
  lineinfile:
    path: item.0.item.file_path
    backrefs: yes
    regexp: "^(.*feature-gates.*)$"
    line: '\1,TTLAfterFinished=true'

With no luck.. :( Any help ?


Solution

  • What you have worked fine for me, but I do not have an item varaible. So I have this:

    - name: append TTLAfterFinished to existing list of feature-gates
      lineinfile:
        path: "{{ role_path }}/files/file_path"
        backrefs: yes
        regexp: "^(.*feature-gates.*)$"
        line: '\1,TTLAfterFinished=true'
    

    Perhaps it is your item variable that is the problem.