Als Vorbereitung zur Migration auf einen neuen Server wird die Konfiguration getestet. Dies soll hier dokumentiert werden.
Zur Dokumentation und ggfs. Wiederholbarkeit soll die Konfiguration mittels Ansible-Skripten erfolgen. Dies benötigt keine Installation auf dem Server/Testsystem, sondern lediglich auf dem Host-System.
Ansible benötigt ein Inventory mit den zu steuernden Server.
inventory.ini
[all] localhost:8822 ansible_ssh_user=chaos
anisble.cfg
[defaults] # path to key for encrypted parameters vault_password_file = ./vault_pass.txt # enable timing information for tasks callback_whitelist = profile_tasks [privilege_escalation] # ask for the sudo pass on start become_ask_pass = true [ssh_connection] # ControlMaster=no was suggested for use with cygwin # ConnectTimeout=0 fixes long waits for timeouts on my system ssh_args = -o ControlMaster=no -o ConnectTimeout=0
Ansible kann verschlüsselte Variablen verwenden, um sensible Daten wie Passwörter oder access-tokens in öffentlich zugänglichen Playbooks zu schützen. Das Passwort kann auf der Kommandozeile angegeben oder in einer (nicht zu veröffentlichenden) Datei gespeichert werden. Letzteres erlaubt die Angabe des Pfades in der .cfg und wird in den Beispielen benutzt.
Die Datei enthält eine beliebe Zeichenfolge in einer Zeile.
ansible -i inventory.txt -l all -v playbook.yml #for generating encrypted variables: ansible-vault encrypt-string --vault-password-file vault_password.txt <put_string_to_encrypt_here>
su # login als root apt-get install sudo /sbin/adduser <nutzername> sudo # fügt nutzer zur sudo-gruppe hinzu exit # zurück zum User mkdir ~/.ssh nano ~/.ssh/authorized_keys #public key aus der cygwin-session des hosts hier her kopieren exit
Idealerweise sollte dies die einzigen direkt ausgeführten Befehle auf dem Server bleiben.
play_timezone.yml
- hosts: all become: yes tasks: - name: check if ntp is installed apt: pkg: - ntp state: present update_cache: no force_apt_get: True register: ntp - name: set hw clock to UTC from host command: hwclock --set --date "{{ lookup('pipe','date -u') }}" when: ntp.failed - name: Set timezone to Europe/Berlin timezone: name: Europe/Berlin hwclock: UTC register: tz - name: Unconditionally reboot the machine to apply tz change reboot: when: tz.changed - name: install ntp apt: pkg: - ntp state: present update_cache: yes force_apt_get: True when: ntp.failed - name: Make sure NTP is started up service: name=ntp state=started enabled=yes
play_packages.yml
- hosts: all become: yes tasks: - name: install apt packages apt: pkg: - git - python3 - metastore state: present update_cache: yes force_apt_get: True
play_git.yml
- hosts: all become: yes vars: access_token_decrypt: !vault | $ANSIBLE_VAULT;1.1;AES256 35383539633666323861613033623039646561613965353464366536623833623361623764303531 3333613330393939303338616161653339636264313662320a656464396239663335393539336434 34386666303363646639363831363134333733386262623462633536326235613335333633653830 3463663462323731310a663137346332626565656230356138303437353836363133363063373333 61333330363535326631653935326566653032373331306262333864326436366566 email_decrypt: !vault | $ANSIBLE_VAULT;1.1;AES256 31376661383735323533663362363861353337376230363636613462363734643634636634363430 6533393361353461613231633833336566333638346161360a313361633636373136646530323235 36393136396262323334643333353839623532373938316539363737376263666162326633356364 3838313833613438610a313761323737363332386262316332666330366437393136613364303631 39623139653234653231663163306261663862356639353232666339333361313165 gitlab_pass_decrypt: !vault | $ANSIBLE_VAULT;1.1;AES256 39376239396166313232393035393864346232343131623563373363636365613038613064333939 3065333533623636376264343136323261633764326363660a366264326562366539363934636534 31643365343864633335343135383939646238373430376239313034356338386636636634313435 3666383233656630640a663935633065646638363864613636366335316465663035393232333866 6230 tasks: - name: Creates directory for git hooks file: path: /etc/etc-git state: directory - name: Copy git hooks copy: src: ./conf/aptpostinstall.bash dest: /etc/etc-git/ owner: root group: root mode: '0744' - name: Copy git hooks2 copy: src: ./conf/aptpreinstall.bash dest: /etc/etc-git/ owner: root group: root mode: '0744' - name: register pre-install hook to dpgk lineinfile: path: /etc/apt/apt.conf.d/90etc-git create: yes regexp: 'aptpreinstall' line: DPkg::Pre-Invoke { '/etc/etc-git/aptpreinstall.bash' }; - name: register post-install hook to dpgk lineinfile: path: /etc/apt/apt.conf.d/90etc-git create: yes regexp: 'aptpostinstall' line: DPkg::Post-Invoke { '/etc/etc-git/aptpostinstall.bash' }; - name: Creates .ssh folder file: path: ~/.ssh state: directory - name: create ssh key openssh_keypair: path: ~/.ssh/id_ssh_rsa register: sshkey - name: "Adding ssh key to gitlab" gitlab_user: #api_url: https://gitlab-heimatsender.ddns.net/ api_token: "{{ access_token_decrypt }}" server_url: https://gitlab-heimatsender.ddns.net/ username: Chaos_99 name: Chaos_99 password: "{{ gitlab_pass_decrypt }}" email: "{{email_decrypt}}" sshkey_name: Ansible sshkey_file: "{{ sshkey.public_key }}" state: present - name: Creates temp directory for git clone file: path: ~/tempgit state: directory - name: clone homedir git repo in ~ git: repo: 'git@gitlab.heimatsender:Chaos_99/bytecluster.git' dest: ~/tempgit update: no accept_hostkey: true register: gitclone - name: install homedir synchronize: src: ~/tempgit dest: / recursive: yes when: gitclone.changed