本章主要介绍playbook中的变量
[blab@node01 ~]$ mkdir demo2
[blab@node01 ~]$ cp ansible.cfg hosts demo2/
[blab@node01 ~]$ cd demo2/
[blab@node01 demo2]$ ls
ansible.cfg hosts
[blab@node01 demo2]$
通过vars来定义变量,vars和 tasks对齐。定义变量的格式如下。
1 vars :2 变量 1 : 值 13 变量 2 : 值 24 ...
1 vars :2 aa : value13 bb : value24 aa : value35 ...
1 {{ 变量名 }}2 {{ 变量名 }}3 {{ 变量名 }}4 {{ 变量名 }}
[blab@node01 demo2]$ cat a.yml
---
- hosts: node02
vars:
myname1: tom1
myname2: tom2
myname3: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook a.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印某个变量] ******************************************************************
ok: [node02] => {
"msg": "变量myname1的值是tom1"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat a.yml
---
- hosts: node02
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3 //增加内容
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
[blab@node01 demo2]
[blab@node01 demo2]$ ansible-playbook a.yml
[WARNING]: While constructing a mapping from /home/blab/demo2/a.yml, line 4,
column 4, found a duplicate dict key (myname1). Using last defined value only.
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印某个变量] ******************************************************************
ok: [node02] => {
"msg": "变量myname1的值是tom3"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat vars.yml
myv1: aaa
myv2: bbb
myv3: ccc
[blab@node01 demo2]$
[blab@node01 demo2]$ cat a.yml
---
- hosts: node02
vars_files: //增加变量
- vars.yml //变量嵌套文件
vars:
myname1: tom1
myname2: tom2
myname3: tom3
myname1: tom3
tasks:
- name: 打印某个变量
debug: msg="变量myname1的值是{{myname1}}"
- name: 打印变量myv1的值
debug: msg="变量myv1的值是{{myv1}}" //增加嵌套文件的值
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook a.yml
[WARNING]: While constructing a mapping from /home/blab/demo2/a.yml, line 6,
column 4, found a duplicate dict key (myname1). Using last defined value only.
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印某个变量] ******************************************************************
ok: [node02] => {
"msg": "变量myname1的值是tom3"
}
TASK [打印变量myv1的值] **************************************************************
ok: [node02] => {
"msg": "变量myv1的值是aaa"
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
1 字典名 :2 var1 : value23 var2 : value24 ...
[blab@node01 demo2]$ cat bb.yml
---
- hosts: node02
vars:
dict1:
myv1: aaa
myv2: bbb
myv3: ccc
dict2:
myv1: 111
myv2: 222
myv3: 333
tasks:
- name: 打印第一个变量
debug: msg="{{dict1.myv1}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook bb.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印第一个变量] *****************************************************************
ok: [node02] => {
"msg": "aaa"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
employee:
uname: lisi
age: 22
sex: man
uname: wangwu
age: 19
sex: man
uname: xiaohua
age: 20
sex: wuman
employee:
- uname: lisi
age: 22
sex: man
- uname: wangwu
age: 19
sex: man
- uname: xiaohua
age: 20
sex: wuman
uname: lisi
age: 22
sex: man
[blab@node01 demo2]$ cat cc.yml
---
- hosts: node02
vars:
users:
- uname: tom
sex: man
age: 19
- uname: bob
sex: man
agx: 20
- uname: mary
sex: women
age: 18
tasks:
- name: 打印一个变量
debug: msg={{users.2}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook cc.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印一个变量] ******************************************************************
ok: [node02] => {
"msg": {
"age": 18,
"sex": "women",
"uname": "mary"
}
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat cc.yml
---
- hosts: node02
vars:
users:
- uname: tom
sex: man
age: 19
- uname: bob
sex: man
agx: 20
- uname: mary
sex: women
age: 18
tasks:
- name: 打印一个变量
debug: msg={{users.2.uname}} //增加uname
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook cc.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印一个变量] ******************************************************************
ok: [node02] => {
"msg": "mary"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
1 listname :2 ‐ var13 ‐ var24 ‐ var35 ...
listname: [var1,var2,var3,...]
[blab@node01 demo2]$ cat dd.yml
---
- hosts: node02
vars:
aa: 3
tasks:
- name: 3*2的值
debug: msg={{aa*2}}
- name: 3的3次方
debug: msg={{3**3}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook dd.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [3*2的值] *******************************************************************
ok: [node02] => {
"msg": "6"
}
TASK [3的3次方] *******************************************************************
ok: [node02] => {
"msg": "27"
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat ee.yml
---
- hosts: node02
tasks:
- name: 执行一个操作系统命令
shell: 'hostname'
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook ee.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [执行一个操作系统命令] **************************************************************
changed: [node02]
PLAY RECAP *********************************************************************
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat ee.yml
---
- hosts: node02
tasks:
- name: 执行一个操作系统命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook ee.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [执行一个操作系统命令] **************************************************************
changed: [node02]
TASK [打印注册变量aa的值] **************************************************************
ok: [node02] => {
"msg": {
"changed": true,
"cmd": "hostname",
"delta": "0:00:00.002749",
"end": "2023-12-21 12:02:47.875633",
"failed": false,
"rc": 0,
"start": "2023-12-21 12:02:47.872884",
"stderr": "",
"stderr_lines": [],
"stdout": "node02",
"stdout_lines": [
"node02"
]
}
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat ee.yml
---
- hosts: node02
tasks:
- name: 执行一个操作系统命令
shell: 'hostname'
register: aa
- name: 打印注册变量aa的值
debug: msg={{aa.stdout}} //增加stdout
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook ee.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [执行一个操作系统命令] **************************************************************
changed: [node02]
TASK [打印注册变量aa的值] **************************************************************
ok: [node02] => {
"msg": "node02"
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat ff.yml
---
- hosts: node02
vars:
list1: ['aaa: ']
tasks:
- name: 打印IP
debug: msg={{ansible_default_ipv4.address}}
- name: 打印主机名
debug: msg={{ansible_fqdn}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook ff.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印IP] ********************************************************************
ok: [node02] => {
"msg": "192.168.182.193"
}
TASK [打印主机名] *******************************************************************
ok: [node02] => {
"msg": "node02"
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat gg.yml
---
- hosts: node02
tasks:
- name: xxx
debug: msg={{groups}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook gg.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [xxx] *********************************************************************
ok: [node02] => {
"msg": {
"all": [
"node02",
"node04",
"node03"
],
"db": [
"node02",
"node03"
],
"db1": [
"node02",
"node04"
],
"ungrouped": []
}
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat gg.yml
---
- hosts: node02
tasks:
- name: xxx
debug: msg={{groups.db}} //增加内容.db
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook gg.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [xxx] *********************************************************************
ok: [node02] => {
"msg": [
"node02",
"node03"
]
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat gg.yml
---
- hosts: node02
tasks:
- name: xxx
debug: msg={{groups['db']}}
[blab@node01 demo2]$
hostvars [ ' 主机名 ' ]. 键值
[blab@node01 demo2]$ cat hh-hostvars.yml
---
- hosts: node02
tasks:
- name: 打印node03的ip
debug: msg={{hostvars.node03.ansible_default_ipv4.address}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook hh-hostvars.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印node03的ip] *************************************************************
fatal: [node02]: 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/blab/demo2/hh-hostvars.yml': line 4, column 5, 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: 打印node03的ip\n ^ here\n"}
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat hh-hostvars.yml
---
- hosts: node03
- hosts: node02
tasks:
- name: 打印node03的ip
debug: msg={{hostvars.node03.ansible_default_ipv4.address}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook hh-hostvars.yml
PLAY [node03] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [打印node03的ip] *************************************************************
ok: [node02] => {
"msg": "192.168.182.210"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 9-inventory.yml
---
- hosts: db
tasks:
- name: 打印我在清单文件中的名称
debug: msg={{inventory_hostname}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 9-inventory.yml
PLAY [db] **********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
ok: [node02]
TASK [打印我在清单文件中的名称] ************************************************************
ok: [node02] => {
"msg": "node02"
}
ok: [node03] => {
"msg": "node03"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat hosts
node02
node03
[db]
node02
node03
[xx] //增加的主机组
node02
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 9-inventory.yml
---
- hosts: db
tasks:
- name: 打印我在清单文件中的名称
debug: msg={{inventory_hostname}}
when: inventory_hostname in groups ['xx'] //增加内容
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 9-inventory.yml
PLAY [db] **********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
ok: [node02]
TASK [打印我在清单文件中的名称] ************************************************************
ok: [node02] => {
"msg": "node02"
}
skipping: [node03]
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo2]$
{{ 变量名 | 函数 }}
[blab@node01 demo2]$ cat 10-vars1.yml
---
- hosts: node02
vars:
aa: tom
bb: BOB
tasks:
- name: xxx
debug: msg={{bb | lower}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [xxx] *********************************************************************
ok: [node02] => {
"msg": "bob"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars2.yml
---
- hosts: node02
tasks:
- name: 数学运算
debug: msg:{{3+'3'}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [数学运算] ********************************************************************
fatal: [node02]: FAILED! => {"msg": "Unexpected templating type error occurred on (msg:{{3+'3'}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars2.yml
---
- hosts: node02
tasks:
- name: 数学运算
debug: msg={{3+('3'|int)}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [数学运算] ********************************************************************
ok: [node02] => {
"msg": "6"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars2.yml
---
- hosts: node02
tasks:
- name: 数学运算
debug: msg={{3+('3'|float)}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [数学运算] ********************************************************************
ok: [node02] => {
"msg": "6.0"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars2.yml
---
- hosts: node02
tasks:
- name: 数学运算
debug: msg={{-3 | abs}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [数学运算] ********************************************************************
ok: [node02] => {
"msg": "3"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars3.yml
---
- hosts: node02
vars:
list1: [1,2,8,3,2]
tasks:
- name: 求列表长度
debug: msg="{{list1 | length}}"
- name: 求列表中的最大值
debug: msg="{{list1 | max}}"
- name: 求列表中的最小值
debug: msg="{{list1 | min}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars3.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [求列表长度] *******************************************************************
ok: [node02] => {
"msg": "5"
}
TASK [求列表中的最大值] ****************************************************************
ok: [node02] => {
"msg": "8"
}
TASK [求列表中的最小值] ****************************************************************
ok: [node02] => {
"msg": "1"
}
PLAY RECAP *********************************************************************
node02 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
如果某个变量没有被定义,那么可以通过default给它设置一个默认值,用法如下。
{{ var1 | default(value1) }}
[blab@node01 demo2]$ cat 10-vars4.yml
---
- hosts: node02
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')}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars4.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [aa的值] ********************************************************************
ok: [node02] => {
"msg": "11"
}
TASK [bb的值] ********************************************************************
ok: [node02] => {
"msg": ""
}
TASK [cc的值] ********************************************************************
ok: [node02] => {
"msg": "xxx"
}
PLAY RECAP *********************************************************************
node02 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars5.yml
---
- hosts: node02
tasks:
- name: 求和
debug: msg="{{3+3|string}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars5.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [求和] **********************************************************************
fatal: [node02]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{3+3|string}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
这个用法其实和前面的3+3'是类似的,单引号也可以转换成字符串。
[blab@node01 demo2]$ cat 10-vars6.yml
---
- hosts: node02
tasks:
- name: 字符串集
debug: msg="{{'aa' | capitalize}}"
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars6.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [字符串集] ********************************************************************
ok: [node02] => {
"msg": "Aa"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
可以看到,aa通过capitalize过滤器转换之后,首字符变成了大写。
hash ( ' 算法名 ' )
[blab@node01 demo2]$ cat 10-vars7.yml
---
- hosts: node02
vars:
password: aaa001
tasks:
- name: 用md5加密
debug: msg={{password | hash('md5')}}
- name: 用sha1加密
debug: msg={{password | hash('sha1')}}
- name: 用sha512加密
debug: msg={{password | hash('sha512')}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars7.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [用md5加密] ******************************************************************
ok: [node02] => {
"msg": "ba5cbfec1ff34168f1ab08e19fd397b0"
}
TASK [用sha1加密] *****************************************************************
ok: [node02] => {
"msg": "189d05ec2089c54128700410b76168bb0bea9be8"
}
TASK [用sha512加密] ***************************************************************
ok: [node02] => {
"msg": "47bb39c0066fad64c31187030979d822b984c6f5fd6acf5db401b914fe69d279c7229f33c7485cfa1c00de6fa0fea36ee87e6c39aa3ba0a29edadb070be71d72"
}
PLAY RECAP *********************************************************************
node02 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
password_hash ( ' 算法名 ' )
[blab@node01 demo2]$ cat 10-vars7.yml
---
- hosts: node02
vars:
password: aaa001
tasks:
- name: 用md5加密
debug: msg={{password | password_hash('md5')}} //修改内容
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars7.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [用md5加密] ******************************************************************
ok: [node02] => {
"msg": "$1$.JFajmN6$pg5Bkp9E.jqvN1abNghW1/"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible node02 -m shell -a "id bob"
node02 | FAILED | rc=1 >>
id: “bob”:无此用户non-zero return code
[blab@node01 demo2]$
[blab@node01 demo2]$ cat 10-vars8.yml
---
- hosts: node02
vars:
password: aaa001
tasks:
- name: 创建一个bob用户
user: user=bob comment="Im bob" groups=root password={{password | password_hash('sha512')}}
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible-playbook 10-vars8.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [创建一个bob用户] ***************************************************************
changed: [node02]
PLAY RECAP *********************************************************************
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo2]$
[blab@node01 demo2]$ ansible node02 -m shell -a "id bob"
node02 | CHANGED | rc=0 >>
uid=1003(bob) gid=1003(bob) 组=1003(bob),0(root)
[blab@node01 demo2]$