Use case 3 – services (systemd) management

In this use case, we will use an Ansible playbook to automatically set up and configure some system services on multiple hosts. The following lines of code show how to make sure a service is installed and then how to carry out a configuration check to make sure it is well-configured. Finally, we start the service and enable it to start upon system startup:

---
- name: Setup and configured recommended Linux services
hosts: Linux
become: yes
gather_facts: yes
tasks:
- name: Install a list of services on Linux hosts
package:
name: '{{ item }}'
state: latest
with_items:
- ntp
- tzdate
- autofs

- name: Fix time zone on Red Hat 6
lineinfile:
path: /etc/sysconfig/clock
line: "ZONE='Europe/London'"
state: present
create: yes
when: ansible_os_family == 'RedHat' and
ansible_distribution_version.split('.')[0] == '6'

- name: Setup time zone on all local hosts
timezone:
name: "Europe/London"

- name: Fix time zone on Red Hat 6
blockinfile:
path: /etc/ntp.conf
block: |
server time.nist.gov iburst
server 0.uk.pool.ntp.org iburst
server 1.uk.pool.ntp.org iburst
insertafter: "# Specify one or more NTP
servers."
state: present
when: ansible_os_family == 'RedHat'

- name: Restart NTP service to apply change and enable
it on Debian
systemd:
name: ntp
enabled: True
state: restarted
when: ansible_os_family == 'Debian'

- name: Restart NTP service to apply change and enable
it on Red Hat
systemd:
name: ntpd
enabled: True
state: restarted
when: ansible_os_family == 'RedHat'

- name: Add NFS and SMB support to automount
blockinfile:
path: /etc/auto.master
block: |
/nfs /etc/auto.nfs
/cifs /etc/auto.cifs
state: present

- name: create the NFS and SMB AutoFS configuration
files
file:
name: '{{ item }}'
state: touch
with_items:
- '/etc/auto.nfs'
- '/etc/auto.cifs'

- name: Restart AutoFS service to apply a change and
enable it
systemd:
name: autofs
enabled: True
state: restarted

This playbook can be called by another playbook as part of a provisioning task to configure the hosts after they are built. It is also possible to add extra functionalities to enable aspects of a bigger Ansible role.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.121.242