Ansible snippets: Difference between revisions
From DWIKI
mNo edit summary |
m (→Lineinfile) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 33: | Line 33: | ||
=Lineinfile= | =Lineinfile= | ||
*[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html Lineinfile documentation] | |||
==Quoting fun with lineinfile regex== | ==Quoting fun with lineinfile regex== | ||
- name: fix the needrestart config | - name: fix the needrestart config | ||
Line 41: | Line 43: | ||
line: '$nrconf{restart} = ''a''' | line: '$nrconf{restart} = ''a''' | ||
when: ansible_distribution == 'Ubuntu' and ansible_distribution_major_version == '22' | when: ansible_distribution == 'Ubuntu' and ansible_distribution_major_version == '22' | ||
==Comment out a line== | |||
- name: comment out a line | |||
lineinfile: | |||
dest: /etc/needrestart/needrestart.conf | |||
state: present | |||
backrefs: true | |||
regexp: '^(foo.*)' | |||
line: '# \1' | |||
==Check for string in file== | |||
- name: Check for rotate setting | |||
lineinfile: | |||
path: "{{ rotatepath }}" | |||
regex: '^ *rotate 180$' | |||
state: absent | |||
register: result | |||
- debug: msg="{{ inventory_hostname }} {{ result.found }}" | |||
==Remove part of string== | |||
use '''replace''' instead | |||
=Files= | =Files= | ||
Line 53: | Line 78: | ||
msg: "File /etc/fstab exists" | msg: "File /etc/fstab exists" | ||
when: fstab.stat.exists | when: fstab.stat.exists | ||
==Check if copy updated a file== | |||
- name: copy file | |||
copy: | |||
src: foo | |||
dest: /tmp/bar | |||
register: newfile | |||
- name: quit play for this host if file was up to date | |||
meta: end_host | |||
when: not newfile.changed | |||
==Modify permissions on files in a directory== | |||
ansible.builtin.file: | |||
mode: gw=rwX | |||
or | |||
- name: fix rights | |||
file: | |||
path: "{{ item.path }}" | |||
mode: u=rw,g=rw,o=r | |||
loop: "{{ query('filetree', '/tmp/foo') | selectattr('state', 'eq', 'file') }}" | |||
loop_control: | |||
label: "/{{ item.path }}" | |||
=Check if a command exists= | =Check if a command exists= | ||
Line 61: | Line 111: | ||
register: mysqld | register: mysqld | ||
failed_when: mysqld.rc == 2 | failed_when: mysqld.rc == 2 | ||
=Systemd= | |||
==Check if systemd service exists== | |||
- name: check for service | |||
name: foo | |||
register: servicestate | |||
# not-found or loaded | |||
- name: show status | |||
debug: | |||
var: servicestate.status.LoadState | |||
==Check if systemd service is enabled== | |||
- name: check for service | |||
name: foo | |||
register: servicestate | |||
when: servicestate.status.LoadState == 'loaded' | |||
- name: show status | |||
debug: | |||
var: servicestate.status.UnitFileState | |||
[[Category:Ansible]] |
Latest revision as of 11:35, 24 March 2025
Systemd
Randomize timer
Create /var/ansible/files/systemd/fstrim.conf
[Timer] RandomizedDelaySec=3h
Playbook:
tasks:
- name: check if /etc/systemd/system/fstrim.timer.d/ exists stat: path: /etc/systemd/system/fstrim.timer.d/ register: override_dir
- name: create /etc/systemd/system/fstrim.timer.d/ file: path: /etc/systemd/system/fstrim.timer.d/ state: directory when: override_dir.stat.exists == False
- name: add fstrim.timer override copy: src: /var/ansible/files/systemd/fstrim.conf dest: /etc/systemd/system/fstrim.timer.d/override.conf notify: daemon-reload
handlers:
- name: daemon-reload systemd: daemon_reload: yes
Lineinfile
Quoting fun with lineinfile regex
- name: fix the needrestart config lineinfile: dest: /etc/needrestart/needrestart.conf state: present regexp: '^#\$nrconf{restart}' line: '$nrconf{restart} = ''a''' when: ansible_distribution == 'Ubuntu' and ansible_distribution_major_version == '22'
Comment out a line
- name: comment out a line lineinfile: dest: /etc/needrestart/needrestart.conf state: present backrefs: true regexp: '^(foo.*)' line: '# \1'
Check for string in file
- name: Check for rotate setting lineinfile: path: "Template:Rotatepath" regex: '^ *rotate 180$' state: absent register: result
- debug: msg="Template:Inventory hostname Template:Result.found"
Remove part of string
use replace instead
Files
Check if a file exists
- name: check for a file stat: path: /etc/fstab register: fstab
- name: print message if exists ansible.builtin.debug: msg: "File /etc/fstab exists" when: fstab.stat.exists
Check if copy updated a file
- name: copy file copy: src: foo dest: /tmp/bar register: newfile
- name: quit play for this host if file was up to date meta: end_host when: not newfile.changed
Modify permissions on files in a directory
ansible.builtin.file: mode: gw=rwX
or
- name: fix rights file: path: "Template:Item.path" mode: u=rw,g=rw,o=r loop: "Template:Query('filetree', '/tmp/foo')" loop_control: label: "/Template:Item.path"
Check if a command exists
- name: check if mysqld is installed shell: which mysqld > /dev/null 2>&1 ignore_errors: true changed_when: false register: mysqld failed_when: mysqld.rc == 2
Systemd
Check if systemd service exists
- name: check for service name: foo register: servicestate
- not-found or loaded
- name: show status debug: var: servicestate.status.LoadState
Check if systemd service is enabled
- name: check for service name: foo register: servicestate when: servicestate.status.LoadState == 'loaded'
- name: show status debug: var: servicestate.status.UnitFileState