30 Ansible für ff@home aufsetzen.page 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. ---
  2. format: markdown
  3. title: Ansible ff@home aufsetzen
  4. toc: yes
  5. ...
  6. # Software
  7. - Debian 12 (bookworm) oder Ubuntu 22.04
  8. - python3 und ansible
  9. ***** Es empfiehlt sich, eine python virtuelle Umgebung zu verwenden *****
  10. Die folgenden Pakete installieren:
  11. sudo apt install python3-pip python3-venv
  12. Dann in das Basisverzeichnis gehen (bei mir /datadisk) und die virtuelle Umgebung anlegen
  13. python3 -m venv ffhome
  14. In dem Verzeichnis ffhome die Umgebung aktivieren
  15. source bin/activate
  16. Dadurch ändert sich der prompt:
  17. (ffhome) 18:16:41[frankb@berglap /datadisk/ffhome 0]
  18. Zum Verlassen der Umgebung:
  19. deactivate oder Terminal Fenster schliessen
  20. Jetzt kann ansible in der ffhome Umgebung installiert werden:
  21. pip3 install ansible
  22. 18:11:20[frankb@berglap /datadisk/ffhome 0]
  23. Die Verzeichnistruktur der Testumgebung
  24. tree -L 4 /datadisk/ffhome
  25. /datadisk/ffhome
  26. ├── ansible.cfg
  27. ├── bin
  28. │   ├── activate
  29. │   ├── activate.csh
  30. │   ├── activate.fish
  31. │   ├── Activate.ps1
  32. │   ├── ansible
  33. │   ├── ansible-community
  34. ...
  35. │   └── yamllint
  36. ├── include
  37. ├── inventory
  38. │   ├── hosts.yaml
  39. │   └── host_vars
  40. | │   ├── bergdesk
  41. │   │   │   ├── vars
  42. │   │   │   ├── vars.01.bak
  43. │   │   │   ├── vars.02.bak
  44. │   │   │   └── vault
  45. │   │   ├── berghofen
  46. │   │   │   ├── vars
  47. │   │   │   ├── vars.01.bak
  48. │   │   │   ├── vars.bak
  49. │   │   │   └── vault
  50. │   │   ├── berglap
  51. │   │   │   ├── vars
  52. │   │   │   ├── vars.01.bak
  53. │   │   │   ├── vars.bak
  54. │   │   │   └── vault
  55. │   │   └── luna
  56. │   │   ├── vars
  57. │   │   └── vault
  58. │   └── resources
  59. │   └── host
  60. │   └── bergdesk
  61. ├── lib
  62. │   └── python3.10
  63. ├── lib64 -> lib
  64. ...
  65. ├── playbooks
  66. │   ├── resources
  67. │   │   └── host
  68. │   │   ├── tincbergdesk.gz
  69. │   │   └── tincberghofen.gz
  70. │   ├── templates
  71. │   │   ├── tinc-up.lan.j2
  72. │   │   └── tinc-up.wan.j2
  73. │   ├── tincbuild.yaml
  74. │   └── update.yaml
  75. └── pyvenv.cfg
  76. Bei den ... sind Zeilen der Übersichtlichkeit halber weggelassen.
  77. Die verwendete ansible.cfg:
  78. [defaults]
  79. inventory=./inventory/
  80. roles_path=./roles
  81. playbook_dir=./playbooks/
  82. interpreter_python=auto_silent
  83. #vault_password_file=~/.vault-password
  84. log_path=/tmp/ansible_ffhome.log
  85. cow_selection=random
  86. nocows=True
  87. stdout_callback=yaml
  88. display_args_to_stdout=True
  89. [privilege_escalation]
  90. become=True
  91. [ssh_connection]
  92. pipelining=True
  93. Datei mit den beteiligten Hosts inventory/hosts.yaml
  94. ---
  95. all:
  96. vars:
  97. ansible_port: 24
  98. ansible_user: frankb
  99. ansible_become: true
  100. apus:
  101. hosts:
  102. berghofen:
  103. ansible_host: 192.168.178.51
  104. ansible_user: fb
  105. ansible_become_password: "{{ berghofen_password }}"
  106. desktops:
  107. hosts:
  108. bergdesk:
  109. ansible_host: 192.168.178.201
  110. ansible_become_pass: '{{ bergdesk_password }}'
  111. berglap:
  112. ansible_host: 192.168.178.52
  113. ansible_become_pass: '{{ berglap_password }}'
  114. luna:
  115. ansible_host: 192.168.178.224
  116. ansible_become_pass: '{{ luna_password }}'
  117. altlast:
  118. hosts:
  119. hoerde:
  120. ansible_host: 193.43.220.136
  121. ansible_become: true
  122. ansible_become_method: su
  123. supernodes:
  124. hosts:
  125. 31.172.33.20:
  126. ansible_port: 22
  127. snng-dus01.ffdo.de:
  128. ansible_port: 22
  129. snng-dtm01.ffdo.de:
  130. ansible_port: 22
  131. Die Gruppen apus und desktops enthalten die testhosts, die Gruppen altlast und supernodes sind nicht komplett einbezogen.
  132. Im Verzeichnis inventory/host_vars sind Variablen für die einzelnen hosts, u.a. die passwords, in vars unverschlüsselt, in vault aes256 geschützt. Weiter sind Parameter für tinc enthalten, nur in vars, unverschlüsselt..
  133. Beispielhaft für berghofen
  134. berghofen/vars
  135. ---
  136. berghofen_password: "{{ vault_berghofen_password }}"
  137. tinc_bindto: 192.168.178.51
  138. wan_broadcast_ip: 193.43.220.191
  139. wan_ip: 193.43.220.162/27
  140. lan_broadcast_ip: 192.168.34.255
  141. lan_ip: 192.168.34.1/24
  142. berghofen/vault
  143. $ANSIBLE_VAULT;1.2;AES256;xx
  144. 35656536383233636434636533613830303439316263636436363932333636626462616461636537
  145. 3838626266396332363236643361626134393238636133640a646333333866643161356333626564
  146. 32373735343033633666353763376230646137663639373438393537663031643562376365396337
  147. 3161646534666236350a303366373433373833373066353030363766616166666361376637393464
  148. 30613139313661643932373239333865616338653132613530393161656466326561633537383535
  149. 3631356664643139383037636565346630643036353364333866
  150. In der vault Datei (hier Klartext)
  151. ---
  152. vault_berghofen_password: hier das echte PW eintragen
  153. dann die vault Datei verschlüsseln mit
  154. ansible-vault encrypt vault --vault-id xxxxx@prompt
  155. anzeigen kann man die Datei mit
  156. ansible-vault view vault
  157. und wieder entschlüsseln mit
  158. ansible-vault decrypt vault
  159. Es gibt z.Zt. zwei playbooks: update.yaml und tincbuild.yaml
  160. playbooks/update.yaml
  161. ---
  162. # name: update yaml
  163. - hosts: [desktops,apus,altlast]
  164. tasks:
  165. - name: Testausgabe
  166. debug: msg="Hallo von {{ ansible_hostname }} Ansible managed!"
  167. - name: df -h Aufruf
  168. command: df -h /
  169. changed_when: false
  170. register: df_cmd
  171. - debug:
  172. msg: '{{df_cmd.stdout_lines}} {{ansible_distribution }}'
  173. - name: ping meine hosts
  174. ansible.builtin.ping:
  175. changed_when: false
  176. # - name: Warte auf enter Taste
  177. # ansible.builtin.pause:
  178. - name: Ist flatpak installiert
  179. stat:
  180. path: /usr/bin/flatpak
  181. register: flatpak_file
  182. when: ansible_os_family == "Debian"
  183. - name: Update flatpak Pakete falls snap vh
  184. command: /usr/bin/flatpak update
  185. when: ansible_os_family == "Debian" and flatpak_file.stat.exists
  186. - name: Ist snap installiert
  187. stat:
  188. path: /usr/bin/snap
  189. register: snap_file
  190. when: ansible_os_family == "Debian"
  191. - name: Update snap Pakete falls snap vh
  192. command: /usr/bin/snap refresh
  193. when: ansible_os_family == "Debian" and snap_file.stat.exists
  194. - name: apt update mit upgrade und autoremove
  195. ansible.builtin.apt:
  196. update_cache: yes
  197. cache_valid_time: 3600
  198. autoremove: yes
  199. upgrade: 'yes'
  200. when: ansible_os_family == "Debian"
  201. - stat:
  202. path: /var/run/needrestart
  203. register: needrestart_file
  204. - name: reboot falls erforderlich
  205. ansible.builtin.reboot:
  206. when:
  207. - ansible_os_family == "Debian" and
  208. ( needrestart_file.stat.exists or
  209. rebootreq_file.stat.exists
  210. )
  211. - name: playbook hier beenden bei ! debian
  212. meta: end_play
  213. when: ansible_os_family != "Debian"
  214. playbooks/tincbuild.yaml
  215. ---
  216. # name: tincbuild yaml
  217. - hosts: [desktops, apus, altlast]
  218. vars:
  219. TINC_VERSION: 1.1pre18
  220. tinc_dev: /dev/net/tun
  221. tinc_mode: switch
  222. tinc_adr_family: ipv4
  223. tinc_max_timeout: 30
  224. tinc_port:
  225. lan: 10001
  226. wan: 661
  227. tasks:
  228. # - name: Show facts available on the system
  229. # ansible.builtin.debug:
  230. # var: ansible_facts
  231. # das folgende klappt leider nicht {{tinc_port.{{ item }}}} syntaxfehler
  232. # geschachtelte variablen gehen nicht.
  233. # - name: willi
  234. # debug: msg="var {{ tinc_port.{{lan}}}}"
  235. - name: Testausgabe
  236. debug: msg="Hallo von {{ ansible_hostname }} Ansible managed!"
  237. - name: df -h Aufruf
  238. command: df -h /
  239. changed_when: false
  240. register: df_cmd
  241. - debug:
  242. msg: '{{df_cmd.stdout_lines}} {{ansible_distribution }}'
  243. # - name: ping meine hosts
  244. # ansible.builtin.ping:
  245. # changed_when: false
  246. # - name: Warte auf enter Taste
  247. # ansible.builtin.pause:
  248. - name: playbook hier beenden bei ! debian
  249. meta: end_play
  250. when: ansible_os_family != "Debian"
  251. - name: Ist tinc Konfigurationsverzeichnis vorhanden
  252. stat:
  253. path: /etc/tinc
  254. register: tinckonf
  255. - name: Gibt es Sicherung von tinc Konfiguration
  256. # ueberlebt keinen reboot
  257. stat:
  258. path: /tmp/tinc{{ ansible_hostname }}.gz
  259. register: tincgz
  260. - name: Konfiguration packen wenn noetig
  261. community.general.archive:
  262. path: /etc/tinc
  263. dest: /tmp/tinc{{ ansible_hostname }}.gz
  264. when: tinckonf.stat.exists
  265. - name: Gesicherte Konfiguration auf Controlnode kopieren
  266. ansible.builtin.fetch:
  267. src: /tmp/tinc{{ ansible_hostname }}.gz
  268. dest: resources/host/
  269. flat: true
  270. when: tinckonf.stat.exists
  271. # - name: Gesicherte Konfiguration auf targetnode loeschen
  272. # file:
  273. # name: /tmp/tinc{{ ansible_hostname }}.gz
  274. # state: absent
  275. # when: tincgz.stat.exists
  276. - name: Abhängigkeiten fuer tinc installieren
  277. apt:
  278. pkg: "{{ item }}"
  279. state: present
  280. with_items:
  281. - build-essential
  282. - libncurses-dev
  283. - libreadline-dev
  284. - pkg-config
  285. - zlib1g-dev
  286. - liblzo2-dev
  287. - libssl-dev
  288. - texinfo
  289. - name: create directory for tinc
  290. file:
  291. name: /opt/tinc
  292. state: directory
  293. - name: Download tinc source
  294. get_url:
  295. url: "https://www.tinc-vpn.org/packages/tinc-{{TINC_VERSION}}.tar.gz"
  296. dest: /opt/tinc/tinc-{{TINC_VERSION}}.tar.gz
  297. register: gettinc
  298. - name: tinc-Quellen entpacken
  299. unarchive:
  300. src: /opt/tinc/tinc-{{TINC_VERSION}}.tar.gz
  301. dest: /usr/src
  302. remote_src: true
  303. when:
  304. - gettinc.changed
  305. - name: Pruefen tinc programm vh
  306. stat:
  307. path: /usr/sbin/tinc
  308. register: tinc_bin
  309. - name: tinc kompilieren und installieren
  310. shell: "cd /usr/src/tinc-{{TINC_VERSION}}
  311. && ./configure --prefix=/usr --sysconfdir=/etc --runstatedir=/run
  312. --localstatedir=/var --with-systemd
  313. && make
  314. && make install"
  315. when:
  316. - tinc_bin.stat.exists == False or gettinc.changed
  317. - name: Gibt es schon eine (alte) tinc Konfiguration
  318. stat:
  319. path: /etc/tinc
  320. register: tinc_etc
  321. - name: tinc Konfigurationsreste beseitigen
  322. file:
  323. path: /etc/tinc
  324. state: absent
  325. when:
  326. - tinc_etc.stat.exists
  327. - name: tinc lan/wan vorkonfigurieren in mehreren Schritten
  328. shell: "tinc --net={{ item }} init {{ansible_hostname}}"
  329. with_items:
  330. - lan
  331. - wan
  332. # when:
  333. # - tinc_bin.stat.exists == False or gettinc.changed
  334. - name: 2048er priv und pub keys löschen
  335. shell: "rm /etc/tinc/{{ item }}/*.priv
  336. && rm /etc/tinc/{{ item }}/hosts/*"
  337. with_items:
  338. - lan
  339. - wan
  340. # when:
  341. # - tinc_bin.stat.exists == False or gettinc.changed
  342. - name: 4096er keys generieren
  343. shell: "tinc --net={{ item }} -b generate-keys 4096"
  344. with_items:
  345. - lan
  346. - wan
  347. # when:
  348. # - tinc_bin.stat.exists == False or gettinc.changed
  349. - name: tinc.conf einrichten
  350. shell: "tinc --net={{ item }} set Device {{tinc_dev}}
  351. && tinc --net={{ item }} set Mode {{tinc_mode}}
  352. && tinc --net={{ item }} set AddressFamily {{tinc_adr_family}}
  353. && tinc --net={{ item }} set MaxTimeout {{tinc_max_timeout}}
  354. && tinc --net={{ item }} set BindToAddress {{tinc_bindto}}"
  355. with_items:
  356. - lan
  357. - wan
  358. # when:
  359. # - tinc_bin.stat.exists == False or gettinc.changed
  360. - name: lan/wan ports setzen
  361. # das geht leider nicht in loop, da Variablen nicht geschachtelt
  362. shell: "tinc --net=lan set Port {{ tinc_port.lan }}
  363. && tinc --net=wan set Port {{ tinc_port.wan }}"
  364. - name: tinc-up anpassen
  365. ansible.builtin.template:
  366. src: templates/tinc-up.{{ item }}.j2
  367. dest: /etc/tinc/{{ item }}/tinc-up
  368. with_items:
  369. - lan
  370. - wan
  371. # when:
  372. # - tinc_bin.stat.exists == False or gettinc.changed
  373. - meta: end_play
  374. Aufruf der beiden playbooks mit
  375. ansible-playbook -b playbooks/update.yaml -i inventory/hosts.yaml --ask-vault-pass
  376. ansible-playbook -b playbooks/tincbuild.yaml -i inventory/hosts.yaml --ask-vault-pass
  377. Gekürzte Ausgabe:
  378. (ffhome) 20:55:59[frankb@berglap /datadisk/ffhome 4] ansible-playbook -b playbooks/update.yaml -i inventory/hosts.yaml --ask-vault-pass
  379. Vault password:
  380. PLAY [desktops,apus,altlast] ***************************************************************
  381. TASK [Gathering Facts] *********************************************************************
  382. [WARNING]: Platform linux on host berglap is using the discovered Python interpreter at
  383. /usr/bin/python3.10, but future installation of another Python interpreter could change the
  384. meaning of that path. See https://docs.ansible.com/ansible-
  385. core/2.17/reference_appendices/interpreter_discovery.html for more information.
  386. ok: [berglap]
  387. [WARNING]: Platform linux on host bergdesk is using the discovered Python interpreter at
  388. /usr/bin/python3.11, but future installation of another Python interpreter could change the
  389. meaning of that path. See https://docs.ansible.com/ansible-
  390. core/2.17/reference_appendices/interpreter_discovery.html for more information.
  391. ok: [bergdesk]
  392. fatal: [luna]: UNREACHABLE! => changed=false
  393. msg: 'Failed to connect to the host via ssh: ssh: connect to host 192.168.178.224 port 24: No route to host'
  394. unreachable: true
  395. [WARNING]: Platform linux on host berghofen is using the discovered Python interpreter at
  396. /usr/bin/python3.11, but future installation of another Python interpreter could change the
  397. meaning of that path. See https://docs.ansible.com/ansible-
  398. core/2.17/reference_appendices/interpreter_discovery.html for more information.
  399. ok: [berghofen]
  400. [WARNING]: Platform freebsd on host hoerde is using the discovered Python interpreter at
  401. /usr/local/bin/python3.9, but future installation of another Python interpreter could
  402. change the meaning of that path. See https://docs.ansible.com/ansible-
  403. core/2.17/reference_appendices/interpreter_discovery.html for more information.
  404. ok: [hoerde]
  405. TASK [Testausgabe] *************************************************************************
  406. ok: [bergdesk] =>
  407. msg: Hallo von bergdesk Ansible managed!
  408. ok: [berglap] =>
  409. msg: Hallo von berglap Ansible managed!
  410. ok: [berghofen] =>
  411. msg: Hallo von berghofen Ansible managed!
  412. ok: [hoerde] =>
  413. msg: Hallo von hoerde Ansible managed!
  414. TASK [df -h Aufruf] ************************************************************************
  415. ok: [berglap]
  416. ok: [bergdesk]
  417. ok: [berghofen]
  418. ok: [hoerde]
  419. TASK [debug] *******************************************************************************
  420. ok: [bergdesk] =>
  421. msg: '[''Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf'', ''/dev/sdb1 439G 98G 319G 24% /''] Debian'
  422. ok: [berglap] =>
  423. msg: '[''Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf'', ''/dev/mapper/system-root 444G 298G 124G 71% /''] Ubuntu'
  424. ok: [berghofen] =>
  425. msg: '[''Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf'', ''/dev/sda6 18G 5,1G 12G 30% /''] Debian'
  426. ok: [hoerde] =>
  427. msg: '[''Filesystem Size Used Avail Capacity Mounted on'', ''s3pool25/jail/hoerde.ffdo.net 3.9G 891M 3.0G 22% /''] FreeBSD'
  428. TASK [ping meine hosts] ********************************************************************
  429. ok: [berglap]
  430. ok: [bergdesk]
  431. ok: [berghofen]
  432. ok: [hoerde]
  433. ...
  434. PLAY RECAP *********************************************************************************
  435. bergdesk : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  436. berghofen : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  437. berglap : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  438. hoerde : ok=5 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
  439. luna : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0