blockinfile:

From wikieduonline
Revision as of 11:36, 9 September 2023 by Welcome (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

path:
backup:
insertafter:
marker: [1]


[[marker:]] "{{mark}} TEST"


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


- blockinfile: |
    dest=/etc/network/interfaces backup=yes
    content="iface eth0 inet static
        address 192.168.0.1
        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[edit]

  1. !/usr/bin/env ansible-playbook
- 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

Official examples[edit]

# 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: "Template:Lookup('ansible.builtin.file', './local/sshd config')"
    path: /etc/ssh/sshd_config
    backup: yes
    validate: /usr/sbin/sshd -T -f %s 
- 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 -->"

Errors[edit]

  • ERROR! 'blockinfile' is not a valid attribute for a Play

Related[edit]

See also[edit]

  • https://stackoverflow.com/a/41284686
  • Advertising: