ansible练习三
一、部署web服务器
1、部署yum仓库、
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)
5、该web服务器的DocumentRoot为/www
6、实现在ansible中能够使用http://node1访问到该网页内容
[student@ansible ansible]$ vim a.yml
---
- name: web statin
hosts: node1
tasks:
- name: disposition AppStream yum
yum_repository:
file: myrepo
name: AppStream
description: Centos8.0 AppStream
baseurl: http://mirrors.ustc.edu.cn/centos-vault/8.5.2111/AppStream/x86_64/os/
enabled: yes
gpgcheck: no
- name: disposition BaseOS yum
yum_repository:
file: myrepo
name: BaseOS
description: Centos8.0 BaseOS
baseurl: http://mirrors.ustc.edu.cn/centos-vault/8.5.2111/BaseOS/x86_64/os/
enabled: yes
gpgcheck: no
- name: mount Dev
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: install httpd
yum:
name: httpd
state: installed
- name: create link
file:
src: /var/www/html
dest: /www
state: link
- name: create files
file:
path: /www/index.html
state: touch
- name: Add content
lineinfile:
dest: /www/index.html
line: my name is zhaoshulin
- name: set selinux context
file:
path: '/www/index.html'
setype: httpd_sys_content_t
- name: apply context
shell:
cmd: restorecon -Rv /www/index.html
- name: modify apache config1
replace:
path: /etc/httpd/conf/httpd.conf
regexp: DocumentRoot "/var/www/html"
replace: DocumentRoot "/www"
- name: modify apache config2
replace:
path: /etc/httpd/conf/httpd.conf
regexp: <Directory "/var/www">
replace: <Directory "/www">
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
- name: set firewalld for httpd
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
[student@ansible ansible]$ ansible-playbook a.yml
******略******
PLAY RECAP ********************************************************************************************
node1 : ok=14 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
测试
[student@ansible ansible]$ curl http://node1
my name is liuxiang
二、使用notify....handlers
1、写一个剧本runtime.yml,只对node1操作
2、创建用户aa,该用户不能用于登录,家目录/www
3、在/www创建一个文件html
4、每次执行该剧本时,将系统的当前时间输入到html文件中。
5、如果html中的时间发生变化,那么创建/tmp/kk的文件
[student@ansible ansible]$ vim runtime.yml
---
- name: runtime
hosts: node1
tasks:
- name: useradd aa
user:
name: aa
shell: /sbin/nologin
home: /www
- name: create file
file:
path: /www/html
state: touch
- name: date
shell: date > /www/html
notify:
- kk
handlers:
- name: kk
file:
path: /tmp/kk
state: touch
[student@ansible ansible]$ ansible-playbook b.yml
******略******
PLAY RECAP ********************************************************************************************
node1 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?