Ansible Lineinfile Basics
Understanding Lineinfile Module
The Ansible lineinfile module is a powerful tool for configuration management and text file editing in infrastructure automation. It allows administrators to modify, insert, or remove specific lines in configuration files with precision and efficiency.
Key Concepts of Lineinfile
Lineinfile provides several critical capabilities for managing text files:
Feature |
Description |
Line Modification |
Directly edit existing lines in files |
Line Insertion |
Add new lines at specific locations |
Line Removal |
Delete specific lines matching patterns |
Backup Creation |
Automatically create file backups before modifications |
Workflow of Lineinfile Module
graph TD
A[Start] --> B{File Exists?}
B -->|Yes| C[Analyze Target Line]
B -->|No| D[Create File]
C --> E[Modify/Insert/Remove Line]
E --> F[Validate Changes]
F --> G[End]
Practical Code Example
- name: Configure SSH Configuration
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
state: present
backup: yes
This example demonstrates how lineinfile can modify the SSH configuration to disable root login, showcasing its configuration management capabilities in infrastructure automation.