自动化运维-ansible中的循环应用
自动化运维-ansible中的循环与过滤器的应用
一、循环的使用示例
-
with_items
迭代列表[student@master ansible] vim a.yml # playbook内容如下 --- - name: test1hosts: node1tasks:- name: test11shell:cmd: echo "{{ item }}"with_items:- user1- user2- user3register: xx- name: test12debug:var: xx.results[0].stdout- name: test13debug:var: xx.results[1].stdout- name: test14debug:var: xx.results[2].stdout
-
with_dict
迭代字典[student@master ansible] vim b.yml # playbook内容如下 --- - name: test2hosts: node1tasks:- name: test21debug:msg: "{{item.key}}&{{item.value}}"with_dict:address: 1netmask: 2gateway: 3
-
with_fileglob
迭代文件[student@master ansible] vim c.yml # playbook内容如下 --- - name: test3hosts: node1tasks:- name: test31debug:msg: "{{item}}"with_fileglob:- /home/student/ansible/*.yml
-
with_lines
迭代行[student@master ansible] vim d.yml # playbook内容如下 --- - name: test4hosts: node1tasks:- name: test41debug:msg: "{{item}}"with_lines:- find /home/student/ansible -name "*.yml"
-
with_nested
嵌套迭代[student@master ansible] vim e.yml # playbook内容如下 --- - name: test5hosts: node1tasks:- name: test51debug:msg:"{{item[0]}}&{{item[1]}}"with_nested:- [a,b]- [1,2,3]
-
with_sequence
排序列start 是从什么开始 end 是结束 stride 是每隔多少
[student@master ansible] vim f.yml # playbook内容如下 --- - name: testhosts: node1tasks:- name: test61debug:msg: "{{item}}"with_sequence:start=5end=15stride=3
-
with_random_choice
随机获得列表中的一个值[student@master ansible] vim g.yml # playbook内容如下 --- - name: test7hosts: node1tasks:- name: test71debug:msg: "{{item}}"with_random_choice:- 1- 2- 3- a- b- c
二、过滤器
现在loop已经替代了with,更多的是loop配合过滤器进行使用
因为格式原因,在这里使用
I
代替 | 符号,使用时请更改为 |
-
常用字符串有关的过滤器
过滤器名 功能描述 示例用法 示例结果/说明 upper
将字符串转换成纯大写 {{ 'abc123ABC' I upper }}
‘ABC123ABC’ lower
将字符串转换成纯小写 {{ 'abc123ABC' I lower }}
‘abc123abc’ capitalize
将字符串首字母大写,之后的所有字母纯小写 {{ 'hello WORLD' I capitalize }}
‘Hello world’ trim
将字符串开头和结尾的空格去除 {{ ' abc ' I trim }}
‘abc’ length
/count
返回字符串长度 {{ 'hello' I length }}
5 first
返回字符串的第一个字符 {{ 'hello' I first }}
‘h’ last
返回字符串的最后一个字符 {{ 'hello' I last }}
‘o’ center
将字符串放在中间,并且设置指定宽度,字符串两边用空格补齐 {{ 'hi' I center(width=10) }}
’ hi ’ list
将字符串转换成列表,每个字符作为一个元素 {{ 'hello' I list }}
[‘h’,‘e’,‘l’,‘l’,‘o’] shuffle
将字符串转换成列表,每个字符作为一个元素,并且随机打乱顺序 {{ 'hello' I shuffle }}
随机顺序,如 [‘o’,‘l’,‘e’,‘h’,‘l’] -
和数字操作有关的过滤器
过滤器名 功能描述 示例用法 示例结果/说明 int
将对应的值转换成int类型 {{ '8' I int }}
8 int
将对应的值转换成int类型,如果无法转换,默认返回指定值 {{ 'a' I int(default=6) }}
6 float
将对应的值转换成浮点型 {{ '8.5' I float }}
8.5 float
当对应的值无法被转换成浮点型时,则返回指定值 {{ 'a' I float(8.88) }}
8.88 abs
获取对应数值的绝对值 {{ -5 I abs }}
5 round
四舍五入 {{ 12.5 I round }}
13 round
取指定小数位数 {{ 3.1415926 I round(5) }}
3.14159 random
从0到指定值中随机返回一个随机数 {{ 100 I random }}
0-100间的随机整数 random
从指定起始值到指定值中随机返回一个随机数 {{ 10 I random(start=5) }}
5-10间的随机整数 random
从指定起始值到指定值中随机返回一个随机数,按指定步长 {{ 15 I random(start=5,step=3) }}
5,8,11,14中的一个 random
从0到指定值中随机返回一个随机数,按指定步长 {{ 15 I random(step=5) }}
0,5,10,15中的一个 -
文件或目录类过滤器
过滤器名 功能描述 示例用法 示例结果/说明 hash
使用指定算法对字符串进行哈希 {{ '123456' I hash('sha1') }}
SHA1哈希字符串 hash
使用md5算法对字符串进行哈希 {{ '123456' I hash('md5') }}
MD5哈希字符串 checksum
获取字符串的校验和(与md5哈希值一致) {{ '123456' I checksum }}
MD5哈希字符串 password_hash
使用加密算法对字符串进行哈希,生成随机"盐" {{ '123456' I password_hash('sha256') }}
带随机盐的SHA256哈希 password_hash
使用加密算法对字符串进行哈希,使用指定的"盐" {{ '123456' I password_hash('sha256','mysalt') }}
带指定盐的SHA256哈希 password_hash
使用sha512算法对字符串进行哈希,生成随机"盐" {{ '123123' I password_hash('sha512') }}
带随机盐的SHA512哈希 password_hash
使用sha512算法对字符串进行哈希,使用指定的"盐" {{ '123123' I password_hash('sha512','ebzL.U5cjaHe55KK') }}
带指定盐的SHA512哈希
三、for 循环示例
-
要求:
从 http://ansible.example.com/materials/newhosts.j2 下载模板文件
完成该模板文件,用来生成新主机清单(主机的显示顺序没有要求),结构如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.10 node1.example.com node1
192.168.122.20 node2.example.com node2
192.168.122.30 node3.example.com node3
192.168.122.40 node4.example.com node4
192.168.122.50 node5.example.com node5
创建剧本/home/student/ansible/newhosts.yml,它将使用上述模板在 node1 主机上
生成文件/etc/newhosts。 -
步骤:
下载模板
[student@master ansible] ansible-playbook newhosts.yml
编辑 newhosts.j2 文件,写入 for 循环
[student@master ansible] vim newhosts.j2 # 编辑内容如下 {% for yy in groups.all %} {{ hostvars[yy].ansible_default_ipv4.address }} {{ hostvars[yy].ansible_fqdn }} [{ hostvars[yy].ansible_hostname }] {% endfor %}
编辑 playbook,newhosts.yml
[student@master ansible] vim newhosts.yml # playbook内容如下 --- - name: get facthosts: all - name: cp filehosts: node1tasks:- name: cp file1template:src: /home/student/ansible/newhosts.j2dest: /etc/newhosts
查看 node1 的 /etc/newhosts 文件
[student@master ansible] ansible node1 -m shell -a 'cat /etc/newhosts'