Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung |
centos:ansible:detail [10.01.2020 09:23. ] – [Inventarisierung größerer Umgebungen] django | centos:ansible:detail [24.09.2022 14:26. ] (aktuell) – gelöscht django |
---|
====== Ansible - Erweiterte Konfigurationsbeispiele ====== | |
{{:centos:ansible:ansible_logo.png?nolink&125|Bild: Ansible Logo}} \\ \\ | |
| |
Nachdem wir uns bereits eingehend mit den **[[centos:ansible:start#grundlagen|Grundlagen]]**, mit der **[[centos:ansible:start#installation|Installation von Ansible]]** und auch schon mit der Grundkonfiguration beschäftigt sowie erste Erfahrungen mit **[[centos:ansible:first#playbook_-_beispiele|Playbooks]]** gesammelt haben, wollen wir uns nun mit der tiefergehenden Konfiguration von Ansible beschäftigen. | |
| |
===== Inventory ===== | |
Zur Verwaltung/Inventarisierung von mehreren Knoten oder Hosts in unserer Infrastrukturumgebung verwendet Ansible Listen oder eine Gruppe von Listen, die man als **Inventory** bezeichnet. Im einfachsten Fall beschreibt solch eine Inventory-Datei lediglich Hosts und/oder deren Hostnamen bzw. IP-Adressen. Auf der anderen Seite kann man aber auch komplexe Landschaften abbilden und Eigenschaften einzelnen Hosts und /oider Gruppe zuordnen. | |
| |
Im Kapitel **[[centos:ansible:first#remote_user-anpassung_via_inventory_hostsyml|erweiterte Konfiguration - Anpassungen "Inventory hosts.yml"]]** hatten wir uns schon einmal kurz mit dem Thema **Inventory** beschäftigt. In den nachfolgenden Beispielen wollen wir nun intensiver auf Details dazu eingehen. | |
| |
Im Kapitel **[[https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html|How to build your inventory]]** der Ansible-Onlinedoku finden sich dazu vile tiefergehende Informationen. | |
| |
{{page>centos:ansible:hosts&nofooter&showheader}} | |
| |
| |
==== ~/ansible/hosts.yml ==== | |
Im Kapitel **[[centos:ansible:first#remote_user-anpassung_via_inventory_hostsyml|erweiterte Konfiguration - Anpassungen "Inventory hosts.yml"]]** hatten wir uns schon einmal kurz mit dem Thema **Inventory** beschäftigt. | |
| |
In der Ansible-Konfigurationsdatei **''/etc/ansible/ansible.cfg''** zeigt zu Beginn der default-Eintrag auf die vorgenannte Datei **[[#etc_ansible_hosts|/etc/ansible/hosts]]**. | |
| |
<code> ... | |
| |
[defaults] | |
| |
# some basic default values... | |
| |
#inventory = /etc/ansible/hosts | |
... | |
</code> | |
| |
Unsere erweiterte YAML-Konfigurationsdatei für die Hosts wolen wir aber künftig in der Ansible-Administationsumgebung unseres Admin-Users vorhalten und pflegen. Im Beispiel unseres Adminusers **django** wäre dies entsprechend der Pfad **''/home/django/ansible/hosts.yml''** | |
| |
Wir hinterlegen also dort, dass zukünftig die Inventory-Datei **''hosts.yml''** eingelesen und ausgewertet werden soll. | |
# vim /etc/ansible/ansible.cfg | |
| |
<code>[defaults] | |
| |
# some basic default values... | |
| |
# Django : 2020-01-01 | |
# default: #inventory = /etc/ansible/hosts | |
inventory = /home/django/ansible/hosts.yml | |
</code> | |
| |
=== inventory - Beispiel === | |
Dort legen wir uns unsere erweiterte Host-Datei im YAML-Format an. | |
$ vim /home/django/ansible/hosts.yml> | |
| |
<file bash /home/django/ansible/hosts.yml>--- #YAML start syntax (optional) | |
all: | |
hosts: # nicht zugeordnete Hosts | |
n3r0.intra.nausch.org: | |
g33k.intra.nausch.org: | |
children: # Untermenge/-gruppe aller Hosts | |
centos8: # Gruppe der definierten CentOS 8 Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: ansible | |
ansible_ssh_private_key_file: /home/django/.ssh/id_ed25519_ansible | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
www8.dmz.nausch.org: | |
ansible_ssh_host: 10.0.0.90 # Hostspezifische Ansible-Systemvariable | |
centos7: # Gruppe der definierten CentOS 7 Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: ansible | |
ansible_ssh_private_key_file: /home/django/.ssh/id_rsa_ansible | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
www7.dmz.nausch.org: | |
ansible_ssh_host: 10.0.0.97 # Hostspezifische Ansible-Systemvariable | |
# optische Abtrennung zu nachfolgenden Definitionen | |
ffmucgluon: # Definition der Gruppe aaler Freifunk Knoten/Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: root | |
ansible_ssh_private_key_file: /home/django/.ssh/id_rsa4096_freifunk_2014 | |
contact_info: 'Django [BOfH] | django@nausch.org | chat -> @django' | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
ff_pliening_gbw_ug: # Host | |
hostname: ff_pliening_gbw_ug # Hostspezifische Informationen / Variablen | |
latitude: -48.19861319429455 | |
longitude: -168.2017571420684 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: TP-Link TL-WDR4300 v1 | |
ansible_ssh_host: 2001:608:a01:102:32b5:c2ff:fe56:62b1 | |
| |
ff_pliening_gbw_egod: # Host | |
hostname: ffplieninggbwegod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_egod | |
latitude: 48.198652080 | |
longitude: 11.797969940 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fea9:22ed | |
| |
ff_pliening_gbw_ogod: # Host | |
hostname: ffplieninggbwogod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_ogod | |
latitude: 48.198699460 | |
longitude: 11.798053090 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec0:aaae | |
| |
ff_pliening_gbw_dgod: # Host | |
hostname: ffplieninggbwdgod # Hostspezifische Informationen / Variablen | |
prettyhostname: ff_pliening_gbw_dgod | |
latitude: 48.198671230 | |
longitude: 11.798122820 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec6:c8eb | |
| |
ff_pliening_gbw_cpod: # Host | |
hostname: ffplieninggbwcpod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_cpod | |
latitude: 48.198726280 | |
longitude: 11.798159030 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec6:c8dd | |
| |
ff_pliening_gbw_kvm_ol: # Host | |
hostname: ffplieninggbwkvmol # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_kvm_ol | |
latitude: 48.198646720 | |
longitude: 11.798047720 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Red Hat KVM | |
ansible_ssh_host: 2001:608:a01:102:5054:ff:fe9e:b358 | |
| |
... #YAML end syntax</file> | |
| |
| |
Die YAML-Konfigurationsdatei enthält entsprechende Bemerkungen, die die einzelnen Blöcke und Funktionen ausreichend beschreiben. Wie können so individuelle und Anwendungsspezifische Lösungen abbilden. | |
| |
<WRAP center round info 100%> | |
Natürlich gilt zu bedenken, dass das gezeigte Beispiel mit nur 8 Hosts und überschaubaren Variablen doch schon recht umfangreich geworden ist, wenn dies alles in eine Inventory-Datei gepackt wird. Bei Dutzenden oder Hunderten von Maschinen wird dies dann daraus schwer zu handeln - von verschachtelten Gruppen in Gruppen, oder Host die Mitglied in mehreren Gruppen sein sollen, sprechen wir dann besser gar nicht. Hier werden wir später auf eine **[[#inventarisierung_groesserer_umgebungen|andere/aufgeteilte Lösung]]** einschwenken müssen. | |
</WRAP> | |
| |
/* Viele Hinweise und Beispiel zu den Gruppen finden sich auch hier im Kapitel **[[https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html|How to build your inventory]]** der Ansible Dokumentation enthalten.*/ | |
| |
=== playbook === | |
Unsere gerade angelegte Inventory-Datei wollen wir nun verwenden um die Geolocation-Daten unserer Freifunk-Knoten zu organisieren. Die Geodaten sind in der obigen Inventory-Datei in den beiden Variablen **''latitude''** und **''longitude''** gespeichert - diese beiden Variablen nutzen wir nun in unserem Playbook. | |
$ ~/ansible/08_set_location.yml | |
| |
<file bash ~/ansible/08_set_location.yml> | |
--- | |
- hosts: ffmucgluon | |
gather_facts: False | |
| |
tasks: | |
- name: "Set GEO location of our own ffmuc-nodes" | |
#https://docs.ansible.com/ansible/latest/modules/raw_module.html | |
raw: uci set gluon-node-info.@location[0]="location"; uci set gluon-node-info.@location[0].share_location="1"; uci set gluon-node-info.@location[0].latitude="{{ latitude }}" ; uci set gluon-node-info.@location[0].longitude="{{ longitude }}"; uci commit gluon-node-info</file> | |
| |
=== Script ausführen === | |
Nun wollen wir unser ersten Playbook ausführen, um die Geodaten aus der Inventory-Datei auf unseren Freifunk-Knoten abzugleichen; hierzu rufen wir unser Script wie folgt auf: | |
$ ansible-playbook -v 08_change_contact.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 [ffmucgluon] ********************************************************************************************************************** | |
| |
<font style="color: rgb(0, 0, 0)">TASK [Update new contact-address on own ffmuc-nodes] ***********************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">changed: [ff_pliening_gbw_egod] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:1ae8:29ff:fea9:22ed closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:1ae8:29ff:fea9:22ed closed."], "stdout": "", "stdout_lines": []} | |
changed: [ff_pliening_gbw_kvm_ol] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:5054:ff:fe9e:b358 closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:5054:ff:fe9e:b358 closed."], "stdout": "", "stdout_lines": []} | |
changed: [ff_pliening_gbw_ogod] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:1ae8:29ff:fec0:aaae closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:1ae8:29ff:fec0:aaae closed."], "stdout": "", "stdout_lines": []} | |
changed: [ff_pliening_gbw_ug] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:32b5:c2ff:fe56:62b1 closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:32b5:c2ff:fe56:62b1 closed."], "stdout": "", "stdout_lines": []} | |
changed: [ff_pliening_gbw_dgod] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:1ae8:29ff:fec6:c8eb closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:1ae8:29ff:fec6:c8eb closed."], "stdout": "", "stdout_lines": []} | |
changed: [ff_pliening_gbw_cpod] => {"changed": true, "rc": 0, "stderr": "Shared connection to 2001:608:a01:102:1ae8:29ff:fec6:c8dd closed.\r\n", "stderr_lines": ["Shared connection to 2001:608:a01:102:1ae8:29ff:fec6:c8dd closed."], "stdout": "", "stdout_lines": []}</font><br> | |
<font style="color: rgb(0, 0, 0)">PLAY RECAP *****************************************************************************************************************************</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_cpod </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_dgod </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_egod </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_kvm_ol </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_ogod </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<font style="color: rgb(196, 160, 0)">ff_pliening_gbw_ug </font><font style="color: rgb(0, 0, 0)">: </font><font style="color: rgb(25, 100, 5)">ok=1 </font><font style="color: rgb(196, 160, 0)">changed=1 </font><font style="color: rgb(0, 0, 0)">unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</font> | |
<br></pre> | |
</html> | |
| |
=== Ergebnis === | |
Alle unsere eigenen definierten Freifunk Knoten haben nun auf der **[[https://map.ffmuc.net/#!/de/map/18e829a922ed|Freifunk München Karte]]** die aktualisierten GeoDaten. | |
| |
==== Inventarisierung größerer Umgebungen ==== | |
Wie schon im vorhergehenden Beispiel angemerkt, wird es bei einer größeren Anzahl von Hostdefinitionen mit umfangreichen Gruppenzugehörigkeiten mit verschiedensten zugeordneten Variablen sehr schnell "schmutzig". All das in einer einzigsten Konfiguration abbilden zu wollen, wird man vermutlich in die Ecke "Hand zu masochistischen Tendenzen" oder Selbstgeisselung eines Admins einordnen - kurzum hier muss ein anderer Lösungsansatz her! | |
| |
$ mkdir host_vars | |
| |
$ mkdir group_vars | |
| |
| |
/* | |
=== inventory - Beispiel === | |
Dort legen wir uns unsere erweiterte Host-Datei im YAML-Format an. | |
$ vim /home/django/ansible/hosts.yml> | |
| |
<file bash /home/django/ansible/hosts.yml>--- #YAML start syntax (optional) | |
all: | |
hosts: # nicht zugeordnete Hosts | |
n3r0.intra.nausch.org: | |
g33k.intra.nausch.org: | |
children: # Untermenge/-gruppe aller Hosts | |
centos8: # Gruppe der definierten CentOS 8 Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: ansible | |
ansible_ssh_private_key_file: /home/django/.ssh/id_ed25519_ansible | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
www8.dmz.nausch.org: | |
ansible_ssh_host: 10.0.0.90 # Hostspezifische Ansible-Systemvariable | |
centos7: # Gruppe der definierten CentOS 7 Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: ansible | |
ansible_ssh_private_key_file: /home/django/.ssh/id_rsa_ansible | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
www7.dmz.nausch.org: | |
ansible_ssh_host: 10.0.0.97 # Hostspezifische Ansible-Systemvariable | |
# optische Abtrennung zu nachfolgenden Definitionen | |
ffmucgluon: # Definition der Gruppe aaler Freifunk Knoten/Hosts | |
vars: # Variablen, die für die ganze Gruppe gelten | |
ansible_ssh_port: 22 | |
ansible_ssh_user: root | |
ansible_ssh_private_key_file: /home/django/.ssh/id_rsa4096_freifunk_2014 | |
contact_info: 'Django [BOfH] | django@nausch.org | chat -> @django' | |
hosts: # Liste aller Hosts die dieser gruppe zugehören | |
ff_pliening_gbw_ug: # Host | |
hostname: ff_pliening_gbw_ug # Hostspezifische Informationen / Variablen | |
latitude: -48.19861319429455 | |
longitude: -168.2017571420684 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: TP-Link TL-WDR4300 v1 | |
ansible_ssh_host: 2001:608:a01:102:32b5:c2ff:fe56:62b1 | |
| |
ff_pliening_gbw_egod: # Host | |
hostname: ffplieninggbwegod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_egod | |
latitude: 48.198652080 | |
longitude: 11.797969940 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fea9:22ed | |
| |
ff_pliening_gbw_ogod: # Host | |
hostname: ffplieninggbwogod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_ogod | |
latitude: 48.198699460 | |
longitude: 11.798053090 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec0:aaae | |
| |
ff_pliening_gbw_dgod: # Host | |
hostname: ffplieninggbwdgod # Hostspezifische Informationen / Variablen | |
prettyhostname: ff_pliening_gbw_dgod | |
latitude: 48.198671230 | |
longitude: 11.798122820 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec6:c8eb | |
| |
ff_pliening_gbw_cpod: # Host | |
hostname: ffplieninggbwcpod # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_cpod | |
latitude: 48.198726280 | |
longitude: 11.798159030 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Ubiquiti UniFi-AC-MESH | |
ansible_ssh_host: 2001:608:a01:102:1ae8:29ff:fec6:c8dd | |
| |
ff_pliening_gbw_kvm_ol: # Host | |
hostname: ffplieninggbwkvmol # Hostspezifische Informationen / Variablen | |
pretty_hostname: ff_pliening_gbw_kvm_ol | |
latitude: 48.198646720 | |
longitude: 11.798047720 | |
branch: stable | |
domain: ffmuc_muc_ost | |
director: ffmuc_muc_ost | |
modell: Red Hat KVM | |
ansible_ssh_host: 2001:608:a01:102:5054:ff:fe9e:b358 | |
| |
... #YAML end syntax</file> | |
| |
| |
Die YAML-Konfigurationsdatei enthält entsprechende Bemerkungen, die die einzelnen Blöcke und Funktionen ausreichend beschreiben. Wie können so individuelle und Anwendungsspezifische Lösungen abbilden. | |
*/ | |
| |
FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME | |
| |
| |
| |
/* https://gerardnico.com/infra/ansible/start */ | |