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   
执行过程代码

 

 

 页面测试

 

 

 

 

posted @ 2022-05-22 13:12  goodbay说拜拜  阅读(59)  评论(0编辑  收藏  举报