Ansible installieren

Nach einem Update des apt- Repositories wird zunächst pip installiert

apt update
apt install python3-pip

pip3 Version prüfen

ansible@server:~$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
ansible@server:~$

pyhton Version prüfen

Python 3.7.3
ansible@server:~$

ansible mit pip3 installieren

pip3 install ansible

ansible Version prüfen

ansible@server:~$ ansible --version
ansible 2.10.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/atom/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
ansible@server:~$

nano für YAML konfigurieren, falls noch keine syntax- Datei vorhanden

ls /usr/share/nano/

YAML syntax Datei erstellen

nano /usr/share/nano/yaml.nanorc
# Supports YAML files
syntax "YAML" ".ya?ml$"
header "^(---|===)" "%YAML"

## Keys
color magenta "^\s[$A-Za-z0-9_-]+\:" 
color brightmagenta "^\s@[$A-Za-z0-9_-]+\:"

## Values
color white ":\s.+$"

## Booleans
icolor brightcyan " (y|yes|n|no|true|false|on|off)$"

## Numbers
color brightred " [[:digit:]]+(.[[:digit:]]+)?"

## Arrays
color red "[" "]" ":\s+[|>]" "^\s*- "

## Reserved
color green "(^| )!!(binary|bool|float|int|map|null|omap|seq|set|str) "

## Comments
color brightwhite "#.*$"

## Errors
color ,red ":\w.+$"
color ,red ":'.+$"
color ,red ":".+$"
color ,red "\s+$"

## Non closed quote
color ,red "['\"][^['\"]]*$"

## Closed quotes
color yellow "['\"].*['\"]"

## Equal sign
color brightgreen ":( |$)"

Verzeichnisstruktur und Berechtigungen erstellen

cd ~
mkdir playbooks && mkdir playbooks/inventory && mkdir playbooks/groupvars && cd ~/playbooks

ansible konfigurieren

nano ~/playbooks/ansible.cfg
[defaults]
inventory = hosts
host_key_checking = False

ansible hosts konfigurieren

[hosts]
server1.myplaylab.net
server2.myplaylab.net

[hosts:vars]
ansible_python_interpreter=/usr/bin/python3

update.yml playbook erstellen (Quelle)

nano ~/playbooks/update.yml
---
- hosts: hosts
  become: true
  become_user: root
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no

    - name: Reboot the box if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists

Install Ansible on Debian
Sample Ansible Setup
Ansible security best practices
Ansible and Ansible Tower: best practices from the field