What are the common `lineinfile` mistakes?
IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer
- A
regexpthat is too loose replaces the wrong line, or one that does not match makes the module append a duplicate on each run. - No
regexp: only the exactlineis checked, so changing the value later appends a second line. - Without
backup: trueorvalidate:, a bad edit tosshd_configorsudoerslocks you out. state: presentwithinsertafteronly applies when no match is found.
- ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PermitRootLogin'
line: 'PermitRootLogin no'
validate: '/usr/sbin/sshd -t -f %s'
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?Beginner