题目1
使用debug模块,显示当前受管主机的dns服务器的ip地址
解析:
bash
1.先创建一个文件
[devops@master chap03]$ vim test1.yml
2.写入要求
[devops@master chap03]$ cat test1.yml
---
- name: show dns.ip
hosts: all
tasks:
- name: show dns_ipaddress
debug:
var: ansible_facts.dns.nameservers #获取每个主机的dns
3.运行
[devops@master chap03]$ ansible-playbook test1.yml
结果:
bash
结果
[devops@master chap03]$ ansible-playbook test1.yml
PLAY [show dns.ip] ****************************************************************
TASK [Gathering Facts] ************************************************************
ok: [master]
ok: [node01]
ok: [node02]
TASK [show dns_ipaddress] *********************************************************
ok: [node02] => {
"ansible_facts.dns.nameservers": [
"114.114.114.114"
]
}
ok: [master] => {
"ansible_facts.dns.nameservers": [
"114.114.114.114"
]
}
ok: [node01] => {
"ansible_facts.dns.nameservers": [
"114.114.114.114"
]
}
PLAY RECAP ************************************************************************
master : ok=2 changed=0 unreachable=0 failed=0 skip ped=0 rescued=0 ignored=0
node01 : ok=2 changed=0 unreachable=0 failed=0 skip ped=0 rescued=0 ignored=0
node02 : ok=2 changed=0 unreachable=0 failed=0 skip ped=0 rescued=0 ignored=0
题目2
将example.conf文件复制到/etc/httpd/conf.d/目录,example.conf文件内容如下:
bash
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
解析:1).首先查看一下有没有/etc/httpd/conf.d/目录 没有的话要进行安装httpd服务
怎么安装?
bash
若是在普通用户里,需要提权才可以下载
[devops@master chap03]$ sudo yum install httpd -y
若是在root里
[root@master ~]# yum install httpd -y
也可以写在文件里面
[devops@master chap03]$ cat test2.yml
---
- name: copy content
hosts: master
tasks:
- name: install httpd #安装httpd
yum:
name: httpd
state: present
- name: copy
copy:
src: ./example.conf
dest: /etc/httpd/conf.d
2).接下来就是写playbook
bash
1.同样创建文件
[devops@master chap03]$ vim test2.yml
2.写入
[devops@master chap03]$ cat test2.yml
---
- name: copy content
hosts: master
tasks:
- name: install httpd #安装httpd
yum:
name: httpd
state: present
- name: copy
copy:
src: ./example.conf
dest: /etc/httpd/conf.d
3.运行
[devops@master chap03]$ ansible-playbook test2.yml
PLAY [copy content] ******************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [master]
TASK [install httpd] *****************************************************************************
ok: [master]
TASK [copy] **************************************************************************************
changed: [master]
PLAY RECAP ***************************************************************************************
master : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3).最后检查,查看是否复制到要求的目录下面
bash
[devops@master chap03]$ ll /etc/httpd/conf.d/
总用量 20
-rw-r--r-- 1 root root 2916 7月 20 2023 autoindex.conf
-rw-r--r-- 1 root root 158 6月 7 20:21 example.conf #这个
-rw-r--r-- 1 root root 400 7月 20 2023 README
-rw-r--r-- 1 root root 1252 7月 20 2023 userdir.conf
-rw-r--r-- 1 root root 653 7月 20 2023 welcome.conf
[devops@master chap03]$ cat /etc/httpd/conf.d/example.conf
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内
容如下:
bash
zuoye
1)写入
bash
[devops@master chap03]$ cat test2.yml
---
- name: copy content
hosts: master
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy #复制配置文件,文件更新时触发重启
copy:
src: ./example.conf
dest: /etc/httpd/conf.d
notify: restart httpd #会触发handlers
- name: write content
copy:
content: "zuoye\n"
dest: /var/www/html/index.html
- name: start and enable httpd #确保服务启动并设置开机自启
service:
name: httpd
state: started
enabled: yes
handlers: #被notify触发的重启服务任务
- name: restart httpd
service:
name: httpd
state: restarted
2)运行和检查
bash
1.运行文件
[devops@master chap03]$ ansible-playbook test2.yml
PLAY [copy content] ****************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [master]
TASK [install httpd] ***************************************************************************************************
ok: [master]
TASK [copy] ************************************************************************************************************
ok: [master]
TASK [write content] ***************************************************************************************************
ok: [master]
TASK [start and enable httpd] ******************************************************************************************
changed: [master]
PLAY RECAP *************************************************************************************************************
master : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.查看httpd服务状态
[devops@master chap03]$ systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Sun 2026-06-07 21:14:03 CST; 12s ago
Docs: man:httpd.service(8)
Main PID: 31925 (httpd)
Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec"
Tasks: 213 (limit: 10681)
Memory: 23.6M
CPU: 53ms
CGroup: /system.slice/httpd.service
├─31925 /usr/sbin/httpd -DFOREGROUND
├─31926 /usr/sbin/httpd -DFOREGROUND
├─31927 /usr/sbin/httpd -DFOREGROUND
├─31928 /usr/sbin/httpd -DFOREGROUND
└─31929 /usr/sbin/httpd -DFOREGROUND
3.查看/var/www/html/index.html是否写入内容
[devops@master chap03]$ cat /var/www/html/index.html
zuoye
题目3
向受管主机的/home/file文件里面写入内容如下:
bash
hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
bash
最好先查看一下磁盘 (由于我是在普通用户里所以需要提权 用sudo)
[devops@master chap03]$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 9.8G 0 rom /mnt
nvme0n1 259:0 0 100G 0 disk (主要的)
├─nvme0n1p1 259:1 0 1G 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 98G 0 part
├─rhel-root 253:0 0 94G 0 lvm /
└─rhel-swap 253:1 0 4G 0 lvm [SWAP]
[devops@master chap03]$ vim test4.yml
---
- name: wirte
hosts: all
gather_facts: yes
tasks:
- name: write /home/file
copy:
content: |
hostname={{ ansible_facts.hostname }}
memory={{ ansible_facts.memtotal_mb }}MB
BIOS version={{ ansible_facts.bios_version }}
distribution={{ ansible_facts.distribution }} {{ansible_facts.distribution_version }}
这里最好要查看一下自己的磁盘
Size of disk device is {{ ansible_facts.devices.nvme0n1.size }}
dest: /home/file
运行和查看结果:
bash
运行文件
[devops@master chap03]$ ansible-playbook test4.yml
PLAY [wirte] ***********************************************************************************************
TASK [Gathering Facts] *************************************************************************************
ok: [node01]
ok: [node02]
ok: [master]
TASK [write /home/file] ************************************************************************************
changed: [node02]
changed: [master]
changed: [node01]
PLAY RECAP *************************************************************************************************
master : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
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
查看是否写入 /home/file
[devops@master chap03]$ ansible all -m command -a "cat /home/file"
node01 | CHANGED | rc=0 >>
hostname=node01
memory=1731MB
BIOS version=VMW201.00V.24006586.B64.2406042154
distribution=RedHat 9.3
Size of disk device is 100.00 GB
node02 | CHANGED | rc=0 >>
hostname=node02
memory=1731MB
BIOS version=VMW201.00V.24006586.B64.2406042154
distribution=RedHat 9.3
Size of disk device is 100.00 GB
master | CHANGED | rc=0 >>
hostname=master
memory=1731MB
BIOS version=VMW201.00V.24006586.B64.2406042154
distribution=RedHat 9.3
Size of disk device is 100.00 GB
题目4
如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和
mariadb服务未运行则运行该服务。
①写入文件
bash
[devops@master chap03]$ vim test4.yml
---
- name: Server
hosts: all
tasks:
- name: install httpd and mariadb-server
yum:
name:
- httpd
- mariadb-server
state: present
loop: "{{ ansible_mounts }}"
when:
# 核心判断逻辑:筛选挂载点为 "/" 且总大小大于 1GB (1073741824 字节)
- item.mount == "/"
- item.size_total > 1073741824
register: install_result
- name: start httpd and mariadb-server
service:
name: "{{ item }}"
state: started # 确保服务正在运行
enable: yes # 确保开机自启
loop:
- httpd
- mariadb-server
when: install_result is changed
~
运行结果:
bash
[devops@master chap03]$ ansible-playbook test4.yml
PLAY [Server] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node01]
ok: [master]
ok: [node02]
TASK [install httpd and mariadb-server] ****************************************
ok: [node01] => (item={'mount': '/', 'device': '/dev/mapper/rhel-root', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 100860428288, 'size_available': 95579623424, 'block_size': 4096, 'block_total': 24624128, 'block_available': 23334869, 'block_used': 1289259, 'inode_total': 49281024, 'inode_available': 49149954, 'inode_used': 131070, 'uuid': '8ecbab0b-3af9-4543-95db-092759691ada'})
skipping: [node01] => (item={'mount': '/boot', 'device': '/dev/nvme0n1p2', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 1006632960, 'size_available': 701554688, 'block_size': 4096, 'block_total': 245760, 'block_available': 171278, 'block_used': 74482, 'inode_total': 524288, 'inode_available': 524266, 'inode_used': 22, 'uuid': 'e587d8ff-9c31-4801-a7b4-6e5b5b278043'})
skipping: [node01] => (item={'mount': '/boot/efi', 'device': '/dev/nvme0n1p1', 'fstype': 'vfat', 'options': 'rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro', 'size_total': 1071628288, 'size_available': 1064316928, 'block_size': 4096, 'block_total': 261628, 'block_available': 259843, 'block_used': 1785, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': 'AAAB-1E85'})
skipping: [node01] => (item={'mount': '/mnt', 'device': '/dev/sr0', 'fstype': 'iso9660', 'options': 'ro,relatime,nojoliet,check=s,map=n,blocksize=2048', 'size_total': 10528546816, 'size_available': 0, 'block_size': 2048, 'block_total': 5140892, 'block_available': 0, 'block_used': 5140892, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': '2023-10-25-09-17-25-00'})
ok: [node02] => (item={'mount': '/', 'device': '/dev/mapper/rhel-root', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 100860428288, 'size_available': 95645716480, 'block_size': 4096, 'block_total': 24624128, 'block_available': 23351005, 'block_used': 1273123, 'inode_total': 49281024, 'inode_available': 49149959, 'inode_used': 131065, 'uuid': '8ecbab0b-3af9-4543-95db-092759691ada'})
skipping: [node02] => (item={'mount': '/boot', 'device': '/dev/nvme0n1p2', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 1006632960, 'size_available': 701554688, 'block_size': 4096, 'block_total': 245760, 'block_available': 171278, 'block_used': 74482, 'inode_total': 524288, 'inode_available': 524266, 'inode_used': 22, 'uuid': 'e587d8ff-9c31-4801-a7b4-6e5b5b278043'})
skipping: [node02] => (item={'mount': '/boot/efi', 'device': '/dev/nvme0n1p1', 'fstype': 'vfat', 'options': 'rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro', 'size_total': 1071628288, 'size_available': 1064316928, 'block_size': 4096, 'block_total': 261628, 'block_available': 259843, 'block_used': 1785, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': 'AAAB-1E85'})
skipping: [node02] => (item={'mount': '/mnt', 'device': '/dev/sr0', 'fstype': 'iso9660', 'options': 'ro,relatime,nojoliet,check=s,map=n,blocksize=2048', 'size_total': 10528546816, 'size_available': 0, 'block_size': 2048, 'block_total': 5140892, 'block_available': 0, 'block_used': 5140892, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': '2023-10-25-09-17-25-00'})
ok: [master] => (item={'mount': '/', 'device': '/dev/mapper/rhel-root', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 100860428288, 'size_available': 95464501248, 'block_size': 4096, 'block_total': 24624128, 'block_available': 23306763, 'block_used': 1317365, 'inode_total': 49281024, 'inode_available': 49137753, 'inode_used': 143271, 'uuid': '8ecbab0b-3af9-4543-95db-092759691ada'})
skipping: [master] => (item={'mount': '/boot', 'device': '/dev/nvme0n1p2', 'fstype': 'xfs', 'options': 'rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 1006632960, 'size_available': 701554688, 'block_size': 4096, 'block_total': 245760, 'block_available': 171278, 'block_used': 74482, 'inode_total': 524288, 'inode_available': 524266, 'inode_used': 22, 'uuid': 'e587d8ff-9c31-4801-a7b4-6e5b5b278043'})
skipping: [master] => (item={'mount': '/boot/efi', 'device': '/dev/nvme0n1p1', 'fstype': 'vfat', 'options': 'rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro', 'size_total': 1071628288, 'size_available': 1064316928, 'block_size': 4096, 'block_total': 261628, 'block_available': 259843, 'block_used': 1785, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': 'AAAB-1E85'})
skipping: [master] => (item={'mount': '/mnt', 'device': '/dev/sr0', 'fstype': 'iso9660', 'options': 'ro,relatime,nojoliet,check=s,map=n,blocksize=2048', 'size_total': 10528546816, 'size_available': 0, 'block_size': 2048, 'block_total': 5140892, 'block_available': 0, 'block_used': 5140892, 'inode_total': 0, 'inode_available': 0, 'inode_used': 0, 'uuid': '2023-10-25-09-17-25-00'})
TASK [start httpd and mariadb-server] ******************************************
skipping: [node02] => (item=httpd)
skipping: [node02] => (item=mariadb-server)
skipping: [node02]
skipping: [master] => (item=httpd)
skipping: [master] => (item=mariadb-server)
skipping: [master]
skipping: [node01] => (item=httpd)
skipping: [node01] => (item=mariadb-server)
skipping: [node01]
PLAY RECAP *********************************************************************
master : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node01 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
题目5
创建一个playbook,要求如下:
该playbook运行在所有受控节点
该playbook覆盖/etc/message文件的内容
在dev主机组的主机上,内容是:Development
在test主机组的主机上,内容是:Test
1)首先创建一个playbook叫做test5.yml,并写入要求
bash
[devops@master chap03]$ vim test5.yml
---
- name: cover /etc/message content
hosts: all
tasks:
- name: dev
copy:
content: "Development\n"
dest: /etc/message
when: "'dev' in group_names" #只有当主机在"dev"这个主机组里面才可以执行上面
- name: test
copy:
content: "Test\n"
dest: /etc/message
when: "'test' in group_names" #只有当主机在"test"这个主机组里面才可以执行上面
2). 对主机组进行配对(我的分配)
dev组
- master
- node01
test组:
- node02
bash
注意该inventory文件必须要与你要执行的文件要在同一个目录下面,
才可以执行
[devops@master chap03]$ cat inventory
master
node01
node02
[dev]
master
node01
[test]
node02
[webservers]
master
[dbservers]
node01
[servers:children]
webservers
dbservers
3)运行和查看
bash
运行文件
[devops@master chap03]$ ansible-playbook test5.yml
PLAY [cover /etc/message content] **********************************************
TASK [Gathering Facts] *********************************************************
ok: [master]
ok: [node01]
ok: [node02]
TASK [dev] *********************************************************************
skipping: [node02]
changed: [master]
changed: [node01]
TASK [test] ********************************************************************
skipping: [master]
skipping: [node01]
changed: [node02]
PLAY RECAP *********************************************************************
master : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node01 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
查看结果是否写上
[devops@master chap03]$ ansible all -m command -a "cat /etc/message"
node02 | CHANGED | rc=0 >>
Test
node01 | CHANGED | rc=0 >>
Development
master | CHANGED | rc=0 >>
Development