Linux-ansible-roles部署apache
roles部署apache-yum安装
建立角色目录
[root@centos7-liyj /data/ansible]#mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files} mkdir: created directory ‘/data/ansible/roles/httpd’ mkdir: created directory ‘/data/ansible/roles/httpd/tasks’ mkdir: created directory ‘/data/ansible/roles/httpd/handlers’ mkdir: created directory ‘/data/ansible/roles/httpd/files’ [root@centos7-liyj /data/ansible]#tree roles/httpd/ roles/httpd/ ├── files ├── handlers └── tasks 3 directories, 0 files
元素执行顺序,task的入口文件
[root@centos7-liyj /data/ansible]#vim roles/httpd/tasks/main.yml [root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/main.yml -include: group.yml -include: user.yml -include: install.yml -include: config.yml -include: index.yml -include: service.yml
1、创建apache系统组
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/group.yml - name: create apapche group group: name=apache system=yes gid=80
2、创建系统用户
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/user.yml - name: create apache user user: name=apache group=apache shell=/sbin/nologin uid=80 system=yes home=/var/www/
3、安装httpd
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/install.yml - name: install httpd package yum: name=httpd
4、拷贝配置文件
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/config.yml - name: index.html copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes notify: restart
5、拷贝页面文件
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/index.yml - name: index.html copy: src=index.html dest=/var/www/html/
6、开机启动服务
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/service.yml - name: start service service: name=httpd state=started enabled=yes
7、主机ip地址作业页面内容
[root@centos7-liyj /data/ansible]#cat roles/httpd/tasks/echo.yml - name: echo IP > index.html shell: echo `ifconfig|sed -nr '2s/^[^0-9]+([0-9.]+).*/\1/p'` > /var/www/html/index.html
8、重启服务触发设置
[root@centos7-liyj /data/ansible]#cat roles/httpd/handlers/main.yml - name: restart service: name=httpd state=restarted
9、主机清单
/etc/ansible/hosts 添加 [websrvs] 10.0.0.37 10.0.0.57
10、执行角色文件,root身份执行httpd目录下元素
[root@centos7-liyj /data/ansible]#cat role_httpd.yml --- - hosts: websrvs remote_user: root roles: - httpd
执行过程

[root@centos7-liyj /data/ansible]#ansible-playbook role_httpd.yml PLAY [websrvs] ******************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************ ok: [10.0.0.37] ok: [10.0.0.57] TASK [httpd : create apapche group] *********************************************************************************** changed: [10.0.0.57] changed: [10.0.0.37] TASK [httpd : create apache user] ************************************************************************************* changed: [10.0.0.57] changed: [10.0.0.37] TASK [httpd : install httpd package] ********************************************************************************** changed: [10.0.0.37] changed: [10.0.0.57] TASK [httpd : index.html] ********************************************************************************************* ok: [10.0.0.57] ok: [10.0.0.37] TASK [httpd : index.html] ********************************************************************************************* changed: [10.0.0.37] changed: [10.0.0.57] TASK [httpd : start service] ****************************************************************************************** changed: [10.0.0.37] changed: [10.0.0.57] TASK [httpd : echo IP > index.html] *********************************************************************************** changed: [10.0.0.57] changed: [10.0.0.37] PLAY RECAP ************************************************************************************************************ 10.0.0.37 : ok=8 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.0.0.57 : ok=8 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
页面测试
分类:
实战案例
, 运维自动化之ANSIBLE
标签:
ansible
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)