0%

创建 Web 内容目录

按照下方所述,创建一个名为 /home/greg/ansible/webcontent.yml 的 playbook:

该 playbook 在 dev 主机组中的受管节点上运行

创建符合下列要求的目录 /webdev

所有者为 webdev

具有常规权限:owner=read+write+execute,group=read+write+execute,other=read+execute

具有 特殊权限:设置组 ID

用符号链接将 /var/www/html/webdev 链接到 /webdev

创建文件 /webdev/index.html,其中包含如下所示的单行文件:Development

dev 主机组中主机上浏览此目录(例如 http://172.25.250.9/webdev/)将生成以下输出:

1
Development
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[greg@control ansible]$ ansible dev -m shell -a 'cat /etc/group | grep webdev'
node1 | CHANGED | rc=0 >>
webdev:x:1003:
[greg@control ansible]$ vim /home/greg/ansible/webcontent.yml
---
- name: 创建 Web 内容目录
hosts: dev
roles:
- apache
tasks:
- name: Create a directory if it does not exist
file:
path: /webdev
state: directory
group: webdev
mode: '2775'
- name: Create a symbolic link
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: Copy using inline content
copy:
content: 'Development'
dest: /webdev/index.html
setype: httpd_sys_content_t
[greg@control ansible]$ ansible-playbook webcontent.yml
[greg@control ansible]$ curl http://172.25.250.9/webdev/
Development

修改文件内容

按照下方所述,创建一个名为 /home/greg/ansible/issue.yml 的 playbook:

该 playbook 将在 所有清单主机 上运行

该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:

dev 主机组中的主机上,这行文本显示为:Development

test 主机组中的主机上,这行文本显示为:Test

prod 主机组中的主机上,这行文本显示为:Production

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[greg@control ansible]$ vim /home/greg/ansible/issue.yml
---
- name: 修改文件内容
hosts: all
tasks:
- name: Copy using inline content
copy:
content: 'Development'
dest: /etc/issue
when: inventory_hostname in groups['dev']
- name: Copy using inline content
copy:
content: 'Test'
dest: /etc/issue
when: inventory_hostname in groups['test']
- name: Copy using inline content
copy:
content: 'Production'
dest: /etc/issue
when: inventory_hostname in groups['prod']
[greg@control ansible]$ ansible-playbook issue.yml
[greg@control ansible]$ ansible dev -a 'cat /etc/issue'
node1 | CHANGED | rc=0 >>
Development
[greg@control ansible]$ ansible test -a 'cat /etc/issue'
node2 | CHANGED | rc=0 >>
Test
[greg@control ansible]$ ansible prod -a 'cat /etc/issue'
node4 | CHANGED | rc=0 >>
Production
node3 | CHANGED | rc=0 >>
Production

生成主机文件

将一个初始模板文件从 http://materials/hosts.j2 下载到 /home/greg/ansible

完成该模板,以便用它生成以下文件:针对每个清单主机包含一行内容,其格式与 /etc/hosts 相同

创建名为 /home/greg/ansible/hosts.yml 的 playbook,它将使用此模板在 dev 主机组中的主机上生成文件 /etc/myhosts

该 playbook 运行后,dev 主机组中主机上的文件 /etc/myhosts 应针对每个受管主机包含一行内容:

1
2
3
4
5
6
7
8
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.250.9 node1.lab.example.com node1
172.25.250.10 node2.lab.example.com node2
172.25.250.11 node3.lab.example.com node3
172.25.250.12 node4.lab.example.com node4
172.25.250.13 node5.lab.example.com node5

注:清单主机名称的显示顺序不重要。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[greg@control ansible]$ wget http://materials/hosts.j2
[greg@control ansible]$ vim hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['nodename'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
[greg@control ansible]$ vim /home/greg/ansible/hosts.yml
---
- name: 生成主机文件
hosts: all
tasks:
- name: Template a file to /etc/myhosts
template:
src: hosts.j2
dest: /etc/myhosts
when: inventory_hostname in groups['dev']
[greg@control ansible]$ ansible-playbook hosts.yml
[greg@control ansible]$ ansible dev -a 'cat /etc/myhosts'
node1 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.250.9 node1.lab.example.com node1
172.25.250.10 node2.lab.example.com node2
172.25.250.13 node5.lab.example.com node5
172.25.250.11 node3.lab.example.com node3
172.25.250.12 node4.lab.example.com node4

创建和使用逻辑卷

创建一个名为 /home/greg/ansible/lv.yml 的 playbook,它将在 所有受管节点 上运行以执行下列任务:

创建符合以下要求的逻辑卷:

逻辑卷创建在 research 卷组中

逻辑卷名称为 data

逻辑卷大小为 1500 MiB

使用 ext4 文件系统格式化逻辑卷

如果无法创建请求的逻辑卷大小,应显示错误信息

1
Could not create logical volume of that size

,并且应改为使用大小 800 MiB

如果卷组 research 不存在,应显示错误信息

1
Volume group does not exist

不要以任何方式挂载逻辑卷

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[greg@control ansible]$ vim /home/greg/ansible/lv.yml
---
- name: 创建和使用逻辑卷
hosts: all
tasks:
- block:
- name: Create a logical volume of 1500m
lvol:
vg: research
lv: data
size: 1500m
- name: Create a ext4 filesystem on /dev/research/data
filesystem:
fstype: ext4
dev: /dev/research/data
rescue:
- debug:
msg: Could not create logical volume of that size
- name: Create a logical volume of 800m
lvol:
vg: research
lv: data
size: 800m
when: ansible_facts['lvm']['vgs']['research'] is defined
- debug:
msg: Volume group does not exist
when: ansible_facts['lvm']['vgs']['research'] is undefined
[greg@control ansible]$ ansible-playbook lv.yml

创建和使用分区

创建一个名为 /home/greg/ansible/partition.yml 的 playbook ,它将在 所有受管节点 上创建分区:

vdb 创建一个 1500M 主分区,分区号 1,并格式化 ext4

prod 组将分区永久挂载到 /data

如果磁盘空间不够,

给出提示信息 Could not create partition of that size

创建 800MiB 分区

如果 vdb不存在,则给出提示信息 this disk is not exist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[greg@control ansible]$ vim /home/greg/ansible/partition.yml
---
- name: 创建和使用分区
hosts: all
tasks:
- block:
- name: Create a new primary partition with a size of 1500MiB
parted:
device: /dev/vdb
number: 1
state: present
part_end: 1500MiB
- name: Create a ext4 filesystem on /dev/vdb1
filesystem:
fstype: ext4
dev: /dev/vdb1
- name: Mount /data
mount:
path: /data
src: /dev/vdb1
fstype: ext4
state: mounted
when: inventory_hostname in groups.prod
rescue:
- debug:
msg: Could not create partition of that size
- name: Create a new primary partition with a size of 800MiB
parted:
device: /dev/vdb
number: 1
state: present
part_end: 800MiB
when: ansible_facts['devices']['vdb'] is defined
- debug:
msg: this disk is not exist
when: ansible_facts['devices']['vdb'] is undefined
[greg@control ansible]$ ansible-playbook partition.yml

使用魔法变量

最常用的有四个:

hostvars

包含受管主机的变量,可以用于获取另一台受管主机的变量的值。

group_names

列出当前受管主机所属的所有组。

groups

列出清单中的所有组和主机。

inventory_hostname

包含清单中配置的当前受管主机的主机名称。

ansible localhost -m debug -a 'var=hostvars["localhost"]'