目录
1.手动定义变量
2.变量文件
3.字典变量
4.列表变量
5.数字变量的运算
6.注册变量
7.facts变量
8.内置变量groups
9. 内置变量hostvars
10.内置变量inventory hostname
11.变量的过滤器
11.1 数字类型
11.2 列表
11.3 设置变量默认值default
11.4 字符串相关
11.5 加密相关
[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]$
[bdqn@RHEL813 demo2]$ ansible-playbook 7-group.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [xxx] **************************************************************************************************************************
ok: [server2] => {
"msg": [
"server2",
"server3"
]
}
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]$
hostvars[' 主机名 '].键值
[bdqn@RHEL813 demo2]$ cat 8-hostvars.yaml
---
- hosts: server2
tasks:
- name: 打印server3 的IP
debug: msg={{hostvars['server3'].ansible_default_ipv4.address}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 8-hostvars.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印Server3的IP] *****************************************************************************************************************
fatal: [server2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'\n\nThe error appears to be in '/home/bdqn/demo2/8-hostvars.yaml': line 4, column 13, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: 打印Server3的IP\n ^ here\n"}
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$ cat 8-hostvars.yaml
---
- hosts: server3
- hosts: server2
tasks:
- name: 打印server3 的IP
debug: msg={{hostvars['server3'].ansible_default_ipv4.address}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 8-hostvars.yaml
PLAY [server3] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server3]
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [打印server3 的IP] ****************************************************************************************************************
ok: [server2] => {
"msg": "192.168.103.15"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
这个变量就是inventory _hostname,记录了每个主机在清单文件中的名称。
练习:写一个playbook,在主机组db上执行,命令如下。
[bdqn@RHEL813 demo2]$ cat 9-inventory1.yaml
---
- hosts: db
tasks:
- name: 打印 我在清单中的名称
debug: msg={{inventory_hostname}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 9-inventory1.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [打印 我在清单中的名称] ******************************************************************************************************************
ok: [server2] => {
"msg": "server2"
}
ok: [server3] => {
"msg": "server3"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat hosts
server2
server3
[db]
server2
server3
[xx]
server2
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 9-inventory1.yaml
---
- hosts: db
tasks:
- name: 打印 我在清单中的名称
debug: msg={{inventory_hostname}}
when: inventory_hostname in groups ['xx']
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 9-inventory1.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [打印 我在清单中的名称] ******************************************************************************************************************
ok: [server2] => {
"msg": "server2"
}
skipping: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
{{ 变量名 | 函数 }}
[bdqn@RHEL813 demo2]$ cat 10-vars1.yaml
---
- hosts: server2
vars:
aa: tom
bb: BOB
tasks:
- name: xxx
debug: msg={{bb | lower }}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [xxx] **************************************************************************************************************************
ok: [server2] => {
"msg": "bob"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars2.yaml
---
- hosts: server2
tasks:
- name: 数学运算
debug: msg={{3+'3'}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [数学运算] *************************************************************************************************************************
fatal: [server2]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{3+'3'}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars2.yaml
---
- hosts: server2
tasks:
- name: 数学运算
debug: msg={{3+('3'|int)}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [数学运算] *************************************************************************************************************************
ok: [server2] => {
"msg": "6"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars2.yaml
---
- hosts: server2
tasks:
- name: 数学运算
debug: msg={{3+('3'|float)}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [数学运算] *************************************************************************************************************************
ok: [server2] => {
"msg": "6.0"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$ cat 10-vars2.yaml
---
- hosts: server2
tasks:
- name: 数学运算
debug: msg={{-3| abs}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [数学运算] *************************************************************************************************************************
ok: [server2] => {
"msg": "3"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars3.yaml
---
- hosts: server2
vars:
list1: [1,2,8,3,2]
tasks:
- name: 求列表长度
debug: msg="{{list1 | length}}"
- name: 求列表中的最大值
debug: msg="{{list1 | max}}"
- name: 求列表的最小值
debug: msg="{{list1 | min}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars3.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [求列表长度] ************************************************************************************************************************
ok: [server2] => {
"msg": "5"
}
TASK [求列表中的最大值] *********************************************************************************************************************
ok: [server2] => {
"msg": "8"
}
TASK [求列表的最小值] **********************************************************************************************************************
ok: [server2] => {
"msg": "1"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
{{ var1 | default(value1) }}
[bdqn@RHEL813 demo2]$ cat 10-vars4.yaml
---
- hosts: server2
vars:
aa: 11
bb:
tasks:
- name: aa的值
debug: msg="{{aa | default('xxx')}}"
- name: bb的值
debug: msg="{{bb | default('xxx')}}"
- name: cc的值
debug: msg="{{cc | default('xxx')}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars4.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [aa的值] *************************************************************************************************************************
ok: [server2] => {
"msg": "11"
}
TASK [bb的值] *************************************************************************************************************************
ok: [server2] => {
"msg": ""
}
TASK [cc的值] *************************************************************************************************************************
ok: [server2] => {
"msg": "xxx"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars5.yaml
---
- hosts: server2
tasks:
- name: 求和
debug: msg="{{ 3+(3|string)}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars5.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [求和] ***************************************************************************************************************************
fatal: [server2]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ 3+(3|string)}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ cat 10-vars6.yaml
---
- hosts: server2
tasks:
- name: 字符串转换
debug: msg="{{'aa' | capitalize}}"
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars6.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [字符串转换] ************************************************************************************************************************
ok: [server2] => {
"msg": "Aa"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
hash('算法名')
[bdqn@RHEL813 demo2]$ cat 10-vars7.yaml
---
- hosts: server2
vars:
passa: haha001
tasks:
- name: 用md5加密
debug: msg={{passsa | hash('md5')}}
- name: 用sha1加密
debug: msg={{passa | hash('sha1')}}
- name: 用sha512加密
debug: msg={{passa | hash('sha512')}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars7.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [用md5加密] ***********************************************************************************************************************
ok: [server2] => {
"msg": "da48dd48779209245c671bf9fddfde23"
}
TASK [用sha1加密] **********************************************************************************************************************
ok: [server2] => {
"msg": "0d7c6d97655f38d7773c9a78a0861d533b1b32f1"
}
TASK [用sha512加密] ********************************************************************************************************************
ok: [server2] => {
"msg": "b37c491d6a36ef90c6c718855c06cc53053fe646fcae8889fafbad198c07a91fc283dd266af5dceb5378ff0a04cc1f9062fd4ea203c81dd2c9fbc58efa72a68b"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
注意hash过滤器中的md5或sha要用单引号引起来 。
password_hash('算法名')
在 Linux系统中,用户密码一般使用的是sha512加密算法,所以一般用password_hash('sha512')给用户的密码加密。
修改10-vars7.yaml的内容如下。
[bdqn@RHEL813 demo2]$ cat 10-vars7.yaml
---
- hosts: server2
vars:
passa: haha001
tasks:
- name: password_hash过滤器 sha512
debug: msg={{passa | password_hash('md5')}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars7.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [password_hash过滤器 sha512] ******************************************************************************************************
ok: [server2] => {
"msg": "$1$PUmqEWIA$Wz5/XL8cpRqy.Bj3x252C1"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo2]$
注意password_hash过滤器中的sha512要用单引号引起来。
练习2:在server2上创建用户bob,并设置密码为haha001。
先确定server2上不存在bob用户,命令如下。
[bdqn@RHEL813 demo2]$ ansible server2 -m shell -a "id bob"
server2 | FAILED | rc=1 >>
id: “bob”:无此用户non-zero return code
[bdqn@RHEL813 demo2]$
然后开始写playbook,命令如下。
[bdqn@RHEL813 demo2]$ cat 10-vars8.yaml
---
- hosts: server2
vars:
passa: haha001
tasks:
- name: 创建一个bob用户
user: user=bob comment="Im bob" groups=root password={{passa | password_hash('sha512')}}
[bdqn@RHEL813 demo2]$
[bdqn@RHEL813 demo2]$ ansible-playbook 10-vars8.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [创建一个用户bob] ********************************************************************************************************************
changed: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@RHEL8 ~]# su - tom
[tom@RHEL8 ~]$ su - bob
密码:
[bob@RHEL8 ~]$