实验环境
Ansible Server Centos7.9
192.168.118.189
Http Server Centos7.9
192.168.118.25
192.168.118.43
1.创建角色相关的目录
mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files}
2.创建角色相关文件
cd /data/ansible/roles/httpd/
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/group.yml
- name: create apache group
group: name=apache system=yes gid=80
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/user.yml
- name: create apache user
user: name=apache system=yes shell=/sbin/nologin home=/var/www uid=80 group=apache
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/install.yml
- name: install httpd package
yum: name=httpd
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/config.yml
- name: config file
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/index.yml
- name: index.html
copy: src=index_master.html dest=/var/www/html/index.html
when: ansible_hostname == "master"
- name: index.html
copy: src=index_slave.html dest=/var/www/html/index.html
when: ansible_hostname == "slave"
[root@mycat ansible]# cat /data/ansible/roles/httpd/tasks/service.yml
- name: start service
service: name=httpd state=started enabled=yes
PlayBook结构
[root@mycat files]# tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│ ├── httpd.conf
│ ├── index.html
│ ├── index_master.html
│ └── index_slave.html
├── handlers
│ └── main.yml
└── tasks
├── config.yml
├── group.yml
├── index.yml
├── install.yml
├── main.yml
├── service.yml
└── user.yml
3.执行 ansible-playbook -C role_httpd.yml测试配置是否正确
[root@mycat ansible]# ansible-playbook -C role_httpd.yml
PLAY [websrvs] ****************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.118.43]
ok: [192.168.118.25]
TASK [httpd : create apache group] ********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : create apache user] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [install httpd package] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : config file] ****************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.25]
changed: [192.168.118.43]
TASK [httpd : start service] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
RUNNING HANDLER [httpd : restart] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
PLAY RECAP ********************************************************************************************************************************************************
192.168.118.25 : ok=8 changed=7 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.118.43 : ok=8 changed=7 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
4.执行ansible playbook安装配置httpd
[root@mycat ansible]# ansible-playbook role_httpd.yml
PLAY [websrvs] ****************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.118.25]
ok: [192.168.118.43]
TASK [httpd : create apache group] ********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : create apache user] *********************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
TASK [install httpd package] **************************************************************************************************************************************
changed: [192.168.118.25]
changed: [192.168.118.43]
TASK [httpd : config file] ****************************************************************************************************************************************
ok: [192.168.118.25]
ok: [192.168.118.43]
TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.43]
changed: [192.168.118.25]
TASK [httpd : index.html] *****************************************************************************************************************************************
skipping: [192.168.118.25]
changed: [192.168.118.43]
TASK [httpd : start service] **************************************************************************************************************************************
changed: [192.168.118.43]
changed: [192.168.118.25]
PLAY RECAP ********************************************************************************************************************************************************
192.168.118.25 : ok=7 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.118.43 : ok=7 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
5.查看httpd服务是否正常启动
[root@master ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
6.登录web页面查看是否符合预期