昆明做网站多少钱,wordpress悬浮搜索,软件app开发制作多少钱,wordpress完整教程RHCE8 资料整理 第 31 章 变量的使用#xff08;二#xff09;31.8 内置变量 groups31.9 内置变量 hostvars31.10 内置变量 inventory_hostname31.11 变量过滤器31.11.1 数字类型31.11.2 列表31.11.3 设置变量默认值default31.11.4 字符串相关31.11.5 加密相关 第 31 章 变量… RHCE8 资料整理 第 31 章 变量的使用二31.8 内置变量 groups31.9 内置变量 hostvars31.10 内置变量 inventory_hostname31.11 变量过滤器31.11.1 数字类型31.11.2 列表31.11.3 设置变量默认值default31.11.4 字符串相关31.11.5 加密相关 第 31 章 变量的使用二
31.8 内置变量 groups
在ansible中除了用户手动定义一些变量外还有一些内置变量这些变量不需要用户定义可以直接使用。
groups用于列出清单文件中所有定义的主机组及里面的主机
[rootnode-137 ansible]# cat 7-groups.yaml
---
- hosts: db1tasks:- name: xxxdebug: msg{{groups}}
[rootnode-137 ansible]# ansible-playbook 7-groups.yamlPLAY [db1] ****************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]TASK [xxx] ****************************************************************************************************************************
ok: [node-138] {msg: {all: [node-138,node-140],db1: [node-138],db2: [node-140],db3: [node-138,node-140],ungrouped: []}
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0这里显示了清单文件中所有的主机组及里面的主机信息
如果仅想列出某个主机组可以通过groups[主机组名]或groups.主机组名来表示
[rootnode-137 ansible]# cat 7-groups.yaml
---
- hosts: db1tasks:- name: xxxdebug: msg{{groups.db1}}- name: yyydebug: msg{{groups[db2]}}
[rootnode-137 ansible]# ansible-playbook 7-groups.yamlPLAY [db1] ****************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]TASK [xxx] ****************************************************************************************************************************
ok: [node-138] {msg: [node-138]
}TASK [yyy] ****************************************************************************************************************************
ok: [node-138] {msg: [node-140]
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored0注意groups[db2]中的引号不能省略单双引号均可 31.9 内置变量 hostvars
hostvars用来显示指定主机的fact变量用法如下
hostvars[主机名].键值此变量一般用于当某个play的hosts中只写了A主机组但同时想在此play中显示B主机组中的信息此时可以选择此变量 这里只能指定主机来获取fact变量不能指定主机组 [rootnode-137 ansible]# cat 8-hostvars.yaml
---
- hosts: db2gather_facts: true- hosts: db1gather_facts: falsetasks:- debug: msg{{hostvars[db2].ansible_default_ipv4.address}} #注意此处
[rootnode-137 ansible]# ansible-playbook 8-hostvars.yamlPLAY [db2] ****************************************************************************************************************************
...
ok: [node-140]PLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
fatal: [node-138]: FAILED! {msg: The task includes an option with an undefined variable. The error was: \hostvars[db2]\ is undefined\n\nThe error appears to be in /opt/ansible/8-hostvars.yaml: line 8, 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 - debug: msg{{hostvars[db2].ansible_default_ipv4.address}}\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \{{ foo }}\\n}PLAY RECAP ****************************************************************************************************************************
node-138 : ok0 changed0 unreachable0 failed1 skipped0 rescued0 ignored0
node-140 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored0[rootnode-137 ansible]# cat 8-hostvars.yaml
---
- hosts: db2gather_facts: true- hosts: db1gather_facts: falsetasks:- debug: msg{{hostvars[node-140].ansible_default_ipv4.address}} #注意此处
[rootnode-137 ansible]# ansible-playbook 8-hostvars.yamlPLAY [db2] ****************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************
...
ok: [node-140]PLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 192.168.81.140
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored0
node-140 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored031.10 内置变量 inventory_hostname
这个变量记录了每个主机在清单文件中的名称
[rootnode-137 ansible]# cat hosts
node-138
node-140[db1]
node-138[db2]
node-140[db3:children]
db1
db2
[rootnode-137 ansible]# cat 9-inventory.yaml
---
- hosts: db3gather_facts: falsetasks:- debug: msg{{inventory_hostname}}
[rootnode-137 ansible]# ansible-playbook 9-inventory.yamlPLAY [db3] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: node-138
}
ok: [node-140] {msg: node-140
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored0
node-140 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored031.11 变量过滤器
所谓变量的过滤器实际上就是对变量的值进行一些操作例如进行类型转化截取加密等操作格式如下
{{变量|函数}}把大写字符转换成小写字符如
[rootnode-137 ansible]# cat 10-vars.yaml
---
- hosts: db1gather_facts: falsevars:aa: aabb: BBtasks:- debug: msg{{bb|lower}}- debug: msg{{aa|upper}}
[rootnode-137 ansible]# ansible-playbook 10-vars.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: bb
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: AA
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0常见的过滤器如下
31.11.1 数字类型
整型int可以把字符串转换成整型 浮点型float可以把字符串转换成小数类型 绝对值abs可以把负数转换成正数
[rootnode-137 ansible]# cat 10-vars.yaml
---
- hosts: db1gather_facts: falsetasks:- debug: msg{{3(3|int)}}- debug: msg{{3(3|float)}}- debug: msg{{-3|abs}}
[rootnode-137 ansible]# ansible-playbook 10-vars.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 6
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 6.0
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 3
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored031.11.2 列表
列表的过滤器可以求出列表的长度最大值最小值 length用于求列表的长度 max用于求列表中的最大值 min用于求列表的最小值 sort排序 sum求和 shuffle打乱顺序显示
[rootnode-137 ansible]# cat 10-vars1.yaml
---
- hosts: db1gather_facts: falsevars:list1: [1,3,5,7,4,6,2,ad,3.14,|]tasks:- debug: msglist1:{{list1}}- debug: msglength:{{list1|length}}- debug: msgmax:{{list1|max}}- debug: msgmin:{{list1|min}}- debug: msgsort:{{list1|sort}}- debug: msgsum:{{list1|sum}}ignore_errors: true- debug: msgshuffle:{{list1|shuffle}}
[rootnode-137 ansible]# ansible-playbook 10-vars1.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: list1:[1, 3, u5, 7, 4, 6, 2, uad, 3.14, u|]
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: length:10
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: max:|
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: min:1
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: sort:[1, 2, 3, 3.14, 4, 6, 7, u5, uad, u|]
}TASK [debug] **************************************************************************************************************************
fatal: [node-138]: FAILED! {msg: Unexpected templating type error occurred on (sum:{{list1|sum}}): unsupported operand type(s) for : int and AnsibleUnicode}
...ignoringTASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: shuffle:[3.14, u5, 6, 2, 7, 4, 3, u|, 1, uad]
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok7 changed0 unreachable0 failed0 skipped0 rescued0 ignored1可以看到列表过滤器按照ASCII码进行计算并且只计算首位 31.11.3 设置变量默认值default
如果某个变量没有被定义那么可以通过default给它设置一个默认值用法
{{ var1 | default(value1) }}如果变量var1已经被定义了否则会被赋值为value1
[rootnode-137 ansible]# cat 10-vars2.yaml
---
- hosts: db1gather_facts: falsevars:aa: 100bb: 200tasks:- debug: msg{{aa|default(111)}}- debug: msg{{bb|default(222)}}- debug: msg{{cc|default(333)}}[rootnode-137 ansible]# ansible-playbook 10-vars2.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 100
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 200
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 333
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored031.11.4 字符串相关
string能把其他数据类型转换成字符串类型 capitalize用于把字符串首字符转换成大写 upper把小写字符串转换成大写 lower把大写字符串转换成小写
[rootnode-137 ansible]# cat 10-vars3.yaml
---
- hosts: db1gather_facts: falsevars:aa: abcdefgbb: ABCDEFGcc: 123456tasks:- debug: msg{{aa|capitalize}}- debug: msg{{bb|lower}}- debug: msg{{aa|upper}}- debug: msg{{cc|string}}- debug: msg{{123456}}- debug: msg{{123456}}- debug: msg{{123456}}- debug: msg{{123456}}
[rootnode-137 ansible]# ansible-playbook 10-vars3.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: Abcdefg
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: abcdefg
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: ABCDEFG
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 123456
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 123456
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 123456
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 123456
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 123456
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok8 changed0 unreachable0 failed0 skipped0 rescued0 ignored0关注前几个函数即可 31.11.5 加密相关
有时候需要对字符串进行加密操作例如创建用户时给用户设定密码就要用密文形式
求哈希值hash算法md5或sha1等
[rootnode-137 ansible]# cat 10-vars4.yaml
---
- hosts: db1gather_facts: falsevars:passwd: haha01tasks:- debug: msg{{passwd|hash(md5)}}- debug: msg{{passwd|hash(sha1)}}- debug: msg{{passwd|hash(sha512)}}
[rootnode-137 ansible]# ansible-playbook 10-vars4.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 56433d003c7aae5f9d33b3a964751b94
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: 56bed31bd19bc363ce992aa45eb9808e844b0b52
}TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: e5eb574e00afc458cf074a665e0a59dee1c0f9faf64d39417a5e5f2a193987ca4d39fa30ce59d60f984aa683adf5087415b5ce3ce03aed40028b25c88b53c3c1
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored0hash过滤器中的算法md5或sha1等需要用单引号引起来 除了使用hash作为过滤器加密外还可以使用password_hash作为过滤器用法
password_hash(算法名)[rootnode-137 ansible]# cat 10-vars4.yaml
---
- hosts: db1gather_facts: falsevars:passwd: haha01tasks:- debug: msg{{passwd|password_hash(sha512)}}
[rootnode-137 ansible]# ansible-playbook 10-vars4.yamlPLAY [db1] ****************************************************************************************************************************TASK [debug] **************************************************************************************************************************
ok: [node-138] {msg: $6$r8h.egWSvkbXDLeo$ChfIk1UJOU3.8XBQhbOZjTvoOF.FsFZ1KM7/xx2Kpwnrh0weePg95qmoikx1HKfN1qzZ/ikdtYgb1oD/nPrb31
}PLAY RECAP ****************************************************************************************************************************
node-138 : ok1 changed0 unreachable0 failed0 skipped0 rescued0 ignored0password_hash过滤器中的算法md5或sha1等同样需要用单引号引起来