# Ansible Role: blockinfile This role contains no tasks, but provides blockinfile module which might be useful when you want to maintain multi-line snippets in config files in /etc. Ansible Galaxy Page: [https://galaxy.ansible.com/list#/roles/1475](https://galaxy.ansible.com/list#/roles/1475) **Request for review:** [The pull request to ansible-modules-extras](https://github.com/ansible/ansible-modules-extras/pull/832) has been made to include blockinfile module in the official distribution of Ansible, which enables you to use blockinfile as a standard module without this role! If you use this module and feel it's useful, please leave some endorsement comments on the PR. I greatly appreciate if you're [an eligible reviewer (existing module author)](https://github.com/ansible/ansible-modules-extras/blob/devel/REVIEWERS.md) and could take some time to review the PR, otherwise if you could ask reviewers of your acquiaintance for the review. It needs two +1 votes from reviewers in order to be nominated for inclusion. ## blockinfile Module This module will insert/update/remove a block of multi-line text surrounded by the marker lines. Example task: ```yaml - blockinfile: dest: /etc/network/interfaces block: | iface eth0 inet static address 192.168.0.1 netmask 255.255.255.0 ``` Text inserted/updated by the task in /etc/network/interfaces: ``` # BEGIN ANSIBLE MANAGED BLOCK iface eth0 inet static address 192.168.0.1 netmask 255.255.255.0 # END ANSIBLE MANAGED BLOCK ``` It uses marker lines `# {BEGIN/END} ANSIBLE MANAGED BLOCK` as default. You can specify alternative marker lines by `marker` option when you need to update files in other formats like HTML, or run multiple blockinfile tasks on the same file. ### Options If this section doesn't show nicely in Ansible Galaxy Page, please refer to equivalent in [GitHub Page](https://github.com/yaegashi/ansible-role-blockinfile#options). >
parameter | required | default | choices | comments |
---|---|---|---|---|
backup |
no | no |
|
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. |
block |
no | The text to insert inside the marker lines. If it's missing or an empty string, the block will be removed as if
state were specified to absent .aliases: content | ||
create |
no | no |
|
Create a new file if it doesn't exist. |
dest |
yes | The file to modify.
aliases: name, destfile | ||
follow (added in 1.8) |
no | no |
|
This flag indicates that filesystem links, if they exist, should be followed. |
group |
no | name of the group that should own the file/directory, as would be fed to chown | ||
insertafter |
no | EOF |
|
If specified, the block will be inserted after the last match of specified regular expression. A special value is available; EOF for inserting the block at the end of the file. If specified regular expresion has no matches, EOF will be used instead. |
insertbefore |
no |
|
If specified, the block will be inserted before the last match of specified regular expression. A special value is available; BOF for inserting the block at the beginning of the file. If specified regular expresion has no matches, the block will be inserted at the end of the file. | |
marker |
no | # {mark} ANSIBLE MANAGED BLOCK | The marker line template. "{mark}" will be replaced with "BEGIN" or "END". | |
mode |
no | mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers (like 0644). Leaving off the leading zero will likely have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r ). | ||
owner |
no | name of the user that should own the file/directory, as would be fed to chown | ||
selevel |
no | s0 | level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the range . _default feature works as for seuser. | |
serole |
no | role part of SELinux file context, _default feature works as for seuser. | ||
setype |
no | type part of SELinux file context, _default feature works as for seuser. | ||
seuser |
no | user part of SELinux file context. Will default to system policy, if applicable. If set to _default , it will use the user portion of the policy if available | ||
state |
no | present |
|
Whether the block should be there or not. |
validate |
no | None | The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the example below. The command is passed securely so shell features like expansion and pipes won't work. |
Last updated on {{ansible_date_time.iso8601}}
``` ```yaml - name: remove HTML as well as surrounding markers blockinfile: dest: /var/www/html/index.html marker: "" content: "" ``` ## Requirements None. ## Role Variables None. ## Dependencies None. ## Example Playbook Complete playbook that makes SSH password authentication for specific user prohibited, then restarts sshd if needed. ```yaml --- - hosts: all remote_user: ansible-agent sudo: yes roles: - yaegashi.blockinfile tasks: - name: Prohibit SSH password authentication for $SUDO_USER blockinfile: dest: /etc/ssh/sshd_config backup: yes content: | Match User {{ansible_env.SUDO_USER}} PasswordAuthentication no notify: Restart sshd handlers: - name: Restart sshd service name: ssh state: restarted ``` ## License GPLv3+ ## Author Information [YAEGASHI Takeshi](https://github.com/yaegashi)