目录
[bdqn@RHEL813 ~]$ mkdir demo2
[bdqn@RHEL813 ~]$ cp ansible.cfg hosts demo2/
[bdqn@RHEL813 ~]$ cd demo2
[bdqn@RHEL813 demo2]$ ls
ansible.cfg hosts
[bdqn@RHEL813 demo2]$
通过vars来定义变量,vars和 tasks对齐。定义变量的格式如下。
vars:
变量1: 值1
变量2: 值2
...
vars:
aa: value1
bb: value2
aa: value3
...
?这里aa重复定义了,所以aa的值最终是value3。
引用变量时用 {{变量名}} ,大括号内侧两边是否有空格是无所谓的,如下所示。?
{{变量名}}
{{ 变量名 }}
{{ 变量名 }}
{{ 变量名 }}
?练习1:写一个名称为1.yaml的playbook,里面定义以下3个变量。
[bdqn@RHEL813 demo2]$ cat 1.yaml
---
- hosts: server2
vars:
myname1: tom1
myname2: tom2
myname3: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[bdqn@RHEL813 demo2]$
?运行此playbook,命令如下。
[bdqn@RHEL813 demo2]$ ansible-playbook 1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印某个变量] ***********************************************************************************************************************
ok: [server2] => {
"msg": "变量myname1的值是tom1"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
定义变量时,同一个变量定义多次,后面定义的生效,修改1.yaml的内容如下。?
[bdqn@RHEL813 demo2]$ cat 1.yaml
---
- hosts: server2
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 1.yaml
[WARNING]: While constructing a mapping from /home/bdqn/demo2/1.yaml, line 4, column 11, found a duplicate dict key (myname1). Using
last defined value only.
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印某个变量] ***********************************************************************************************************************
ok: [server2] => {
"msg": "变量myname1的值是tom3"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
如果定义的变量太多,可以把变量拿出来单独放在一个文件中,然后在playbook中通过vars_files引用此变量文件,那么就可以直接使用此变量文件中的变量了,就像变量文件中的变量直接在YAML文件中定义似的,这样更方便管理。
?例如,创建文件vars.yaml,内容如下。
[bdqn@RHEL813 demo2]$ cat vars.yaml
myv1: aaa
myv2: bbb
myv3: ccc
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 1.yaml
---
- hosts: server2
vars_files:
- vars.yaml
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
- name: 打印变量myv1的值
debug: msg='变量myv1的值是{{myv1}}'
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印某个变量] ***********************************************************************************************************************
ok: [server2] => {
"msg": "变量myname1的值是tom1"
}
TASK [打印变量myv1的值] *******************************************************************************************************************
ok: [server2] => {
"msg": "变量myv1的值是aaa"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
字典是在vars下定义的,语法如下。
字典名:
var1: value2
var2: value2
...
注意在字典中定义这一个个变量时,变量前面是不加“-”的,且定义变量没有先后顺序。
[bdqn@RHEL813 demo2]$ cat 2.yaml
---
- hosts: server2
vars:
dict1:
myv1: aaa
myv2: bbb
myv3: ccc
dict2:
myv1: 111
myv2: 222
myv3: 333
tasks:
- name: 打印第一个变量
debug: msg="{{dict1.myv1}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印第一个变量] **********************************************************************************************************************
ok: [server2] => {
"msg": "aaa"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
employee:
uname: lisi
age: 22
sex: man
uname: wangwu
age: 24
sex: man
uname: xiaohua
age: 21
sex: wuman
employee:
‐ uname: lisi
age: 22
sex: man
‐ uname: wangwu
age: 24
sex: man
‐ uname: xiaohua
age: 21
sex: wuman
uname: lisi
age: 22
sex: man
列表和字典的不同如下。
练习1:判断下面的aa1和aa2是列表还是字典。aa1? ? ? ? ? ? ?aa2- xx: v1? ? ? ? xx: v1- yy: v2? ? ? ? yy: v2
这里左边的aa1是列表,每个元素中仅有一个变量,右边的aa2是字典。 ?
[bdqn@RHEL813 demo2]$ cat 3-list.yaml
---
- hosts: server2
vars:
users:
- uname: tom
sex: men
age: 19
- uname: bob
sex: men
age: 20
- uname: mary
sex: women
age: 22
tasks:
- name: 打印一个变量
debug: msg={{ users[2] }}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 3-list.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印一个变量] ***********************************************************************************************************************
ok: [server2] => {
"msg": {
"age": 22,
"sex": "women",
"uname": "mary"
}
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 3-list.yaml
---
- hosts: server2
vars:
users:
- uname: tom
sex: men
age: 19
- uname: bob
sex: men
age: 20
- uname: mary
sex: women
age: 22
tasks:
- name: 打印一个变量
debug: msg={{ users[2].uname }}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 3-list.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印一个变量] ***********************************************************************************************************************
ok: [server2] => {
"msg": "mary"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
listname:
‐ var1
‐ var2
‐ var3
...
?这种定义变量的方式可以换成如下内容。
listname: [var1,var2,var3,...]
[bdqn@RHEL813 demo2]$ cat 4-list.yaml
---
- hosts: server2
vars:
aa: 3
tasks:
- name: 3乘2的值
debug: msg={{aa*2}}
- name: 3的3次方
debug: msg="{{3**3}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 4-list.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [3乘2的值] ************************************************************************************************************************
ok: [server2] => {
"msg": "6"
}
TASK [3的3次方] ************************************************************************************************************************
ok: [server2] => {
"msg": "27"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 5-regl.yaml
---
- hosts: server2
tasks:
- name: 执行一个操作系统的命令
shell: 'hostname'
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 5-regl.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [执行一个操作系统的命令] ******************************************************************************************************************
changed: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 5-regl.yaml
---
- hosts: server2
tasks:
- name: 执行一个操作系统的命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa}}
[bdqn@RHEL813 demo2]$
运行此playbook,命令如下。
[bdqn@RHEL813 demo2]$ ansible-playbook 5-regl.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [执行一个操作系统的命令] ******************************************************************************************************************
changed: [server2]
TASK [打印注册变量aa的值] *******************************************************************************************************************
ok: [server2] => {
"msg": {
"changed": true,
"cmd": "hostname",
"delta": "0:00:00.002696",
"end": "2023-12-21 11:22:46.403782",
"failed": false,
"rc": 0,
"start": "2023-12-21 11:22:46.401086",
"stderr": "",
"stderr_lines": [],
"stdout": "RHEL8",
"stdout_lines": [
"RHEL8"
]
}
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 5-regl.yaml
---
- hosts: server2
tasks:
- name: 执行一个操作系统的命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa.stdout}}
[bdqn@RHEL813 demo2]$
运行此playbook,命令如下。
[bdqn@RHEL813 demo2]$ ansible-playbook 5-regl.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [执行一个操作系统的命令] ******************************************************************************************************************
changed: [server2]
TASK [打印注册变量aa的值] *******************************************************************************************************************
ok: [server2] => {
"msg": "RHEL8"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$ cat 6-fact.yaml
---
- hosts: server2
vars:
list1: ['aaa: ']
tasks:
- name: 打印IP
debug: msg={{ansible_default_ipv4.address}}
- name: 打印主机名
debug: msg={{ansible_fqdn}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 6-fact.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印IP] *************************************************************************************************************************
ok: [server2] => {
"msg": "192.168.103.14"
}
TASK [打印主机名] ************************************************************************************************************************
ok: [server2] => {
"msg": "RHEL8"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 7-group.yaml
---
- hosts: server2
tasks:
- name: xxx
debug: msg={{groups}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 7-group.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [xxx] **************************************************************************************************************************
ok: [server2] => {
"msg": {
"all": [
"server2",
"server3"
],
"db": [
"server2",
"server3"
],
"ungrouped": []
}
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 7-group.yaml
---
- hosts: server2
tasks:
- name: xxx
debug: msg={{groups['db']}}
[bdqn@RHEL813 demo2]$