文章目录
- 循环
- with_items迭代列表
- with_dict迭代字典
- with_fileglob迭代文件
- with_lines迭代行
- Loop
- ansible判断
- 实例
循环
with_items迭代列表
Linux中依次需要安装多个软件包时,我们可以使用with_items迭代功能进行实现
例如:
安装httpd、samba、samba-client软件包
[student@ansible ansible]$ vim t.yml
---
- name: install packages
hosts: node1
tasks:
- name: yum_repo1
yum_repository:
file: local
name: BaseOS
description: BaseOS
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: yum_repo2
yum_repository:
file: local
name: AppStream
description: AppStream
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: mount cdrom
mount:
src: /dev/sr0
path: /mnt
fstype: iso9660
state: mounted
- name: install pks
yum:
name: "{{ item }}"
state: present
with_items:
- httpd
- samba
- samba-client
with_dict迭代字典
item.key对应着是字典的键,item.value对应着字典的值
[student@ansible ansible]$ vim e.yml
---
- name: test
hosts: node1
tasks:
- name: debug
debug:
msg: "{{ item.key }} & {{ item.value }}"
with_dict:
address: 1
netmask: 2
gateway: 3
with_fileglob迭代文件
拷贝多个文件到受控主机上时,可以使用
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
tasks:
- name: cp file
copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob:
- /tmp/*.sh
- /tmp/*.py
with_lines迭代行
with_lines可以将命令行的输出结果按行迭代
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
tasks:
- name: cp file
copy:
src: "{{ item }}"
dest: /tmp/
with_lines:
- find /etc/ansible -name "*.yml"
Loop
现在loop已经替代了with,更多的是loop配合过滤器进行使用
常用字符串有关的过滤器
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
testvar: "abc6484FSDA12516"
testvar1: " abc "
tasks:
- name: debug1
debug:
msg: "{{ testvar | upper }}" #upper将字符串转换成纯大写
- name: debug2
debug:
msg: "{{ testvar | lower }}" #lower将字符串转换成纯小写
- name: debug3
debug:
msg: "{{ testvar1 | trim }}" #trim将字符串的首尾空格去掉
- name: debug4
debug:
msg: "{{ testvar | length }}" #length求字符串的长度
使用加密算法对字符串进行hash加密
创建一个用户,并且设置密码为redhat,密码采用SHA512哈希格式
[student@ansible ansible]$ vim test.yml
---
- name: create user
hosts: node1
tasks:
- name: create ttt
user:
name: ttt
password: "{{ 'redhat' | password_hash('sha512') }}"
和数字操作有关的过滤器
[student@ansible ansible]$ vim test.yml
---
- name: this playbook
hosts: servera
vars:
testvar4: -1
tasks:
- name: conversion init
debug:
msg: "{{ 8+('8' | int) }}" #将对应的值转换成int类型
#ansible中,字符串和整形不能直接计算,比如{{ 8+'8' }}会报错
#所以,我们可以把一个值为数字的字符串转换成整形后再做计算
- name: not conversion init
debug:
msg: "{{ 'a' | int(default=6) }}" #将对应的值转换成int类型,如果无法转换,默认返回0
#使用int(default=6)或者int(6)时,如果无法转换则返回指定值6
- name: conversion float
debug:
msg: "{{ '8' | float }}" #将对应的值转换成浮点型,如果无法转换,默认返回'0.0'
- name: not conversion float
debug:
msg: "{{ 'a' | float(8.88) }}" #当对应的值无法被转换成浮点型时,则返回指定值’8.88‘
- name: absolute
debug:
msg: "{{ testvar4 | abs }}" #获取对应数值的绝对值
- name: rounding
debug:
msg: "{{ 12.5 | round }}" #四舍五入
- name: rouding behind 5
debug:
msg: "{{ 3.1415926 | round(5) }}" #取小数点后五位
- name: random
debug:
msg: "{{ 100 | random }}" #从0到100中随机返回一个随机数
- name: random2
debug:
msg: "{{ 10 | random(start=5) }}" #从5到10中随机返回一个随机数
- name: random step3
debug:
msg: "{{ 15 | random(start=5,step=3) }}" #从5到15中随机返回一个随机数,步长为3
#步长为3的意思是返回的随机数只有可能是5、8、11、14中的一个
- name: random multiple is 5
debug:
msg: "{{ 15 | random(step=5) }}" #从0到15中随机返回一个随机数,这个随机数是5的倍数
文件或目录类过滤器
[student@ansible ansible]$ vim test.yml
---
- name: 文件或目录类的过滤器
hosts: servera
tasks:
- name: 使用sha1算法对字符串进行哈希
debug:
msg: "{{ '123456' | hash('sha1') }}"
- name: 使用md5算法对字符串进行哈希
debug:
msg: "{{ '123456' | hash('md5') }}"
- name: 获取到字符串的校验和,与md5哈希值一致
debug:
msg: "{{ '123456' | checksum }}"
- name: 使用sha256算法对字符串进行哈希,哈希过程中会生成随机"盐",以便无法直接对比出原值
debug:
msg: "{{ '123456' | password_hash('sha256') }}"
- name: 使用sha256算法对字符串进行哈希,并使用指定的字符串作为"盐"
debug:
msg: "{{ '123456' | password_hash('sha256','mysalt') }}"
- name: 使用sha512算法对字符串进行哈希,哈希过程中会生成随机"盐",以便无法直接对比出原值
debug:
msg: "{{ '123123' | password_hash('sha512') }}"
- name: 使用sha512算法对字符串进行哈希,并使用指定的字符串作为"盐"
debug:
msg: "{{ '123123' | password_hash('sha512','ebzL.U5cjaHe55KK') }}"
ansible判断
判断运算符:“==”、“!=”、“>”、“<”、“>=”、“<=”、“and”、“or”、“not”、is、in
每次执行完一个任务,不管成功与失败,都会将执行的结果进行注册,可以使用这个注册的变量来进行when
判断变量:
Defined:判断变量是否已经定义,已定义则返真
Undefined:判断变量是否已经定义,未定义则返真
None:判断变量值是否为空,如果变量已经定义,但是变量值为空,则返真
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
aa: 11
cc:
tasks:
- name: create debug1
debug:
msg: a
when: aa is defined
- name: create debug2
debug:
msg: ab
when: bb is undefined
- name: create debug3
debug:
msg: abc
when: cc is none
判断执行结果:
Success/successed:通过任务的返回信息判断执行状态,任务执行成功返回真
Failure/failed:通过执行任务的返回信息判断执行状态,任务执行失败则返回真
Change/changed:通过任务的返回信息判断执行状态,任务返回状态为changed则返回真
Skip/skipped:通过任务的返回信息判断执行状态,当任务没有满足条件,而被跳过执行,则返回真。
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
aa: 11
tasks:
- name: shell
shell:
cmd: ls /mnt
when: aa == 11
register: dd
- name: create debug1 success
debug:
msg: chenyu success
when: dd is success
- name: create debug2 fail
debug:
msg: chenyu failed
when: dd is failed
- name: create debug3 change
debug:
msg: chenyu changed
when: dd is changed
- name: create debug4 skip
debug:
msg: chenyu skip
when: dd is skip
判断路径: 以下判断均对ansible主机中的路径,与目标主机无关
file:判断路径是否是一个文件
directory:判断路径是否是一个目录
link:判断路径是否是一个软连接
mount:判断路径是否是一个挂载点
exists:判断路径是否存在
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
a1: /test/file1
a2: /test/
a3: /test/softlinka
a4: /test/hardlinka
a5: /boot/
tasks:
- name: debug1
debug:
msg: this is file
when: a1 is file
- name: debug2
debug:
msg: "this is directory"
when: a2 is directory
- name: debug3
debug:
msg: "this is softlink"
when: a3 is link
- name: debug4
debug:
msg: "this is hardlink"
when: a4 is link
- name: debug5
debug:
msg: "this is mount directory"
when: a5 is mount
- name: debug6
debug:
msg: "this is exists"
when: a1 is exists
判断字符串:
lower:判断包含字母的字符串中的字母是否纯小写
upper:判断包含字母的字符串中的字母是否纯大写
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
a1: abc
a2: ABC
a3: a1b
tasks:
- name: debug1
debug:
msg: this string is all lower
when: a1 is lower
- name: debug2
debug:
msg: this string is all upper
when: a2 is upper
- name: debug3
debug:
msg: sss
when: a3 is lower
其他:
string:判断对象是否是一个字符串
number:判断对象是否一个数字
[student@ansible ansible]$ vim test.yml
---
- name: test
hosts: node1
vars:
a1: 1
a2: "1"
a3: a
tasks:
- name: debug1
debug:
msg: this is number
when: a1 is number
- name: debug2
debug:
msg: this is string
when: a2 is string
- name: debug3
debug:
msg: this is string
when: a3 is string
实例
block/rescue/always: 限制性block,如果执行失败,则执行rescue,无论是block还是rescue执行失败还是成功,在最后都执行always
给node1添加一块5G的硬盘
给node2添加一块2G的硬盘
node3不添加硬盘
写一个partition.yml的playbook,满足如下要求:
给所有的受控主机创建分区,创建2500M的分区,然后对分区进行格式化,格式化为xfs的文件系统。
如果不能满足创建2500M的分区,则显示错误消息为:disk size not enough,接着改为创建1000M。
如果没有/dev/sdb硬盘,则显示错误消息为:the device is not sdb
[student@ansible ansible]$ vim partition.yml
---
- name: parted test
hosts: all
tasks:
- name: create parted
block:
- name: create parted 2500M
parted:
device: /dev/sdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 2510MiB
state: present
rescue:
- name: fail
debug:
msg: disk size not enough
- name: create parted 1000M
parted:
device: /dev/sdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 1010MiB
state: present
always:
- name: format sdb1
filesystem:
dev: /dev/sdb1
fstype: xfs
when: "ansible_devices.sdb is defined"
- name: sdb not exists
debug:
msg: the device is not sdb
when: "ansible_devices.sdb is not defined"
[student@ansible ansible]$ ansible-playbook partition.yml
PLAY [parted test] **********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [node3]
ok: [node1]
ok: [node2]
TASK [create parted 2500M] **************************************************************************************************************************************************************************************
skipping: [node3]
fatal: [node2]: FAILED! => {"changed": false, "err": "Error: The location 2510MiB is outside of the device /dev/sdb.\n", "msg": "Error while running parted script: /sbin/parted -s -m -a optimal /dev/sdb -- unit KiB mklabel msdos mkpart primary 10MiB 2510MiB", "out": "", "rc": 1}
changed: [node1]
TASK [fail] *****************************************************************************************************************************************************************************************************
ok: [node2] => {
"msg": "disk size not enough"
}
TASK [create parted 1000M] **************************************************************************************************************************************************************************************
changed: [node2]
TASK [format sdb1] **********************************************************************************************************************************************************************************************
skipping: [node3]
changed: [node2]
changed: [node1]
TASK [sdb not exists] *******************************************************************************************************************************************************************************************
skipping: [node1]
skipping: [node2]
ok: [node3] => {
"msg": "the device is not sdb"
}
PLAY RECAP ******************************************************************************************************************************************************************************************************
node1 : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node2 : ok=4 changed=2 unreachable=0 failed=0 skipped=1 rescued=1 ignored=0
node3 : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
[student@ansible ansible]$ for i in node1 node2 node3; do ssh root@$i lsblk |grep sdb; done
sdb 8:16 0 5G 0 disk
└─sdb1 8:17 0 2.5G 0 part
sdb 8:16 0 2G 0 disk
└─sdb1 8:17 0 1000M 0 part