Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung |
centos:ansible:playbooks1 [14.09.2022 14:12. ] – django | centos:ansible:playbooks1 [14.09.2022 14:50. ] (aktuell) – gelöscht django |
---|
====== Ansible - Playbookbeispiele ====== | |
{{:centos:ansible:ansible_logo.png?nolink&125|Bild: Ansible Logo}} \\ \\ | |
| |
Nachdem wir uns bereits eingehend mit den **[[centos:ansible:start#grundlagen|Grundlagen]]** und auch schon das benötigte Programmpaket auf unserer Admin-Workstation zur Orchestrierung **[[centos:ansible:start#installation|installiert haben]]**, werden wir uns nun ein paar Beispile ansehen, wie man sich das Leben mit Ansible-Playbooks leichter gestalten kann. | |
| |
===== Playbook - Beispiele ===== | |
In den beiden Kapiteln **[[centos:ansible:start#playbooks|Playbooks]]** und **[[centos:ansible:start#yaml_-_was_ist_das|YAML - was ist das?]]** hatten wir uns schon eingehend mit den Hintergrundinformationen zu diesen beiden Themenblöcken beschäftigt, so dass wir uns nun direkt mit unserem ersten Playbook beschäftigen können. | |
| |
| |
| |
==== 05: NTP-Daemon chrony installieren und konfigurieren ==== | |
In folgendem Beispiel Nummer fünf wollen wir auf unseren **CentOS 8**-Hosts den NTP-Deamon **[[https://chrony.tuxfamily.org/|chrony]]** installieren und auch entsprechend als Client konfigurieren. | |
| |
=== Script anlegen === | |
Das Script legen wir wie auch schon bei den anderen Beispielen zuvor im Verzeichnis **''~/ansible''** an | |
$ vim 05_chrony.yml | |
| |
<file bash 05_chrony.yml>--- | |
- hosts: centos8 | |
become: true | |
vars: | |
sudoers: ansible | |
config_file: /etc/chrony.conf | |
# chronyd client config-options | |
chrony_pool: "server time.dmz.nausch.org iburst" | |
chrony_stratumweight: "stratumweight 0" | |
chrony_makestep: "makestep 10 3" | |
| |
tasks: | |
- name: Install chrony ntp Deamon | |
dnf: | |
#https://docs.ansible.com/ansible/latest/modules/dnf_module.html | |
name: chrony | |
state: latest | |
| |
- name: Check if /etc/chrony.orig does exists | |
stat: | |
#https://docs.ansible.com/ansible/latest/modules/stat_module.html | |
path: /etc/chrony.conf.orig | |
register: stat_result | |
| |
- name: Make a copy of /etc/chrony.conf as /etc/chrony.conf.orig | |
copy: | |
#https://docs.ansible.com/ansible/latest/modules/copy_module.html | |
remote_src: yes | |
src: /etc/chrony.conf | |
dest: /etc/chrony.conf.orig | |
when: stat_result.stat.exists == False | |
| |
- name: Copy template config-file in place | |
template: | |
#https://docs.ansible.com/ansible/latest/modules/template_module.html | |
src: templates/CentOS8/chrony-client.conf.j2 | |
dest: "{{ config_file }}" | |
| |
- name: Make sure Chrony is started up | |
service: | |
#https://docs.ansible.com/ansible/latest/modules/service_module.html | |
name: chronyd | |
state: started | |
enabled: yes | |
... | |
</file> | |
| |
Die Konfigurationsdatei unseres chrony-Daemon werden wir im Arbeitsbereich unserer ansible-Umgebung auf dem Admin-Rechner/-Server in einem eigenen Verzeichnis vorhalten. Diese Verzeichnis erstellen wir uns nun noch. | |
$ mkdir -p ~/ansible/templates/CentOS8/ | |
| |
Ansible nutzt die **[[https://jinja.palletsprojects.com/en/2.10.x/templates/|Jinja2 Template Engine]]** zum abgleich der verwendeten Variablen in einem Playbook. Wir werden also unsere Konfigurationsdatei entsprechend präparieren und dort ablegen. Als Datei-Extension verwenden wir hier **''.j2''**, um dies optisch abzutrennen. Wir könnten auch andere Datei-Extension verwenden, da Ansible selbst nur den Inhalt bzw. die Formatierung der Variablen interprätiert. | |
| |
| |
$ vim ~/ansible/templates/CentOS8/chrony-client.conf.j2 | |
| |
<file bash ~/ansible/templates/CentOS8/chrony-client.conf.j2># Use public servers from the pool.ntp.org project. | |
# Please consider joining the pool (http://www.pool.ntp.org/join.html). | |
{{ chrony_pool }} | |
| |
# Ignore stratum in source selection | |
{{ chrony_stratumweight }} | |
| |
# Record the rate at which the system clock gains/losses time. | |
driftfile /var/lib/chrony/drift | |
| |
# Allow the system clock to be stepped in the first three updates | |
# if its offset is larger than 1 second. | |
makestep 1.0 3 | |
| |
# Enable kernel synchronization of the real-time clock (RTC). | |
rtcsync | |
| |
# In first three updates step the system clock instead of slew | |
# if the adjustment is larger than 10 seconds. | |
{{ chrony_makestep }} | |
| |
# Enable hardware timestamping on all interfaces that support it. | |
#hwtimestamp * | |
| |
# Increase the minimum number of selectable sources required to adjust | |
# the system clock. | |
#minsources 2 | |
| |
# Allow NTP client access from local network. | |
#allow 192.168.0.0/16 | |
| |
# Serve time even if not synchronized to a time source. | |
#local stratum 10 | |
| |
# Specify file containing keys for NTP authentication. | |
keyfile /etc/chrony.keys | |
| |
# Get TAI-UTC offset and leap seconds from the system tz database. | |
leapsectz right/UTC | |
| |
# Specify directory for log files. | |
logdir /var/log/chrony | |
| |
# Select which information is logged. | |
#log measurements statistics tracking | |
| |
</file> | |
| |
Die drei Konfigurationsoptionen, die wir für unsere chrony-client Konfiguration später setzen und ggf. verändern wollen haben wir hier mit einer Variable belegt: | |
* **''%%{{%% chrony_pool %%}}%%''** : Server von dem bzw. denen wir später die Zeit beziehen wollen. | |
* **''%%{{%% chrony_stratumweight %%}}%%''** : Ignorieren der **stratum** Bewertung, da wir hier nur einen Zielhost zur Zeit befragen | |
* **''%%{{%% chrony_makestep %%}}%%''** : Definition wie bei den ersten Aktualisierungsschritten zu verfahren ist. | |
| |
=== Script Beschreibung === | |
Im Playbook greifen wir auf folgende Ansible-Module zurück: | |
* **[[https://docs.ansible.com/ansible/latest/modules/dnf_module.html|dnf]]** zum Installieren des Paketes | |
* **[[https://docs.ansible.com/ansible/latest/modules/stat_module.html|stat]]** und **[[https://docs.ansible.com/ansible/latest/modules/copy_module.html|copy]]** zum Sichern der originalen Konfigurationsdatei. | |
* **[[https://docs.ansible.com/ansible/latest/modules/template_module.html|template]]** zur Konfiguration unseres Daemon | |
* **[[https://docs.ansible.com/ansible/latest/modules/service_module.html|service]]** zum (automatischen) Starten (beim Systemstart des Hosts)). | |
| |
In unserem Playbook werden am Anfang den entsprechenden Variablen ihre werte zugewiesen. Im Anschluss daran werden fünft **''tasks''** definiert: | |
- Aufgabe: Installation des chrony NTP-Daemon | |
- Aufgabe: Überprüfen ob von der Konfigurationsdatei, die das RPM-Paket mitbrachte schon eine Sicherungskopie erstellt wurde. | |
- Aufgabe: Sofern bei der Prüfung in Aufgabe **2** noch keine Sicherungskopie erstellt wurde, wird eine Sicherungsopie erstellt. | |
- Aufgabe: Konfigurieren unseres chrony-Daemon | |
- Aufgabe: Starten des chrony-Daemon und aktivieren des automatischen Starts beim Starten des Hosts | |
| |
=== Script ausführen === | |
Zum Kopieren der unterschiedlichen Dateien rufen wir nun unser Playbook wie folgt auf: | |
$ ansible-playbook -v 05_chrony.yml | |
| |
<html><pre class="code"> | |
<font style="color: rgb(43, 100, 164)">Using /etc/ansible/ansible.cfg as config file</font> | |
<font style="color: rgb(0, 0, 0)">BECOME password: | |
| |
PLAY [centos8] ************************************************************************************************************************* | |
| |
TASK [Gathering Facts] *****************************************************************************************************************</font> | |
<font style="color: rgb(25, 100, 5)">ok: [www7.dmz.nausch.org] | |
ok: [www8.dmz.nausch.org]</font><br> | |
<font style="color: rgb(0, 0, 0)">TASK [Install chrony ntp Deamon] *******************************************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">changed: [www8.dmz.nausch.org] => {"changed": true, "msg": "", "rc": 0, "results": ["Installed: chrony", "Installed: chrony-3.3-3.el8.x86_64"]}</font><br> | |
<font style="color: rgb(0, 0, 0)">TASK [Check if /etc/chrony.orig does exists] *******************************************************************************************</font> | |
<font style="color: rgb(25, 100, 5)">ok: [www8.dmz.nausch.org] => {"changed": false, "stat": {"exists": false}}</font><br> | |
<font style="color: rgb(0, 0, 0)">TASK [Make a copy of /etc/chrony.conf as /etc/chrony.conf.orig] ************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">changed: [www8.dmz.nausch.org] => {"changed": true, "checksum": "89175e7c294dedf12bd473a952014e2cefd5766d", "dest": "/etc/chrony.conf.orig", "gid": 0, "group": "root", "md5sum": "97078948a9e2c1b99ab3e38d26a3311d", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 1085, "src": "/etc/chrony.conf", "state": "file", "uid": 0}</font><br> | |
<font style="color: rgb(0, 0, 0)">TASK [Copy template config-file in place] **********************************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">changed: [www8.dmz.nausch.org] => {"changed": true, "checksum": "37539ecdd11393937e5596894db41a02c6121c5f", "dest": "/etc/chrony.conf", "gid": 0, "group": "root", "md5sum": "adde7eeb1766f7f83bd3fba6cc30ec23", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 1265, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1578323551.4891849-132640554634531/source", "state": "file", "uid": 0}</font><br> | |
<font style="color: rgb(0, 0, 0)">TASK [Make sure Chrony is started up] **************************************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">changed: [www8.dmz.nausch.org] => {"changed": true, "enabled": true, "name": "chronyd", "state": "started", "status": {"ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", "After": "sysinit.target system.slice -.mount systemd-tmpfiles-setup.service sntp.service ntpdate.service tmp.mount systemd-journald.socket ntpd.service basic.target", "AllowIsolate": "no", "AmbientCapabilities": "", "AssertResult": "no", "AssertTimestampMonotonic": "0", "Before": "multi-user.target shutdown.target", "BlockIOAccounting": "no", "BlockIOWeight": "[not set]", "CPUAccounting": "no", "CPUQuotaPerSecUSec": "infinity", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "[not set]", "CPUUsageNSec": "[not set]", "CPUWeight": "[not set]", "CacheDirectoryMode": "0755", "CanIsolate": "no", "CanReload": "no", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend", "CollectMode": "inactive", "ConditionResult": "no", "ConditionTimestampMonotonic": "0", "ConfigurationDirectoryMode": "0755", "Conflicts": "systemd-timesyncd.service shutdown.target ntpd.service", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "NTP client/server", "DevicePolicy": "auto", "Documentation": "man:chronyd(8) man:chrony.conf(5)", "DynamicUser": "no", "EnvironmentFiles": "/etc/sysconfig/chronyd (ignore_errors=yes)", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "0", "ExecMainStartTimestampMonotonic": "0", "ExecMainStatus": "0", "ExecStart": "{ path=/usr/sbin/chronyd ; argv[]=/usr/sbin/chronyd $OPTIONS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStartPost": "{ path=/usr/libexec/chrony-helper ; argv[]=/usr/libexec/chrony-helper update-daemon ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "FailureAction": "none", "FileDescriptorStoreMax": "0", "FragmentPath": "/usr/lib/systemd/system/chronyd.service", "GID": "[not set]", "GuessMainPID": "yes", "IOAccounting": "no", "IOSchedulingClass": "0", "IOSchedulingPriority": "0", "IOWeight": "[not set]", "IPAccounting": " |