任务控制 之 选择控制
前言
when相当于shell脚本里的if 判断,when语句就是用来实现这个功能的,它是一个jinja2的语法,但是不需要双大括号
很多任务只有在特定条件下才能执行, 这就是 when 语句发挥作用的地方。 比如我们只需要在数据库服务器上安装 MySQL 软件, 根据系统种类来确定是使用 apt 模块还是yum 模块进行软件包管理
提示:本篇文章所使用的环境为centos-8.2基于ansible-2.8.0 搭建
具体环境搭建,请参考:ansible-2.8.0 搭建链接
一、when 条件判断
条件涉及到的运算处理
简单循环–打印指定节点主机的主机名
[root@Ansible-Server ansible]# vim when1.yml
---
- hosts: all
tasks:
- name: show hosts name
debug:
var: inventory_hostname
when: inventory_hostname == 'node02'
[root@Ansible-Server ansible]# ansible-playbook when1.yml --syntax-check
playbook: when1.yml
[root@Ansible-Server ansible]# ansible-playbook when1.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
ok: [node02]
ok: [node03]
ok: [node04]
TASK [show hosts name] *********************************************************************************************
skipping: [node01]
ok: [node02] => {
"inventory_hostname": "node02"
}
skipping: [node03]
skipping: [node04]
PLAY RECAP *********************************************************************************************************
node01 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
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
node04 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
判断用户是否存在、不存在,则创建
[root@Ansible-Server ansible]# vim when2.yml
---
- hosts: localhost
tasks:
- name: test user tom is exist
shell: id tom
ignore_errors: yes
register: info
- name: show info
debug:
var: info
- name: create user
user:
name: tom
state: present
when: info.rc != 0
[root@Ansible-Server ansible]# ansible-playbook when2.yml --syntax-check
playbook: when2.yml
[root@Ansible-Server ansible]# ansible-playbook when2.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test user tom is exist] **************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "id tom", "delta": "0:00:00.005382", "end": "2020-10-15 00:43:56.390833", "msg": "non-zero return code", "rc": 1, "start": "2020-10-15 00:43:56.385451", "stderr": "id: ‘tom’: no such user", "stderr_lines": ["id: ‘tom’: no such user"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [show info] ***************************************************************************************************
ok: [localhost] => {
"info": {
"changed": true,
"cmd": "id tom",
"delta": "0:00:00.005382",
"end": "2020-10-15 00:43:56.390833",
"failed": true,
"msg": "non-zero return code",
"rc": 1,
"start": "2020-10-15 00:43:56.385451",
"stderr": "id: ‘tom’: no such user",
"stderr_lines": [
"id: ‘tom’: no such user"
],
"stdout": "",
"stdout_lines": []
}
}
TASK [create user] *************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@Ansible-Server ansible]# id tom
uid=1002(tom) gid=1004(tom) groups=1004(tom)
改写后,执行
[root@Ansible-Server ansible]# vim when3.yml
---
- hosts: localhost
tasks:
- name: test user tom is exist
shell: id tom
ignore_errors: yes
register: info
- name: show info
debug:
var: info
- name: create user
user:
name: tom
state: present
when: info is false
[root@Ansible-Server ansible]# ansible-playbook when3.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test user tom is exist] **************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "id tom", "delta": "0:00:00.005600", "end": "2020-10-15 00:44:56.356227", "msg": "non-zero return code", "rc": 1, "start": "2020-10-15 00:44:56.350627", "stderr": "id: ‘tom’: no such user", "stderr_lines": ["id: ‘tom’: no such user"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [show info] ***************************************************************************************************
ok: [localhost] => {
"info": {
"changed": true,
"cmd": "id tom",
"delta": "0:00:00.005600",
"end": "2020-10-15 00:44:56.356227",
"failed": true,
"msg": "non-zero return code",
"rc": 1,
"start": "2020-10-15 00:44:56.350627",
"stderr": "id: ‘tom’: no such user",
"stderr_lines": [
"id: ‘tom’: no such user"
],
"stdout": "",
"stdout_lines": []
}
}
TASK [create user] *************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@Ansible-Server ansible]# id tom
uid=1002(tom) gid=1004(tom) groups=1004(tom)
二、Jinja2 模板
为远程主机生成hosts文件
#编辑jinja2模板
[root@Ansible-Server ansible]# vim template/host.j2
{% for i in range(4,8) %}
192.168.5.{{ i }} node0{{ i }}
{% endfor %}
[root@Ansible-Server ansible]# vim create_user.yml
---
- hosts: all
tasks:
- name: create user_files
template:
src: template/host.j2
dest: /opt/hosts
#检测语法
[root@Ansible-Server ansible]# ansible-playbook create_user.yml --syntax-check
playbook: create_user.yml
#执行playbook
[root@Ansible-Server ansible]# ansible-playbook create_user.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
ok: [node02]
ok: [node04]
ok: [node03]
TASK [create user_files] *******************************************************************************************
changed: [node01]
changed: [node04]
changed: [node03]
changed: [node02]
PLAY RECAP *********************************************************************************************************
node01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node04 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#查看结果
[root@Ansible-Server ansible]# ansible all -m shell -a 'more /opt/hosts'
node03 | CHANGED | rc=0 >>
192.168.5.4 node04
192.168.5.5 node05
192.168.5.6 node06
192.168.5.7 node07
node01 | CHANGED | rc=0 >>
192.168.5.4 node04
192.168.5.5 node05
192.168.5.6 node06
192.168.5.7 node07
node04 | CHANGED | rc=0 >>
192.168.5.4 node04
192.168.5.5 node05
192.168.5.6 node06
192.168.5.7 node07
node02 | CHANGED | rc=0 >>
192.168.5.4 node04
192.168.5.5 node05
192.168.5.6 node06
192.168.5.7 node07
jinja2 if 单分支
#编辑jinja2 文件
[root@Ansible-Server ansible]# vim template/test1.j2
{% if a>b %}
{{ a }} > {{ b }}
{% endif %}
#编辑playbook
[root@Ansible-Server ansible]# vim jinjia_if1.yml
---
- hosts: localhost
vars:
a: 50
b: 20
tasks:
- name: test if
template:
src: template/test1.j2
dest: /opt/test1
#检测语法性
[root@Ansible-Server ansible]# ansible-playbook jinjia_if1.yml --syntax-check
playbook: jinjia_if1.yml
#执行playbook
[root@Ansible-Server ansible]# ansible-playbook jinjia_if1.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test if] *****************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试:
[root@Ansible-Server ansible]# cat /opt/test1
50 > 20
jinja2 if 双分支
#编辑jinjia2 文件
[root@Ansible-Server ansible]# vim template/test2.j2
{% if a>b %}
{{ a }} > {{ b }}
{% else %}
{{ a }} < {{ b }}
{% endif %}
#编辑playbook
[root@Ansible-Server ansible]# vim jinjia_if2.yml
---
- hosts: localhost
vars:
a: 50
b: 100
tasks:
- name: test if
template:
src: template/test2.j2
dest: /opt/test2
#检测语法
[root@Ansible-Server ansible]# ansible-playbook jinjia_if2.yml --syntax-check
playbook: jinjia_if2.yml
#执行playbook
[root@Ansible-Server ansible]# ansible-playbook jinjia_if2.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test if] *****************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试:
[root@Ansible-Server ansible]# cat /opt/test2
50 < 100
jinja2 if 多分支
#编辑jinjia2 文件
[root@Ansible-Server ansible]# vim template/test3.j2
{% if a>b %}
{{ a }} > {{ b }}
{% elif a<b %}
{{ a }} < {{ b }}
{% else %}
{{ a }} = {{ b }}
{% endif %}
#编辑playbook
[root@Ansible-Server ansible]# vim jinjia_if3.yml
---
- hosts: localhost
vars:
a: 50
b: 50
tasks:
- name: test if
template:
src: template/test3.j2
dest: /opt/test3
#检测语法
[root@Ansible-Server ansible]# ansible-playbook jinjia_if3.yml --syntax-check
playbook: jinjia_if3.yml
#执行playbook
[root@Ansible-Server ansible]# ansible-playbook jinjia_if3.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test if] *****************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试:
[root@Ansible-Server ansible]# cat /opt/test3
50 = 50
jinja2 循环–为远程主机生成hosts文件
#编辑jinja2文件
[root@Ansible-Server ansible]# vim template/hosts.j2
{% for host in groups['all']%}
{{ hostvars[host].ansible_facts.default_ipv4.address}} {{ hostvars[host].ansible_facts.fqdn}} {{ hostvars[host].ansible_facts.hostname}}
{% endfor %}
#编辑playbook文件
[root@Ansible-Server ansible]# vim hosts.yml
---
- hosts: all
tasks:
- name: create hosts file
template:
src: template/hosts.j2
dest: /opt/hosts
#检测语法
[root@Ansible-Server ansible]# ansible-playbook hosts.yml --syntax-check
playbook: hosts.yml
#执行playbook
[root@Ansible-Server ansible]# ansible-playbook hosts.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node03]
ok: [node02]
ok: [node01]
ok: [node04]
TASK [create hosts file] *******************************************************************************************
changed: [node03]
changed: [node04]
changed: [node01]
changed: [node02]
PLAY RECAP *********************************************************************************************************
node01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node04 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试
[root@Ansible-Server ansible]# ansible all -m shell -a 'more /opt/hosts'
node04 | CHANGED | rc=0 >>
192.168.5.4 node01 node01
192.168.5.5 node02 node02
192.168.5.6 node03 node03
192.168.5.7 node04 node04
node02 | CHANGED | rc=0 >>
192.168.5.4 node01 node01
192.168.5.5 node02 node02
192.168.5.6 node03 node03
192.168.5.7 node04 node04
node01 | CHANGED | rc=0 >>
192.168.5.4 node01 node01
192.168.5.5 node02 node02
192.168.5.6 node03 node03
192.168.5.7 node04 node04
node03 | CHANGED | rc=0 >>
192.168.5.4 node01 node01
192.168.5.5 node02 node02
192.168.5.6 node03 node03
192.168.5.7 node04 node04
三、ignore_errors条件判断
[root@master ~]# cat ignore1.yml
---
- hosts: all
vars:
user_name: tom
tasks:
- name: test user
shell: id {{ user_name }}
register: userinfo
ignore_errors: yes
- name: create user
user:
name: "{{ user_name }}"
state: present
when: userinfo.rc != 0
四、changed_when条件判断
1、changed_when关键词的作用是在条件成立时,将任务状态改为changed。
[root@master ~]# cat changed_when1.yml
-
- hosts: localhost
tasks:
- debug:
msg: "test message"
changed_when: 2 > 1
2、当changed_when状态被设置为false时,不管原任务状态为啥,最终都会被设置为ok状态
[root@master ~]# cat changed_when2.yml
---
- hosts: localhost
tasks:
- debug:
msg: "test message"
changed_when: False
五、force_handlers强制执行处理程序
给dev组部署 Apache-http 服务
#编辑playbook文件
[root@ansible-server ansible]# vim force_handers.yml
---
- hosts: dev
force_handlers: yes
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: index.html
copy:
content: "web test page.\n"
dest: /var/www/html/index.html
- name: set service port 8888
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8888"
notify: restart httpd
- name: set selinux for httpd
seport:
ports: 8888
proto: tcp
setype: http_port_t
state: present
- name: firewall
firewalld:
port: 8888/tcp
permanent: yes
state: enabled
immediate: yes
- name: test
shell: /bin/false
- name: start httpd
service:
name: httpd
enable: yes
state: started
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
#检查语法
[root@ansible-server ansible]# ansible-playbook force_handers.yml --syntax-check
playbook: force_handers.yml
#执行playbook
[root@ansible-server ansible]# ansible-playbook force_handers.yml
PLAY [dev] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
TASK [install httpd] ***********************************************************************************************
changed: [node01]
TASK [index.html] **************************************************************************************************
ok: [node01]
TASK [set service port 8888] ***************************************************************************************
changed: [node01]
TASK [set selinux for httpd] ***************************************************************************************
ok: [node01]
TASK [firewall] ****************************************************************************************************
ok: [node01]
TASK [test] ********************************************************************************************************
fatal: [node01]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.003446", "end": "2020-10-17 11:59:25.734063", "msg": "non-zero return code", "rc": 1, "start": "2020-10-17 11:59:25.730617", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
RUNNING HANDLER [restart httpd] ************************************************************************************
changed: [node01]
PLAY RECAP *********************************************************************************************************
node01 : ok=7 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
#测试:
[root@ansible-server ansible]# curl http://node01:8888
web test page.
六、failed_when条件判断
指定任务失败,执行失败,则输出结果
#编辑playbook
[root@ansible-server ansible]# vim failed_when1.yml
---
- hosts: all
tasks:
- name: 1.test sdb is exist
shell: ls /dev/sdb
register: msg
ignore_errors: yes
- name: 2.if sdb is not exist output error msg
debug:
msg: "sdb is not exist"
when: msg is failed
failed_when: msg is failed
#检查语法
[root@ansible-server ansible]# ansible-playbook failed_when1.yml --syntax-check
playbook: failed_when1.yml
#执行playbook
[root@ansible-server ansible]# ansible-playbook failed_when1.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node04]
ok: [node02]
ok: [node03]
ok: [node01]
TASK [1.test sdb is exist] *****************************************************************************************
fatal: [node02]: FAILED! => {"changed": true, "cmd": "ls /dev/sdb", "delta": "0:00:00.005611", "end": "2020-10-17 14:59:28.479618", "msg": "non-zero return code", "rc": 2, "start": "2020-10-17 14:59:28.474007", "stderr": "ls: cannot access '/dev/sdb': No such file or directory", "stderr_lines": ["ls: cannot access '/dev/sdb': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [node03]: FAILED! => {"changed": true, "cmd": "ls /dev/sdb", "delta": "0:00:00.005764", "end": "2020-10-17 14:59:28.479016", "msg": "non-zero return code", "rc": 2, "start": "2020-10-17 14:59:28.473252", "stderr": "ls: cannot access '/dev/sdb': No such file or directory", "stderr_lines": ["ls: cannot access '/dev/sdb': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [node01]: FAILED! => {"changed": true, "cmd": "ls /dev/sdb", "delta": "0:00:00.007071", "end": "2020-10-17 14:59:28.495881", "msg": "non-zero return code", "rc": 2, "start": "2020-10-17 14:59:28.488810", "stderr": "ls: cannot access '/dev/sdb': No such file or directory", "stderr_lines": ["ls: cannot access '/dev/sdb': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [node04]: FAILED! => {"changed": true, "cmd": "ls /dev/sdb", "delta": "0:00:00.006007", "end": "2020-10-17 14:59:28.484816", "msg": "non-zero return code", "rc": 2, "start": "2020-10-17 14:59:28.478809", "stderr": "ls: cannot access '/dev/sdb': No such file or directory", "stderr_lines": ["ls: cannot access '/dev/sdb': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [2.if sdb is not exist output error msg] **********************************************************************
fatal: [node01]: FAILED! => {
"msg": "sdb is not exist"
}
fatal: [node02]: FAILED! => {
"msg": "sdb is not exist"
}
fatal: [node03]: FAILED! => {
"msg": "sdb is not exist"
}
fatal: [node04]: FAILED! => {
"msg": "sdb is not exist"
}
PLAY RECAP *********************************************************************************************************
node01 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
node02 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
node03 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
node04 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
七、force_handlers强制执行处理程序
#编辑playbook
[root@ansible-server ansible]# vim test_rescue.yml
---
- hosts: localhost
tasks:
- name: test rescure
block:
- name: test file
shell: ls /etc/ooo
rescue:
- name: debug
debug:
msg: /etc/ooo is not exist
always:
- name: debug
debug:
msg: always do it
#检查语法
[root@ansible-server ansible]# ansible-playbook test_rescue.yml --syntax-check
playbook: test_rescue.yml
#执行playbook
[root@ansible-server ansible]# ansible-playbook test_rescue.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test file] ***************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "ls /etc/ooo", "delta": "0:00:00.003440", "end": "2020-10-17 16:00:33.988332", "msg": "non-zero return code", "rc": 2, "start": "2020-10-17 16:00:33.984892", "stderr": "ls: cannot access '/etc/ooo': No such file or directory", "stderr_lines": ["ls: cannot access '/etc/ooo': No such file or directory"], "stdout": "", "stdout_lines": []}
TASK [debug] *******************************************************************************************************
ok: [localhost] => {
"msg": "/etc/ooo is not exist"
}
TASK [debug] *******************************************************************************************************
ok: [localhost] => {
"msg": "always do it"
}
PLAY RECAP *********************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
修改后,再次执行
#编辑playbook
[root@ansible-server ansible]# vim test_rescue.yml
---
- hosts: localhost
tasks:
- name: test rescure
block:
- name: test file
shell: ls /etc/hosts
rescue:
- name: debug
debug:
msg: /etc/ooo is not exist
always:
- name: debug
debug:
msg: always do it
#执行playbook
[root@ansible-server ansible]# ansible-playbook test_rescue.yml
PLAY [localhost] ***************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
TASK [test file] ***************************************************************************************************
changed: [localhost]
TASK [debug] *******************************************************************************************************
ok: [localhost] => {
"msg": "always do it"
}
PLAY RECAP *********************************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
八、Ansible blocks
见下面实战演练 章节涉及
实战演练
示例 一
编写剧本partition.yml,创建一个分区,需求如下:
事先添加一块5G硬盘。
1> 在vdb(或sdb,根据自己机器环境)中,创建一个主分区,编号是1,大小1000MiB
2> 如果设备vdb不存在,则错误消息磁盘不存在应该显示
3> 格式化成xfs文件系统,挂在到/newpart
4> 如果无法创建请求的分区大小,则应使用错误消息“无法创建该大小的分区”,而应该使用大小为500Mib的创建
#编辑playbook
[root@ansible-server ansible]# vim partition.yml
---
- hosts: dev
tasks:
- name: 1.test sdb is exist
shell: lsblk /dev/sdb
ignore_errors: yes
register: info
- name: 2.sdb is not exist output info
debug:
msg: /dev/sdb is not exist
when: info.rc != 0
- name: 3.create Partition
block:
- name: 3.1 create Partition of 1000MiB
parted:
device: /dev/sdb
number: 1
state: present
part_end: 1000MiB
rescue:
- name: 3.2 show info
debug:
msg: not to create Partition
- name: 3.3 create Partition of 500MiB
parted:
device: /dev/sdb
number: 1
state: present
part_end: 500MiB
- name: 4.Format the file system
filesystem:
fstype: xfs
dev: /dev/sdb1
- name: 5.create mount files
file:
path: /newpart
state: directory
- name: 6.mount
mount:
src: /dev/sdb1
path: /newpart
fstype: xfs
state: mounted
#检查语法
[root@ansible-server ansible]# ansible-playbook partition.yml --syntax-check
playbook: partition.yml
#执行playbook
[root@ansible-server ansible]# ansible-playbook partition.yml
PLAY [dev] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
TASK [1.test sdb is exist] *****************************************************************************************
changed: [node01]
TASK [2.sdb is not exist output info] ******************************************************************************
skipping: [node01]
TASK [3.1 create Partition of 1000MiB] *****************************************************************************
changed: [node01]
TASK [4.Format the file system] ************************************************************************************
ok: [node01]
TASK [5.create mount files] ****************************************************************************************
changed: [node01]
TASK [6.mount] *****************************************************************************************************
changed: [node01]
PLAY RECAP *********************************************************************************************************
node01 : ok=6 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
#检测
[root@node01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 379M 0 379M 0% /dev
tmpfs 396M 0 396M 0% /dev/shm
tmpfs 396M 5.7M 391M 2% /run
tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.8G 16G 11% /
/dev/sda1 976M 130M 780M 15% /boot
tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 993M 40M 954M 4% /newpart
[root@node01 ~]# more /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Sep 13 06:41:19 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=3d37cd64-cfc3-4e9f-bd44-6512b3632a17 /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
/dev/sdb1 /newpart xfs defaults 0 0
示例 二
编写剧本lv.yml,需求如下:
1> 将上题中硬盘剩余空间创建一个2G主分区
2> 创建一个逻辑卷大小1500MiB,从research卷组中来
3> 如果research这个卷组不存在,则输出错误消息:不存在,然后创建卷组research(从第一步创建的分区中)
4> 如果这个逻辑卷的大小不能够创建,则输出错误信息
Could not create logical volume of that size
并使用800MiB大小来创建逻辑卷
5> 使用xfs格式化这个逻辑卷
6> 实现开机自动挂载到/data目录下,只有主机是dev才可以
#编辑playbook
[root@ansible-server ansible]# vim lv.yml
---
- hosts: dev
tasks:
- name: 1.create 2G Primary partition
parted:
device: /dev/sdb
number: 2
state: present
part_start: 1GiB
part_end: 3GiB
- name: 2.test Primary partition research is exist
block:
- name: test Primary partition research is exist
shell: vgs research
rescue:
- name: 3.1 output error info
debug:
msg: research is not exist
- name: 3.2 create vg
lvg:
vg: research
pvs: /dev/sdb2
- name: 4.create lvx
block:
- name: 4.1 create 1500 MiB
lvol:
vg: research
lv: lvx
size: 1500
rescue:
- name: 4.2 not enough space
debug:
msg: Could not create logical volume of that size
- name: 4.3 create 800 MiB lvx
lvol:
vg: reseach
lv: lvx
size: 800
- name: 5. xfs format
filesystem:
fstype: xfs
dev: /dev/research/lvx
- name: 6.create mount file
file:
path: /date
state: directory
- name: 7.mount
mount:
src: /dev/research/lvx
path: /date
fstype: xfs
state: mounted
#检查语法
[root@ansible-server ansible]# ansible-playbook lv.yml --syntax-check
playbook: lv.yml
#执行playbook
[root@ansible-server ansible]# ansible-playbook lv.yml --syntax-check
playbook: lv.yml
[root@ansible-server ansible]# ansible-playbook lv.yml
PLAY [dev] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
TASK [1.create 2G Primary partition] *******************************************************************************
changed: [node01]
TASK [test Primary partition research is exist] ********************************************************************
fatal: [node01]: FAILED! => {"changed": true, "cmd": "vgs research", "delta": "0:00:00.021587", "end": "2020-10-17 18:26:30.417403", "msg": "non-zero return code", "rc": 5, "start": "2020-10-17 18:26:30.395816", "stderr": " Volume group \"research\" not found.\n Cannot process volume group research", "stderr_lines": [" Volume group \"research\" not found.", " Cannot process volume group research"], "stdout": "", "stdout_lines": []}
TASK [3.1 output error info] ***************************************************************************************
ok: [node01] => {
"msg": "research is not exist"
}
TASK [3.2 create vg] ***********************************************************************************************
changed: [node01]
TASK [4.1 create 1500 MiB] *****************************************************************************************
[WARNING]: The value 1500 (type int) in a string field was converted to '1500' (type string). If this does not look
like what you expect, quote the entire value to ensure it does not change.
changed: [node01]
TASK [5. xfs format] ***********************************************************************************************
changed: [node01]
TASK [6.create mount file] *****************************************************************************************
ok: [node01]
TASK [7.mount] *****************************************************************************************************
changed: [node01]
PLAY RECAP *********************************************************************************************************
node01 : ok=8 changed=5 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
#测试:
[root@node01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- <17.00g
swap cl -wi-ao---- 2.00g
lvx research -wi-ao---- 1.46g
[root@node01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 379M 0 379M 0% /dev
tmpfs 396M 0 396M 0% /dev/shm
tmpfs 396M 5.7M 391M 2% /run
tmpfs 396M 0 396M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.8G 16G 11% /
/dev/sda1 976M 130M 780M 15% /boot
tmpfs 80M 0 80M 0% /run/user/0
/dev/sdb1 993M 40M 954M 4% /newpart
/dev/mapper/research-lvx 1.5G 43M 1.5G 3% /date
[root@node01 ~]# more /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Sep 13 06:41:19 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=3d37cd64-cfc3-4e9f-bd44-6512b3632a17 /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
/dev/sdb1 /newpart xfs defaults 0 0
/dev/research/lvx /date xfs defaults 0 0