Table of Contents
Ansible guide
1. Best practices
1.1 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?
1.2 On the web
2. 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
3. Troubleshooting
3.1 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.
3.2 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 importtasks. Must use includetasks. 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
-
3.3 Variable in included task name
includetasks: “ansible_pkg_mgr/repos.yml” # variable in task name not resolved for importtasks. 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.
3.4 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
}