This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
ansible:ansible_tips [2025/02/24 22:07] manu |
ansible:ansible_tips [2025/02/25 20:18] (current) manu |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Ansible tips and tricks ====== | ====== Ansible tips and tricks ====== | ||
| - | ===== Replace inplace ===== | + | ===== Replacement in a file ===== |
| + | |||
| + | ==== Replace inplace ==== | ||
| Replace inplace sshd_config param (at same position) | Replace inplace sshd_config param (at same position) | ||
| Line 30: | Line 32: | ||
| line: "Port 2849" | line: "Port 2849" | ||
| </code> | </code> | ||
| + | |||
| + | ==== Insert after / before ==== | ||
| + | |||
| + | Use **insertafter** or **insertbefore** | ||
| + | <code> | ||
| + | tasks: | ||
| + | - name: add to ansible hosts file | ||
| + | lineinfile: | ||
| + | dest: /ansible/sshd_config | ||
| + | insertafter: '^\[ansible_ssh_host\]' | ||
| + | line: " test ansible_ssh_host=172.0.0.3" | ||
| + | </code> | ||
| + | |||
| + | ==== Comment the all lines of a file ==== | ||
| + | |||
| + | <code> | ||
| + | tasks: | ||
| + | - replace: | ||
| + | path: /ansible/sshd_config | ||
| + | regexp: '^(?!#)' | ||
| + | replace: '#' | ||
| + | </code> | ||
| + | |||
| + | ===== Copy / backup file ===== | ||
| + | |||
| + | <code> | ||
| + | - name: Ansible Copy using Conditional Statements | ||
| + | hosts: test_group | ||
| + | tasks: | ||
| + | #The env variables can be passed in: | ||
| + | #ex: ansible-playbook ~/playbook.yml -e "env=prod" | ||
| + | - name: Copy Apache config for production | ||
| + | copy: | ||
| + | src: prod_httpd.conf | ||
| + | dest: /etc/httpd/conf/httpd.conf | ||
| + | when: env == 'prod' | ||
| + | </code> | ||
| + | |||