Difference between revisions of "Blockinfile:"
Jump to navigation
Jump to search
Line 19: | Line 19: | ||
address 192.168.0.1 | address 192.168.0.1 | ||
netmask 255.255.255.0" | netmask 255.255.255.0" | ||
+ | |||
+ | == Official examples == | ||
+ | <pre> | ||
+ | # 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 | ||
+ | |||
+ | - 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 -->" | ||
+ | Authors | ||
+ | </pre> | ||
Revision as of 18:32, 7 September 2023
- 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"
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 - 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 -->" Authors
Related
See also
- Ansible:
lineinfile, blockinfile
- Ansible modules:
gather_facts, set_fact
,ansible.builtin.file
,lineinfile
,stat
,group
,mount
,git:
,package:
,command:
,shell:
,raw:
,template:
,register:
,user:
, Ansible playbooks examples,tasks:
,lineinfile, blockinfile:
,shell:
,command:
,raw:
,ansible.builtin.file, service:, aws.ecs_taskdefinition, aws.asg, docker_container, kubernetes.core.k8s
,community.
,include_tasks
Advertising: