文章目录
ansible变量
定义变量规则:由字母/数字/下划线组成,变量需要以字母开头,ansible内置的关键字不能作为变量名。
ansible中,可以将变量简化为三个范围 Global范围(高):从命令行和ansible配置设置的变量 play范围(中):在play和相关结构中设置的变量 Host范围(低):inventory、facts或register的变量,在主机组和个别主机上设置的变量 三个范围的变量优先级依次降低,如果变量重复定义,则以优先级高的为准
注册和定义变量的各种方式
ansible中定义变量的方式有很多种,大致有:
(1) 将模块的执⾏结果注册为变量;
(2) 直接定义字典类型的变量;
(3) role中⽂件内定义变量;
(4) 命令⾏传递变量;
(5) 借助with_items迭代将多个task的结果赋值给⼀个变量;
(6) inventory中的主机或主机组变量;
(7) 内置变量。
vars定义变量
[root@ansible]# su - student
[student@ansible]$ cd ansible/
[student@ansible ansible]$ pwd
/home/student/ansible
[student@ansible ansible]$ vim sb.yml
---
- name: vars变量
hosts: dev
vars:
- aa: 11
- bb: 22
- cc:
a1: c31
a2: c32
tasks:
- name: create debug
debug:
msg: "{
{ aa }}"
- name: create debug2
debug:
msg: "{
{ bb }}"
- name: create debug3
debug:
msg: "{
{ cc.a1 }}"
- name: create debug4
debug:
msg: "{
{ cc.a2 }}"
[student@ansible ansible]$ ansible-playbook sb.yml -C
PLAY [vars变量] **********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [create debug] ************************************************************
ok: [node1] => {
"msg": 11
}
TASK [create debug2] ***********************************************************
ok: [node1] => {
"msg": 22
}
TASK [create debug] ************************************************************
ok: [node1] => {
"msg": "c31"
}
TASK [create debug4] ***********************************************************
ok: [node1] => {
"msg": "c32"
}
PLAY RECAP *********************************************************************
node1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@ansible ansible]$
Vars_files定义变量
[student@ansible ansible]$ vim var.yml
aa: 11
bb: 22
cc:
a1: c31
a2: c32
[student@ansible ansible]$ vim sb.yml
---
- name: vars_files变量
hosts: dev
vars_files: /home/student/ansible/var.yml
tasks:
- name: create debug
debug:
msg: "{
{ aa }}"
- name: create debug2
debug:
msg: "{
{ bb }}"
- name: create debug3
debug:
msg: "{
{ cc.a1 }}"
- name: create debug4
debug:
msg: "{
{ cc.a2 }}"
[student@ansible ansible]$ ansible-playbook sb.yml -C
PLAY [vars_files变量] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [create debug] ************************************************************
ok: [node1] => {
"msg": 11
}
TASK [create debug2] ***********************************************************
ok: [node1] => {
"msg": 22
}
TASK [create debug3] ***********************************************************
ok: [node1] => {
"msg": "c31"
}
TASK [create debug4] ***********************************************************
ok: [node1] => {
"msg": "c32"
}
PLAY RECAP *********************************************************************
node1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@ansible ansible]$ ls
ansible.cfg inventory sb.yml
httpd.yml roles var.yml
register注册变量
使⽤register选项,可以将当前task的输出结果赋值给⼀个变量。
[student@ansible ansible]$ vim sb.yml
---
- name: register注册变量
hosts: dev
tasks:
- name: shell
shell: "cat /tmp/zz"
register: zz
- name: 创建debug
debug:
var: zz
[student@ansible ansible]$ ansible-playbook sb.yml
PLAY [register注册变量] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [shell] *******************************************************************
changed: [node1]
TASK [创建debug] *****************************************************************
ok: [node1] => {
"zz": {
"changed": true,
"cmd": "cat /tmp/zz",
"delta": "0:00:00.003963",
"end": "2022-10-25 23:21:23.533205",
"failed": false,
"rc": 0,
"start": "2022-10-25 23:21:23.529242",
"stderr": "",
"stderr_lines": [],
"stdout": "hello zz",
"stdout_lines": [
"hello zz"
]
}
}
PLAY RECAP *********************************************************************
node1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@ansible ansible]$ vim sb.yml
---
- name: register注册变量
hosts: dev
tasks:
- name: shell
shell: "cat /tmp/zz"
register: zz
- name: 创建debug
debug:
msg: "{
{ zz.rc }}"
[student@ansible ansible]$ ansible-playbook sb.yml
PLAY [register注册变量] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [shell] *******************************************************************
changed: [node1]
TASK [创建debug] *****************************************************************
ok: [node1] => {
"msg": "0"
}
PLAY RECAP *********************************************************************
node1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
set_fact定义变量
set_fact和register的功能很相似,也是将值赋值给变量。它更像shell中变量的赋值⽅式,可以将某个变量的值赋值给另⼀个变量,也可以将字符串赋值给变量
通过ansible node1 -m setup 可以查询node1主机所有的事实变量