Ansible snippets: Difference between revisions

From DWIKI
mNo edit summary
mNo edit summary
Line 41: Line 41:
     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'
=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

Revision as of 16:22, 23 January 2023

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'


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