====== - Ansible guide ======
===== - Best practices =====
==== - Using variable ====
2 types of variables:
* playbook parameters: define how the playbook behaves, i.e.,
* what roles to run for each hosts --> These variables seem to be host-vars
* role configuration --> this should be avoided. Role related variables see the 2nd type vars.
* Role variables: mostly should store data so the role can be reused with data for each playbook. E.g., role to set iptables rules taking rules parameters from variables.
* How to change role behavior though?
==== - On the web ====
* https://steampunk.si/blog/ansible-role-argument-specification/
* https://sensu.github.io/sensu-go-ansible/roles/install.html
* https://github.com/sensu/sensu-go-ansible/tree/master/roles/install
===== - Tips&Tricks =====
Display facts from all hosts and store them indexed by hostname at /tmp/facts.
ansible -i inventory workers -m gather_facts --tree /tmp/facts |less
List tags and tasks
ansible-playbook -i inventory --extra-vars "variable_hosts=k8s-02" install_k8s_playbook.yml --tags k8s --skip-tags masters,install --list-tasks -vvvv
ansible-playbook -i inventory --extra-vars "variable_hosts=k8s-02" install_k8s_playbook.yml --tags k8s --skip-tags masters,install --list-tags -vvvv
===== - Troubleshooting =====
* https://www.puzzle.ch/de/blog/articles/2020/01/22/10-dinge-ueber-ansible-die-du-vielleicht-noch-nicht-kanntest
==== - Tasks of included task not shown in task list ====
When using dynamic includes, it is important to keep these limitations in mind:
- You cannot use notify to trigger a handler name which comes from a dynamic include.
- You cannot use --start-at-task to begin execution at a task inside a dynamic include.
- Tags which only exist inside a dynamic include will not show up in –list-tags output.
- Tasks which only exist inside a dynamic include will not show up in –list-tasks output.
==== - Fact not defined for included tasks.yml ====
Problem:
1 ---
2 # tasks file for install
3 - name: debug k8s.install undef fact
4 debug:
5 msg:
6 - "role_path: {{ role_path }}"
7 - "undef? ansible_pkg_mgr: {{ ansible_pkg_mgr }}"
8
9 - name : setup package repo
10 import_tasks: repositories.yml
11 tags: [install]
12 when: ansible_facts.os_family != "Windows"
13 # error: undef ansible_pkg_mgr
14
15 #- name: install k8s packages
16 # import_tasks: packages.yml
17 # tags: [install]
18 #
main.yml :b9[yaml] 6,31 All 1
---
2 #k8s-cluster-ansible/roles/k8s/roles/install/tasks/repositories.yml
3
4 - name: package repositories for "{{ ansible_pkg_mgr }}"
5 import_tasks: "{{ ansible_pkg_mgr }}/repos.yml"
6
~ ~
Solution:
Variables in task name not resolved for import_tasks. Must use include_tasks. Use apply.tags to recursively set tags.
include_tasks: task_name
tags: [always, or, t,a,g] <-- needed for the include task to be executed.
args:
apply:
tags: [t,a,g]
become: yes
* https://serverfault.com/questions/1023392/use-ansible-include-tasks-with-tags-on-the-sub-tasks
==== - Variable in included task name ====
include_tasks: "{{ ansible_pkg_mgr }}/repos.yml" # variable in task name not resolved for import_tasks. Must use include_tasks. Use apply.tags to recursively set tags.
In this case may be use when to check the variable then select file name to be **imported**.
==== - Include_vars puts variable in ansible_facts ====
var.yml
1 ---
2 # Varfile to configure k8s installion using variables
3 #k8s_customize: # Customized k8s setup
4 network_plugin: "cilium"
5 ingress: "traefik"
6 dashboard: "dashboard"
7 apps:
8 - "mosquitto"
Using name, the name is also the variable containing file content. If not, file content are added directly to ansible_facts.
10 - include_vars: 11 file: "{{ playbook_dir }}/config.yml"
12 name: k8s_customize # create ansible_facts.k8s_customize
TASK [include_vars] ********************************************************************************************************************************** task path: /mnt/c/Users/Thuy Dang/Workspace/00_current/iiot/k8s-cluster-ansible/k8s-master-playbook.yml:10 ok: [master] => {
"ansible_facts": { "k8s_customize": {
"apps": "mosquitto",
"dashboard": "dashboard",
"ingress": "traefik",
"network_plugin": "cilium"
}
},
"ansible_included_var_files": [
"/mnt/c/Users/Thuy Dang/Workspace/00_current/iiot/k8s-cluster-ansible/config.yml" ],
"changed": false
}
==== - Inspect networks ====
* https://www.digitalocean.com/community/tutorials/how-to-inspect-kubernetes-networking
*