Editing Blockinfile:

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
{{lowercase}}
 
 
* https://docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html
 
* https://docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html
[[path:]]
 
[[backup:]]
 
[[insertafter:]]
 
[[marker:]] <ref>https://stackoverflow.com/a/41284686</ref>
 
 
 
<nowiki>[[marker:]] "{{mark}} TEST"</nowiki>
 
 
 
marker: "# Request {mark}"
 
marker_begin: "Ticketnumer"
 
marker_end: "end"
 
 
- blockinfile:
 
      path: /path/to/your_file
 
      [[state:]] present
 
      [[insertafter:]] [[EOF]]
 
      [[content:]] |
 
          your first line
 
          your second line
 
          your third line
 
  
  
Line 31: Line 9:
 
         netmask 255.255.255.0"
 
         netmask 255.255.255.0"
  
 
 
# BEGIN ANSIBLE MANAGED BLOCK
 
your first line
 
your second line
 
your third line
 
# END ANSIBLE MANAGED BLOCK
 
 
== Blockinfile and conditional ==
 
#!/usr/bin/env ansible-playbook
 
<pre>
 
- hosts: localhost
 
  become: no
 
  gather_facts: no
 
  tasks:
 
    - name: "Searching for a String"
 
      register: ispresentvar
 
      shell: "grep -i 'your' /tmp/your_file.txt"
 
 
    - name: "sample task in case the String present in the file"
 
      debug: msg="INFO your is present in /tmp/your_file.txt"
 
      when: ispresentvar is changed
 
 
    - blockinfile:
 
        path: /tmp/kk.txt
 
        state: present
 
        #backup: yes
 
        marker: "# Request {mark}"
 
        marker_begin: "Ticketnumer"
 
        marker_end: "end"
 
        content: |
 
            your first line
 
            your second line
 
            your third line
 
      when: ispresentvar is not changed
 
</pre>
 
 
== Official examples ==
 
# Before [[Ansible 2.3]], option 'dest' or 'name' was used instead of 'path'
 
- name: Insert/Update "Match User" configuration block in /etc/ssh/sshd_config
 
  ansible.builtin.blockinfile:
 
    path: /etc/ssh/sshd_config
 
    block: |
 
      [[Match]] User ansible-agent
 
      [[PasswordAuthentication]] no
 
 
- name: Insert/Update eth0 configuration stanza in /etc/network/interfaces
 
        (it might be better to copy files into /etc/network/interfaces.d/)
 
  ansible.builtin.blockinfile:
 
    path: /etc/network/interfaces
 
    block: |
 
      iface eth0 inet static
 
          address 192.0.2.23
 
          netmask 255.255.255.0
 
 
- name: Insert/Update configuration using a local file and validate it
 
  ansible.builtin.blockinfile:
 
    block: "{{ lookup('ansible.builtin.file', './local/sshd_config') }}"
 
    path: /etc/ssh/sshd_config
 
    backup: yes
 
    [[validate:]] /usr/sbin/[[sshd -T]] -f %s
 
<pre>
 
- name: Insert/Update HTML surrounded by custom markers after <body> line
 
  ansible.builtin.blockinfile:
 
    path: /var/www/html/index.html
 
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
 
    insertafter: "<body>"
 
    block: |
 
      <h1>Welcome to {{ ansible_hostname }}</h1>
 
      <p>Last updated on {{ ansible_date_time.iso8601 }}</p>
 
 
- name: Remove HTML as well as surrounding markers
 
  ansible.builtin.blockinfile:
 
    path: /var/www/html/index.html
 
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
 
    block: ""
 
 
- name: Add mappings to /etc/hosts
 
  ansible.builtin.blockinfile:
 
    path: /etc/hosts
 
    block: |
 
      {{ item.ip }} {{ item.name }}
 
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
 
  loop:
 
    - { name: host1, ip: 10.10.1.10 }
 
    - { name: host2, ip: 10.10.1.11 }
 
    - { name: host3, ip: 10.10.1.12 }
 
 
- name: Search with a multiline search flags regex and if found insert after
 
  blockinfile:
 
    path: listener.ora
 
    block: "{{ listener_line | indent(width=8, first=True) }}"
 
    insertafter: '(?m)SID_LIST_LISTENER_DG =\n.*\(SID_LIST ='
 
    marker: "    <!-- {mark} ANSIBLE MANAGED BLOCK -->"
 
</pre>
 
 
== Errors ==
 
* <code>ERROR! 'blockinfile' is not a valid attribute for a Play</code>
 
  
 
== Related ==
 
== Related ==
 
* <code>[[lineinfile:]]</code>
 
* <code>[[lineinfile:]]</code>
* [[regex]]
 
* <code>[[when:]]</code>
 
  
 
== See also ==
 
== See also ==
* {{lineinfile}}
+
* {{Ansible}}
* {{Ansible modules}}
 
  
 
[[Category:Ansible]]
 
[[Category:Ansible]]

Please note that all contributions to wikieduonline may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Wikieduonline:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Advertising: