1 vars:
2 aa: value1
3 bb: value2
4 aa: value3
5 ...
1 {{变量名}}
2 {{ 变量名 }}
3 {{ 变量名 }}
4 {{ 变量名 }}
[root@pp demo1]# cat 1.yaml
---
- hosts: up
vars:
myname: tom1
myname: tom2
myname: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[root@pp demo1]#
[root@pp demo2]# ansible up -m ping
up | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@pp demo2]# ansible-playbook 1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印某个变量] ****************************************************************************
ok: [up] => {
"msg": "变量myname1的值是tom1"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
[root@pp demo2]# cat 1.yaml
---
- hosts: up
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 1.yaml
[WARNING]: While constructing a mapping from /root/demo2/1.yaml, line 4, column 5, found
a duplicate dict key (myname1). Using last defined value only.
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印某个变量] ****************************************************************************
ok: [up] => {
"msg": "变量myname1的值是tom3"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
[root@pp demo2]# cat vars.yaml
myv1: aaa
myv2: bbb
myv3: ccc
[root@pp demo2]#
[root@pp demo2]# cat 1.yaml
---
- hosts: up
vars_files:
- vars.ymal
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3
tasks:
- name: 打印某个变量
debug: msg='变量myname1的值是{{myname1}}'
- name: 打印变量myv1的值
debug: msg='变量myv1的值是{{myv1}}'
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 1.yaml
[WARNING]: While constructing a mapping from /root/demo2/1.yaml, line 6, column 5, found
a duplicate dict key (myname1). Using last defined value only.
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印某个变量] ****************************************************************************
ok: [up] => {
"msg": "变量myname1的值是tom3"
}
TASK [打印变量myv1的值] ************************************************************************
ok: [up] => {
"msg": "变量myv1的值是aaa"
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
31.3 字典变量
1 字典名:
2 var1: value2
3 var2: value2
4 ...
[root@pp demo2]# cat 2.yaml
---
- hosts: up
vars:
dict1:
myv1: aaa
myv2: bbb
myv3: ccc
doct2:
myv1: 111
myv2: 222
myv3: 333
tasks:
- name: 打印第一个变量
debug: msg="{{dict1.myv1}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印第一个变量] ***************************************************************************
ok: [up] => {
"msg": "aaa"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
1 employee:
2 uname: lisi
3 age: 22
4 sex: man
5
6 uname: wangwu
7 age: 24
8 sex: man
9
10 uname: xiaohua
11 age: 21
12 sex: wuman
1 employee:
2 ‐ uname: lisi
3 age: 22
4 sex: man
5
6 ‐ uname: wangwu
7 age: 24
8 sex: man
9
10 ‐ uname: xiaohua
11 age: 21
12 sex: wuman
1 uname: lisi
2 age: 22
3 sex: man
[root@pp demo2]# cat 3-list.yaml
---
- hosts: up
vars:
user:
- uname: tom
sex: mem
age: 19
- uname: bob
sex: men
age: 20
- uname: mary
sex: women
age: 22
tasks:
- name: 打印一个变量
debug: msg={{user[2]}}
[root@pp demo2]#
这里列表users定义了3个元素,现在要获取第3个元素用users[2]来表示,运行结果如下。
[root@pp demo2]# ansible-playbook 3-list.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印一个变量] ****************************************************************************
ok: [up] => {
"msg": {
"age": 22,
"sex": "women",
"uname": "mary"
}
}
PLAY RECAP *******************************************************************************
up
如果要获取第3个用户的用户名,修改3-list.yaml的内容如下。
[root@pp demo2]# cat 3-list.yaml
---
- hosts: up
vars:
user:
- uname: tom
sex: mem
age: 19
- uname: bob
sex: men
age: 20
- uname: mary
sex: women
age: 22
tasks:
- name: 打印一个变量
debug: msg={{user[2].uname}}
[root@pp demo2]#
运行此playbook,命令如下
[root@pp demo2]# ansible-playbook 3-list.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印一个变量] ****************************************************************************
ok: [up] => {
"msg": "mary"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
定义列表时,也可以直接写值不写变量,通过如下方式来定义。
1 listname:
2 ‐ var1
3 ‐ var2
4 ‐ var3
5 ...
这种定义变量的方式可以换成如下内容。
1 listname: [var1,var2,var3,...]
[root@pp demo2]# cat 4-list.yaml
---
- hosts: up
vars:
aa: 3
tasks:
- name: 3乘2的值
debug: msg={{aa*2}}
- name: 3的3次方
debug: msg="{{3**3}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 4-list.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [3乘2的值] *****************************************************************************
ok: [up] => {
"msg": "6"
}
TASK [3的3次方] *****************************************************************************
ok: [up] => {
"msg": "27"
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]# cat 5-regl.yaml
---
- hosts: up
tasks:
- name: 执行一个操作命令
shell: 'hostname'
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 5-regl.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [执行一个操作命令] **************************************************************************
changed: [up]
PLAY RECAP *******************************************************************************
up : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]# cat 5-regl.yaml
---
- hosts: up
tasks:
- name: 执行一个操作命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 5-regl.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [执行一个操作命令] **************************************************************************
changed: [up]
TASK [打印注册变量aa的值] ************************************************************************
ok: [up] => {
"msg": {
"changed": true,
"cmd": "hostname",
"delta": "0:00:00.003840",
"end": "2023-12-21 11:59:35.610753",
"failed": false,
"rc": 0,
"start": "2023-12-21 11:59:35.606913",
"stderr": "",
"stderr_lines": [],
"stdout": "up",
"stdout_lines": [
"up"
]
}
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored
[root@pp demo2]# cat 5-regl.yaml
---
- hosts: up
tasks:
- name: 执行一个操作命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa.stdout}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 5-regl.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [执行一个操作命令] **************************************************************************
changed: [up]
TASK [打印注册变量aa的值] ************************************************************************
ok: [up] => {
"msg": "up"
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
[root@pp demo2]# cat 6-fact.yaml
---
- hosts: up
vars:
list: ['aaa:']
tasks:
- name: 打印IP
debug: msg={{ansible_default_ipv4.address}}
- name: 打印主机名
debug: msg={{ansible_fqdn}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 6-fact.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印IP] ******************************************************************************
ok: [up] => {
"msg": "192.168.248.23"
}
TASK [打印主机名] *****************************************************************************
ok: [up] => {
"msg": "up"
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0