# Nützliche Hinweise

### Links zu Ansible Docs

- [List of Behavioral Inventory Parameters](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#list-of-behavioral-inventory-parameters)
- [Best practices for variables and vaults](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#best-practices-for-variables-and-vaults)
- [Ansible Vault - verschlüsselter Speicher](https://docs.ansible.com/ansible/latest/user_guide/vault.html#providing-vault-passwords)

### Überprüfung der Erreichbarkeit aller Hosts

```
root@server:/etc/ansible# ansible -i /etc/ansible/INVENTORY -m ping HOST-GRUPPE
server-01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
server-02 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
server-03 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
```

### Verwendung der shell

Abfragen ob ein paket installiert ist:

`root@server#ansible -i /etc/ansible/INVENTORY -m shell -a "dpkg -l | grep PAKETNAME" HOST-GRUPPE`

Hier mal ein Beispiel:

```
root@server:/etc/ansible# ansible -i /etc/ansible/INVENTORY -m shell -a "dpkg -l | grep lsb-re" linuxserver
server-01 | SUCCESS | rc=0 >>
ii  lsb-release                 9.20161125       all     Linux Standard Base version report utility

server-02 | SUCCESS | rc=0 >>
ii  lsb-release                 9.20161125       all     Linux Standard Base version reping utility

server-03 | SUCCESS | rc=0 >>
ii  lsb-release                 9.20161125       all     Linux Standard Base version reporting uity

```

### Arbeiten mit Variablen, geschützten sensiblen Daten in Variablen usw.

<p class="callout info">Quelle: [https://www.digitalocean.com/community/tutorials/how-to-use-vault-to-protect-sensitive-ansible-data-on-ubuntu-16-04](https://www.digitalocean.com/community/tutorials/how-to-use-vault-to-protect-sensitive-ansible-data-on-ubuntu-16-04)</p>

##### So könnt ihr Variablen in config Dateien aufrufen:

`variableABC: "{{ variablexyz }}"`

Ich nutze das z.B. um Passwörter zu schützen, die ich in einer sog. "Ansible Vault" verschlüsselt gespeichert habe.

[Hier gehts zur Ansible Doku zum Thema Vault.](https://docs.ansible.com/ansible/latest/user_guide/vault.html#providing-vault-passwords)

##### Variablen - Hostnamen aus dem Inventory in config files einbauen

Wenn man z.b. ein Cluster für Graylog hat und möchte diese Cluster Nodes mit einer einheitlichen Konfiguration für einen Telegraf Datensammler bestücken dann muss man nicht für jeden Node eine separate telegraf.conf bereitstellen.

Ich habe es so gelöst:

1\. einheitliche telegraf.conf Datei:

```
[[inputs.graylog]]
metrics = ["...."]
password = "Graylog-Passwort"
servers = ["http://#graylog-node:9000/api/system/metrics/multiple"]
username = "graylog-user"
```

2\. Ansible Playbook

- In der Hostgruppe "g-gl-cluster" befinden sich die Hosts, die zum Graylog gehören.
- Mit dem Aufruf "{{ inventory\_hostname }}" wird dann der Hostname des Hosts, der gerade bearbeitet wird, aus dem Inventory ausgelesen und an die entsprechende Stelle in die config Datei. Dazu wird das Ansible Modul "lineinfile" benutzt: [https://docs.ansible.com/ansible/latest/modules/lineinfile\_module.html#lineinfile-module](https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html#lineinfile-module)

```
---
- hosts: g-gl-cluster
  tasks:
    - name: copy the telegraf config file to the group g-prometheus-clients
      copy:
       src: "/etc/ansible/etc-configs/telegraf/telegraf-inputs-graylog.conf"
       dest: "/etc/telegraf/telegraf.d/telegraf-inputs-graylog.conf"
       owner: root
       group: root
       mode: 0644
       backup: yes
    - name: Replace hostname in telegraf config file
      lineinfile:
        path: '/etc/telegraf/telegraf.d/telegraf-inputs-graylog.conf'
        regexp: '^servers'
        line: 'servers = ["http://{{ inventory_hostname }}:9000/api/system/metrics/multiple"]'
        state: present
        create: yes
    - name: Restart service telegraf, in all cases
      service:
       name: telegraf
       state: restarted
```

### Ausführung von Playbooks auf Hosts beschränken

`#ansible-playbook dein-playbook.yml --limit=einzelner-host`