Skip to content

Instantly share code, notes, and snippets.

@muloka
Created November 28, 2013 04:26
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 33 You must be signed in to fork a gist
  • Save muloka/7687250 to your computer and use it in GitHub Desktop.
Save muloka/7687250 to your computer and use it in GitHub Desktop.
## credit: http://fabian-affolter.ch/blog/the-lineinfile-module-of-ansible/
---
- hosts: alpine_install
user: root
tasks:
# - name: create a complete empty file
# command: /usr/bin/touch /test/test.conf
- name: create a new file with lineinfile
lineinfile: dest=/test/test.conf
regexp='^' line=''
state=present
create=True
- name: add a string to the new file
lineinfile: dest=/test/test.conf
regexp='^'
line='Hello, World!'
state=present
- name: add a multiline string to the file and delete the string from before
# Be aware, with the given regex the string will be added everytime the playbook runs
lineinfile: dest=/test/test.conf
regexp='^'
line='#This is a comment\n#Another comment\n#Another comment, again\n#Again a comment\n#The last comment'
state=present
- name: add a single line, in this case the same as the comment but uncommented
lineinfile: dest=/test/test.conf
regexp='^Another'
insertafter='^#Another'
line='Another comment, no longer a comment'
state=present
- name: remove the line '#Again a comment'
lineinfile: dest=/test/test.conf
regexp='^#Again'
state=absent
- name: add a new string at the beginning of the file
lineinfile: dest=/test/test.conf
regexp='^This'
insertbefore=BOF
line='This is no longer a comment'
- name: add a new string before the match
lineinfile: dest=/test/test.conf
regexp='^Another'
insertbefore='^#Another'
line='Another comment, no longer'
- name: add a new string at the end of the file
lineinfile: dest=/test/test.conf
regexp=''
insertafter=EOF
line='The latest entry'
@gvenka008c
Copy link

I have the file with the below content.

"Bamboo": {
"Endpoint": "http://xx.xx.xxx.xxx:800",
"Zookeeper": {
"Host": "//xx.xxx.xxx.xx:2181,xx.xxx.xxx.xx:2181,xx.xxx.xxx.xx:2181",
"Path": "/marathon-haproxy/state",
"ReportingDelay": 5
}
},

I wanted to dynamically update the below line using lineinfile
"Endpoint": "http://xx.xx.xxx.xxx:800",

My Ansible has the below code
lineinfile: dest=/var/bamboo/production.json.bak state=present regexp="^ "Bamboo"":" {" insertafter=" "Bamboo"":"{" line=" "Endpoint"":" "http://{{ansible_default_ipv4.address}}:8080" "

For some reason, it is adding the line to the EOF.

can you please suggest on how can i fix it?

@daneshg
Copy link

daneshg commented Jun 14, 2016

  • name : check regex

    lineinfile : dest=file

               regexp='Endpoint'
    
               line=' "Endpoint":"http://xx.xx.xxx.xxx:800" '
    
               state=present
    

find Endponit with regexp and replace that by new line

@dtolj
Copy link

dtolj commented Oct 16, 2016

How to add a new string before the FIRST match?

@olegsidokhmetov
Copy link

olegsidokhmetov commented May 14, 2021

Hello!

I have a code


Component "{{ .Env.XMPP_INTERNAL_MUC_DOMAIN }}" "muc"
    storage = "memory"
    modules_enabled = {
        "ping";
        {{ if .Env.XMPP_INTERNAL_MUC_MODULES }}
        "{{ join "\";\n\"" (splitList "," .Env.XMPP_INTERNAL_MUC_MODULES) }}";
        {{ end }}
    }
    muc_room_locking = false
    muc_room_default_public_jids = true

Component "{{ .Env.XMPP_MUC_DOMAIN }}" "muc"
    storage = "memory"
    modules_enabled = {
        "muc_meeting_id";
        {{ if .Env.XMPP_MUC_MODULES }}
        "{{ join "\";\n\"" (splitList "," .Env.XMPP_MUC_MODULES) }}";
        {{ end }}
        {{ if and $ENABLE_AUTH (eq $AUTH_TYPE "jwt") }}
        "{{ $JWT_TOKEN_AUTH_MODULE }}";
        {{ end }}
    }
    muc_room_cache_size = 1000
    muc_room_locking = false
    muc_room_default_public_jids = true

Component "focus.{{ .Env.XMPP_DOMAIN }}" "client_proxy"
    target_address = "{{ .Env.JICOFO_AUTH_USER }}@{{ .Env.XMPP_AUTH_DOMAIN }}"

Component "speakerstats.{{ .Env.XMPP_DOMAIN }}" "speakerstats_component"
    muc_component = "{{ .Env.XMPP_MUC_DOMAIN }}"

Component "conferenceduration.{{ .Env.XMPP_DOMAIN }}" "conference_duration_component"
    muc_component = "{{ .Env.XMPP_MUC_DOMAIN }}"

{{ if $ENABLE_LOBBY }}
Component "lobby.{{ .Env.XMPP_DOMAIN }}" "muc"
    storage = "memory"
    restrict_room_creation = true
    muc_room_locking = false
    muc_room_default_public_jids = true
{{ end }}

In the file I have 3 of the same line with name **muc_room_default_public_jids = true** . How I can add new line "muc_restrict_executable = "/config/data/custom_prosody_plugins/mod_muc_restrict_rooms_exec.sh", in this block

    muc_room_cache_size = 1000
    muc_room_locking = false
    muc_room_default_public_jids = true

after "muc_room_default_public_jids = true"

I used this code:


    - name: Add new line after muc_room_default_public_jids = true' in file "jitsi-meet.cfg.lua"
      lineinfile:
        path: /home/docker-jitsi-meet/prosody/rootfs/defaults/conf.d/jitsi-meet.cfg.lua
        insertafter: "muc_room_default_public_jids ="
        line: "muc_restrict_executable = "/config/data/custom_prosody_plugins/mod_muc_restrict_rooms_exec.sh"
        state: present  
      register: lineinfileexample

But this code insert new line after 1 (first) match :(

Command firstmatch: yes does not work, because I need a second line with this name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment