2 types of variables:
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
When using dynamic includes, it is important to keep these limitations in mind:
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
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.
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
}