Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Software >> Automation >> Ansible >> Modules >> lineinfile >> Examples

 

lineinfile


## DOCUMENTATION

online help: 
offline help:


## EXAMPLE 1
## PLAYBOOK

---
- name: lineinfile module example 1
  hosts: all
  become: True
  gather_facts: no
  tasks:
  #
  - name: Debug (1), check file before add
    shell: cat /home/ansible/hello.txt
    register: results1_before
  - debug: var=results1_before.stdout_lines
  - name: (1) Add a line to a file if the file does not exist, without passing regexp
    lineinfile:
      path: /home/ansible/hello.txt
      line: "Welcome to our website!"
      create: yes
  - name: Debug (1), check file after add
    shell: cat /home/ansible/hello.txt
    register: results1_after
  - debug: var=results1_after.stdout_lines
  #
  - name: Debug (2), check file before update
    shell: cat /home/ansible/hello.txt
    register: results2_before
  - debug: var=results2_before.stdout_lines
  - name: (2) update a line with specific string pattern (using regexp)
    lineinfile:
      path: /home/ansible/hello.txt
      regexp: '^Hello$'
      line: Hello, world
  - name: Debug (2), check file after update
    shell: cat /home/ansible/hello.txt
    register: results2_after
  - debug: var=results2_after.stdout_lines
  #
  - name: Debug (3), check file before remove
    shell: cat /home/ansible/hello.txt
    register: results3_before
  - debug: var=results3_before.stdout_lines
  - name: (3) remove a line with specific string pattern (using regexp)
    lineinfile:
      path: /home/ansible/hello.txt
      regexp: '^Welcome to our.*$'
      state: absent
  - name: Debug (3), check file after remove
    shell: cat /home/ansible/hello.txt
    register: results3_after
  - debug: var=results3_after.stdout_lines
  #
  - name: Debug (4), check file before insert before & after
    shell: cat /home/ansible/hostnames.txt
    register: results4_before
  - debug: var=results4_before.stdout_lines
  - name: (4a) Insert line before
    lineinfile:
      path: /home/ansible/hostnames.txt
      insertbefore: '^server5'
      line: server4
  - name: (4b) Insert line after
    lineinfile:
      path: /home/ansible/hostnames.txt
      insertafter: '^server1'
      line: server2
  - name: Debug (4), check file after remove
    shell: cat /home/ansible/hostnames.txt
    register: results4_after
  - debug: var=results4_after.stdout_lines



## OUTPUT

PLAY [lineinfile module example 1] *****************************************************************

TASK [Debug (1), check file before add] ************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results1_before.stdout_lines": [
        "Hello"
    ]
}
ok: [ubuntu20_int] => {
    "results1_before.stdout_lines": [
        "Hello"
    ]
}

TASK [(1) Add a line to a file if the file does not exist, without passing regexp] *****************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (1), check file after add] *************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results1_after.stdout_lines": [
        "Hello",
        "Welcome to our website!"
    ]
}
ok: [ubuntu20_int] => {
    "results1_after.stdout_lines": [
        "Hello",
        "Welcome to our website!"
    ]
}

TASK [Debug (2), check file before update] *********************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results2_before.stdout_lines": [
        "Hello",
        "Welcome to our website!"
    ]
}
ok: [ubuntu20_int] => {
    "results2_before.stdout_lines": [
        "Hello",
        "Welcome to our website!"
    ]
}

TASK [(2) update a line with specific string pattern (using regexp)] *******************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (2), check file after update] **********************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results2_after.stdout_lines": [
        "Hello, world",
        "Welcome to our website!"
    ]
}
ok: [ubuntu20_int] => {
    "results2_after.stdout_lines": [
        "Hello, world",
        "Welcome to our website!"
    ]
}

TASK [Debug (3), check file before remove] *********************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results3_before.stdout_lines": [
        "Hello, world",
        "Welcome to our website!"
    ]
}
ok: [ubuntu20_int] => {
    "results3_before.stdout_lines": [
        "Hello, world",
        "Welcome to our website!"
    ]
}

TASK [(3) remove a line with specific string pattern (using regexp)] *******************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (3), check file after remove] **********************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results3_after.stdout_lines": [
        "Hello, world"
    ]
}
ok: [ubuntu20_int] => {
    "results3_after.stdout_lines": [
        "Hello, world"
    ]
}

TASK [Debug (4), check file before insert before & after] ******************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results4_before.stdout_lines": [
        "server1",
        "server3",
        "server5"
    ]
}
ok: [ubuntu20_int] => {
    "results4_before.stdout_lines": [
        "server1",
        "server3",
        "server5"
    ]
}

TASK [(4a) Insert line before] *********************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [(4b) Insert line after] **********************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (4), check file after remove] **********************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "results4_after.stdout_lines": [
        "server1",
        "server2",
        "server3",
        "server4",
        "server5"
    ]
}
ok: [ubuntu20_int] => {
    "results4_after.stdout_lines": [
        "server1",
        "server2",
        "server3",
        "server4",
        "server5"
    ]
}

PLAY RECAP *****************************************************************************************
debian11_int               : ok=21   changed=13   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=21   changed=13   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]