目录
- ansible 变量优先级示例
- 1.不是变量的变量
- 2.角色默认值
- 3.主机配置清单 或 动态脚本生成的 group vars
- 4.主机配置清单 group_vars/all
- 5.剧本 group_vars/all
- 6 主机配置清单 group_vars/*
- 7 剧本 group_vars/*
- 8.主机清单文件 or 动态生成主机清单文件的主机变量
- 9.主机清单文件 host_var/*
- 10.剧本 host_vars/*
- 11. host facts / cached set_facts
- 12. 剧本 vars
- 13. 剧本 vars_prompt
- 14. play vars_files
- 15 角色 变量
- 16 块 vars (only for tasks in block)
- 17. 任务 vars (only for the task)
- 18. include_vars
- 19. set_facts / registered vars
- 20.角色与 引入角色 参数
- 21.include params
- 22. 额外变量 (for example, -e "user=my_user")(always win precedence)
ansible 变量优先级示例
ansible多处都可设置变量,但是其中变量优先级不清楚,这里按官方示例文档基于最新2.17 ansible-core版本,编写验证编写验证变量优先级的示例
以下按从优先级低到高排序
1.不是变量的变量
command line values (for example, -u my_user, these are not variables)
这里使用变量ansible_user,它一般对应参数 -u 或 inventory文件的 ansible_user变量
这里说的最低的优先级是: 如果inventory配置了ansible_user 大于 -u 权重
ansible的非 -e 方式传递参数权重最低
priority01.yml
---
- hosts: ubuntu
tasks:
- name: Display the user running the task
debug:
msg: "The user is {{ ansible_user }}"
/etc/ansible/hosts
[ubuntu]
107.151.199.209 ansible_user=root
ansible-playbook -u rootb priority01.yml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [Display the user running the task] **********************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "The user is root"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.角色默认值
role defaults (as defined in Role directory structure)
先创建一个简单的role
#手动创建角色的基本目录结构
#tasks定义任务
#defaults: 角色默认变量
#files: 存放文件
#templates: 模板文件
#handlers: 触发器
#vars: 高于defaults的变量存储目录
#meta: 元数据信息
mkdir -p my_simple_role/{tasks,defaults,files,templates,handlers,vars,meta}
编辑task文件
my_simple_role/tasks/main.yml
- name: print vars
debug:
msg: "{{ role_default_vars }}"
编辑默认变量文件
my_simple_role/defaults/main.yml
role_default_vars: "role_default_vars_with_defaults"
编辑可用playbook文件
playbook.yaml
---
- hosts: ubuntu
roles:
- my_simple_role
3.主机配置清单 或 动态脚本生成的 group vars
inventory file or script group vars
这是指在 inventory 文件或动态 inventory 脚本中为特定组定义的变量
一般形式是
[groupname:vars]动态 inventory 脚本生成的变量暂时用不到
在inventory中也可以配置组变量
/etc/ansible/hosts
[ubuntu]
107.151.199.209 ansible_user=root
[ubuntu:vars]
role_default_vars="role_default_vars_with_inventory_group_vars"
ansible-playbook playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_inventory_group_vars"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.主机配置清单 group_vars/all
inventory group_vars/all
这里指的是 /etc/ansible/group_vars/all文件
/etc/ansible/group_vars/all
---
role_default_vars: role_default_vars_with_group_vars_all
ansible-playbook playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_group_vars_all"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.剧本 group_vars/all
这里指的playbook.yaml文件成的位置下的 group_vars/all文件变量
#创建目录
mkdir group_vars
group_vars/all
---
role_default_vars: role_default_vars_with_playbook_group_vars_all
ansible-playbook playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_playbook_group_vars_all"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6 主机配置清单 group_vars/*
inventory group_vars/*
示例
/etc/ansible/group_vars/ubuntu
指的是整个ubuntu的组变量
*表示group_vars目录下所有的文件,一般文件名称对应组名称
/etc/ansible/group_vars/ubuntu
---
tar: zar
ping: pong
foo_list:
- one
- two
- three
role_default_vars: role_default_vars_with_group_vars_ubuntu
ansible-playbook playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_group_vars_ubuntu"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7 剧本 group_vars/*
playbook group_vars/*
示例
group_vars/ubuntu
指的是当前playbook.yaml剧本目录下
*表示group_vars目录下所有的文件,一般文件名称对应组名称
---
role_default_vars: role_default_vars_with_playbook_group_vars_ubuntu
ansible-playbook playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_playbook_group_vars_ubuntu"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
8.主机清单文件 or 动态生成主机清单文件的主机变量
inventory file or script host vars
示例
[webservers]
webserver1 ansible_host=192.168.1.10 http_port=8080
/etc/ansible/hosts
[ubuntu]
107.151.199.209 ansible_user=root role_default_vars="role_default_vars_with_inventory_hosts_vars"
[ubuntu:vars]
role_default_vars="role_default_vars_with_inventory_group_vars"
ansible-playbook /root/handlers/priority/playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_inventory_hosts_vars"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
9.主机清单文件 host_var/*
示例
/etc/ansible/host_vars/107.151.199.209
指定单个主机变量信息
/etc/ansible/host_vars/107.151.199.209
---
foo: bar
baz: qux
role_default_vars: "role_default_vars_with_inventory_hosts_vars_hosts_vars"
ansible-playbook /root/handlers/priority/playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_inventory_hosts_vars_hosts_vars"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.剧本 host_vars/*
示例
host_vars/107.151.199.209
在playbook.yaml文件目录下的 host_vars/ip文件中指定单个主机变量信息
mkdir host_vars
host_vars/107.151.199.209
---
role_default_vars: "role_default_vars_with_playbook_hosts_vars"
ansible-playbook /root/handlers/priority/playbook.yaml
PLAY [ubuntu] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ****************************************************************************************************************************************************************************************************************************************
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_playbook_hosts_vars"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
11. host facts / cached set_facts
Host Facts 是 Ansible 通过setup自动收集的系统信息,用于描述目标主机的状态
Cached Set Facts 是您通过 set_fact 动态设置并选择缓存的自定义变量cached set_facts 指的是将获取到的变量缓存,可以与redis关联,或通过内置set_fact模块缓存变量,此配置较为复杂,不再详细描述
当前的文件里没有与facts相关的变量,facts变量有一个叫ansible_os_family
的,会打印出操作系统名称,那么我使用第10个优先级的变量配置下
host_vars/107.151.199.209
---
role_default_vars: "role_default_vars_with_playbook_hosts_vars"
ansible_os_family: "None"
修改role
my_simple_role/tasks/main.yml
- name: print vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
修改playbook.yaml
gather_facts: no
禁止获取系统信息
---
- hosts: ubuntu
gather_facts: no
roles:
- my_simple_role
此时打印的变量ansible_os_family
结果为None是符合设置的变量的
ansible-playbook playbook.yaml
PLAY [ubuntu] *****
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_playbook_hosts_vars",
"None"
]
}
PLAY RECAP *****
107.151.199.209 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
再次修改playbook.yaml
gather_facts: yes
获取系统信息
---
- hosts: ubuntu
gather_facts: yes
roles:
- my_simple_role
此时变量已被facts覆盖,获取到真实的系统名称
ansible-playbook playbook.yaml
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [107.151.199.209]
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_playbook_hosts_vars",
"Debian"
]
}
PLAY RECAP *****
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
12. 剧本 vars
play vars
就是在playbook 剧本中设置变量
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
roles:
- my_simple_role
ansible-playbook playbook.yaml
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [107.151.199.209]
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_playbook_vars",
"Debian"
]
}
PLAY RECAP *****
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
13. 剧本 vars_prompt
vars_prompt 模块
就是在playbook 剧本中手动设置变量
修改playbook文件,playbook.yaml
name: "role_default_vars"
指定变量名称
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_prompt:
- name: "role_default_vars"
prompt: "Please enter role_default_vars:"
private: no
roles:
- my_simple_role
手动输入变量值
ansible-playbook playbook.yaml
Please enter role_default_vars:: role_default_vars_with_prompt
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [107.151.199.209]
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_prompt",
"Debian"
]
}
PLAY RECAP *****
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
14. play vars_files
vars_files 模块
vars_files 加载变量文件,将变量定义存储在单独的 YAML 文件中,并在 Playbook 中引用这些文件,从而使变量管理更加模块化和易于维护
创建目录
mkdir vars
创建变量文件
vars/vars.yaml
---
role_default_vars: "role_default_vars_with_playbook_vars_file"
修改playbook.yaml文件
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
vars_prompt:
- name: "role_default_vars"
prompt: "Please enter role_default_vars:"
private: no
roles:
- my_simple_role
ansible-playbook playbook.yaml
Please enter role_default_vars:: role_default_vars_with_prompt
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_playbook_vars_file",
"Debian"
]
}
PLAY RECAP ******
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
15 角色 变量
role vars (as defined in Role directory structure)
在角色变量中定义文件,需要通过vars/main.yml文件
vars/main.yaml
---
role_default_vars: "role_default_vars_with_role_vars_main"
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
vars_prompt:
- name: "role_default_vars"
prompt: "Please enter role_default_vars:"
private: no
roles:
- my_simple_role
ansible-playbook playbook.yaml
Please enter role_default_vars:: role_default_vars_with_prompt
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_vars_main",
"Debian"
]
}
PLAY RECAP ******
107.151.199.209 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
16 块 vars (only for tasks in block)
block vars (only for tasks in block)
block
是 Ansible 中的一种结构化语法,允许您将一组任务组织在一起,并对整个块应用特定的处理,比如错误处理、条件判断或变量定义- block: - name: Print a message debug: msg: "The variable inside block is {{ block_var }}" - name: Print another message debug: msg: "This is another task using the block variable {{ block_var }}" vars: block_var: "This is a block-level variable"
block块下的变量 block_var只会作用于block块区域中
因vars_prompt需要反复输入变量,此时移除此代码,添加block代码
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
roles:
- my_simple_role
my_simple_role/tasks/main.yml
- name: print vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
- block:
- name: print block vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
vars:
role_default_vars: "role_default_vars_with_block_vars"
ansible-playbook playbook.yaml
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_vars_main",
"Debian"
]
}
TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_block_vars",
"Debian"
]
}
PLAY RECAP ******
107.151.199.209 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
17. 任务 vars (only for the task)
task vars (only for the task)
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
tasks:
- name: print task vars
debug:
msg: "{{ role_default_vars }}"
vars:
role_default_vars: "role_default_vars_with_tasks_vars"
roles:
- my_simple_role
ansible-playbook playbook.yaml
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_vars_main",
"Debian"
]
}
TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_block_vars",
"Debian"
]
}
TASK [print task vars] ******
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_tasks_vars"
}
PLAY RECAP ******
107.151.199.209 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
18. include_vars
include_vars
用于动态加载变量,这与前面 vars_files 有不同
特性 include_vars vars_files 功能 动态加载变量文件 静态加载变量文件 使用位置 在任务中使用,可以结合条件和循环 在 Playbook 顶层定义 加载时机 在任务执行时加载 在 Playbook 开始时加载 灵活性 高,可动态加载不同的变量文件 低,加载固定的变量文件 典型使用场景 根据条件或其他动态因素加载变量文件,可以与条件和循环结合使用 预先加载全局变量文件,适用于预先加载固定变量文件的场景
include_vars.yaml
---
role_default_vars: "role_default_vars_with_playbook_include_vars"
playbook.yaml
添加include_vars模块
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
tasks:
- name: include vars
include_vars: "include_vars.yaml"
- name: print task vars
debug:
msg: "{{ role_default_vars }}"
vars:
role_default_vars: "role_default_vars_with_tasks_vars"
roles:
- my_simple_role
ansible-playbook playbook.yaml
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_vars_main",
"Debian"
]
}
TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_block_vars",
"Debian"
]
}
TASK [include vars] ******
ok: [107.151.199.209]
TASK [print task vars] ******
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_playbook_include_vars"
}
PLAY RECAP ******
107.151.199.209 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
19. set_facts / registered vars
set_facts 是一个模块,可显示设置变量
register 只是用于承接任务的结果标准输出和错误输出
因为显示两者明显区别要配置facts_cache缓存及多playbook任务,较为复杂,因此暂时略过,后结再行构建示例
特性 set_facts register 创建方式 显式地创建或修改变量 捕获任务的输出结果 作用范围 可配置facts_cache开启缓存,实现多playbook全局有效,否则与register作用范围相同 仅对创建此变量的playbook有效 使用场景 动态设置主机变量,用于 playbook 内的变量共享 捕获 task 的执行结果,用于后续 tasks 的条件判断或信息展示
my_simple_role/tasks/main.yml
- name: print vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
- block:
- name: print block vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
vars:
role_default_vars: "role_default_vars_with_block_vars"
- name: set register vars
shell: "echo role_default_vars_with_register_vars"
register: role_default_vars
- name: print role_default_vars vars
debug:
msg:
- "{{ role_default_vars.stdout }}"
- name: set set_fact vars
set_fact:
role_default_vars: "role_default_vars_with_set_fact_vars"
- name: print role_default_vars vars
debug:
msg:
- "{{ role_default_vars }}"
ansible-playbook playbook.yaml
PLAY [ubuntu] *******
TASK [Gathering Facts] *******
ok: [107.151.199.209]
#....#略过
TASK [my_simple_role : set register vars] *******
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_register_vars"
]
}
TASK [my_simple_role : set set_fact vars] *******
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_set_fact_vars"
]
}
#....#略过
PLAY RECAP *******
107.151.199.209 : ok=9 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
20.角色与 引入角色 参数
role (and include_role) params
role引入角色的方式与-tasks类似,必须与hosts绑定,include_role可以在playbook的任意位置引入
这里说明下,playbook执行顺序,在同一文件中包含tasks与roles时,先执行roles,再执行tasks,tasks中的include_roles按tasks顺序正常执行,所以此playbook有两次引入了role,roles方式加载引入时,变量role_default_vars是有stdout属性的,而在include_roles引入时,因为变量被引入,导致变量role_default_vars是没有stdout属性的,因此配置了ignore_errors
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
tasks:
- name: include vars
include_vars: "include_vars.yaml"
- name: print task vars
debug:
msg: "{{ role_default_vars }}"
vars:
role_default_vars: "role_default_vars_with_tasks_vars"
- name: "include_role 加载方式"
include_role:
name: my_simple_role
vars:
role_default_vars: "role_default_vars_with_include_role_params_vars"
roles:
- name: "role 方式加载"
role: my_simple_role
vars:
role_default_vars: "role_default_vars_with_role_params_vars"
/root/handlers/priority/my_simple_role/tasks/main.yml
- name: print vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
- block:
- name: print block vars
debug:
msg:
- "{{ role_default_vars }}"
- "{{ ansible_os_family }}"
vars:
role_default_vars: "role_default_vars_with_block_vars"
- name: set register vars
shell: "echo role_default_vars_with_register_vars"
register: role_default_vars
- name: print role_default_vars vars
debug:
msg:
- "{{ role_default_vars.stdout }}"
ignore_errors: yes
- name: set set_fact vars
set_fact:
role_default_vars: "role_default_vars_with_set_fact_vars"
- name: print role_default_vars vars
debug:
msg:
- "{{ role_default_vars }}"
ansible-playbook playbook.yaml
PLAY [ubuntu] ******
TASK [Gathering Facts] ******
ok: [107.151.199.209]
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_block_vars",
"Debian"
]
}
TASK [my_simple_role : set register vars] ******
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_register_vars"
]
}
TASK [my_simple_role : set set_fact vars] ******
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_set_fact_vars"
]
}
TASK [include vars] ******
ok: [107.151.199.209]
TASK [print task vars] ******
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_set_fact_vars"
}
TASK [include_role 加载方式] ******
included: my_simple_role for 107.151.199.209
TASK [my_simple_role : print vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : print block vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : set register vars] ******
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] ******
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars vars\n ^ here\n"}
...ignoring
TASK [my_simple_role : set set_fact vars] ******
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] ******
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars"
]
}
PLAY RECAP ******
107.151.199.209 : ok=16 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
21.include params
include params
include
和include_tasks
可以使用 params 来传递变量- name: 包含另一个任务文件 include_tasks: other_tasks.yml vars: param1: "value1" param2: "value2"
include_params.yaml
---
- name: Display vars
debug:
msg: "The user is {{ role_default_vars }}"
playbook.yaml
---
- hosts: ubuntu
gather_facts: yes
vars:
role_default_vars: "role_default_vars_with_playbook_vars"
vars_files:
- "vars/vars.yaml"
tasks:
- name: include vars
include_vars: "include_vars.yaml"
- name: print task vars
debug:
msg: "{{ role_default_vars }}"
vars:
role_default_vars: "role_default_vars_with_tasks_vars"
- name: "include_role 加载方式"
include_role:
name: my_simple_role
vars:
role_default_vars: "role_default_vars_with_include_role_params_vars"
- name: print include_tasks_params_vars
include_tasks: "include_params.yaml"
vars:
role_default_vars: "role_default_vars_with_include_task_params_vars"
roles:
- name: "role 方式加载"
role: my_simple_role
vars:
role_default_vars: "role_default_vars_with_role_params_vars"
ansible-playbook playbook.yaml
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [107.151.199.209]
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_block_vars",
"Debian"
]
}
TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_register_vars"
]
}
TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_set_fact_vars"
]
}
TASK [include vars] *****
ok: [107.151.199.209]
TASK [print task vars] *****
ok: [107.151.199.209] => {
"msg": "role_default_vars_with_set_fact_vars"
}
TASK [include_role 加载方式] *****
included: my_simple_role for 107.151.199.209
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars",
"Debian"
]
}
TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars vars\n ^ here\n"}
...ignoring
TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
ok: [107.151.199.209] => {
"msg": [
"role_default_vars_with_include_role_params_vars"
]
}
TASK [print include_tasks_params_vars] *****
included: /root/handlers/priority/include_params.yaml for 107.151.199.209
TASK [Display vars] *****
ok: [107.151.199.209] => {
"msg": "The user is role_default_vars_with_include_task_params_vars"
}
PLAY RECAP *****
107.151.199.209 : ok=18 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
22. 额外变量 (for example, -e "user=my_user")(always win precedence)
extra vars (for example, -e "user=my_user")(always win precedence)
这属于开挂,像极了css的 !import 提升权重
ansible-playbook playbook.yaml -e "role_default_vars=123"
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [107.151.199.209]
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"123",
"Debian"
]
}
TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
"msg": [
"123",
"Debian"
]
}
TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars vars\n ^ here\n"}
...ignoring
TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
ok: [107.151.199.209] => {
"msg": [
"123"
]
}
TASK [include vars] *****
ok: [107.151.199.209]
TASK [print task vars] *****
ok: [107.151.199.209] => {
"msg": "123"
}
TASK [include_role 加载方式] *****
included: my_simple_role for 107.151.199.209
TASK [my_simple_role : print vars] *****
ok: [107.151.199.209] => {
"msg": [
"123",
"Debian"
]
}
TASK [my_simple_role : print block vars] *****
ok: [107.151.199.209] => {
"msg": [
"123",
"Debian"
]
}
TASK [my_simple_role : set register vars] *****
changed: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
fatal: [107.151.199.209]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'stdout'\n\nThe error appears to be in '/root/handlers/priority/my_simple_role/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: print role_default_vars vars\n ^ here\n"}
...ignoring
TASK [my_simple_role : set set_fact vars] *****
ok: [107.151.199.209]
TASK [my_simple_role : print role_default_vars vars] *****
ok: [107.151.199.209] => {
"msg": [
"123"
]
}
TASK [print include_tasks_params_vars] *****
included: /root/handlers/priority/include_params.yaml for 107.151.199.209
TASK [Display vars] *****
ok: [107.151.199.209] => {
"msg": "The user is 123"
}
PLAY RECAP *****
107.151.199.209 : ok=18 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=2
标签:107.151,优先级,vars,示例,default,ansible,role,199.209,ok
From: https://www.cnblogs.com/anyux/p/18365160