文章目录
- 使用role角色安装httpd服务
- 使用role角色安装nginx服务
使用role角色安装httpd服务
//创建ansible目录
[root@node-17 ~]# mkdir /data/ansible
//进入ansible目录
[root@node-17 ~]# cd /data/ansible/
//创建存放角色的目录
[root@node-17 ansible]# mkdir roles/{httpd,mysql,nginx}/{tasks,files,templates,vars,handlers} -p
//提前安装httpd服务,后面需要其配置文件httpd.conf
[root@node-17 httpd]# yum -y install httpd
//将httpd服务配置文件httpd.conf文件拷贝至角色roles目录下的files目录下
[root@node-17 httpd]# cp /etc/httpd/conf/httpd.conf ./files/
//在/roles/files/目录下创建我们想要展示的网站页面文件index.yml
[root@node-17 httpd]# vim ./files/index.html
//新建编辑tasks目录下的main.yml文件
[root@node-17 httpd]# vim ./tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- inclede: service.yml
---------------------------------------------
//查看我们创建的main.yml文件,根据流程继续创建文件
[root@node-17 httpd]# cat ./tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- inclede: service.yml
//新建编辑install.yml文件
[root@node-17 httpd]# vim ./tasks/config.yml
---
- name: install httpd
yum: name=httpd
----------------------------------------------
//新建编辑config.yml文件
[root@node-17 httpd]# vim ./tasks/config.yml
---
- name: config
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf backup=yes
notify: restart #触发handler里定义的事件,名字要和/handlers/main.yml文件里定义的事件名相同
------------------------------------------------
//新建编辑index.ynml文件
[root@node-17 httpd]# vim ./tasks/index.yml
---
- name: index.html
copy: src=index.html dest=/var/www/html/index.html
--------------------------------------------------
//新建编辑service.yml文件
[root@node-17 httpd]# vim ./tasks/service.yml
---
- name: start service
service: name=httpd enabled=yes state=started
//查看httpd角色目录
[root@node-17 httpd]# tree
.
├── files
│ ├── httpd.conf
│ └── index.html
├── handlers
├── tasks
│ ├── config.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
└── vars
5 directories, 7 files
//新建编辑重启服务handler文件
[root@node-17 httpd]# vim ./handlers/main.yml
---
- name: restart #与notify触发器定义的名字相同
service: name=httpd state=restarted
//记得要回到和/roles/目录同一个目录下再继续进行
[root@node-17 nginx]# cd ../../
//新建编辑调用角色的httpd_role.yml文件
[root@node-17 ansible]# vim /data/ansible/httpd_role.yml
---
- hosts: websrvs
remote_user: root
roles:
- httpd
//使用ansible-playbook -C 检查.yml文件可能出现的语法错误(干跑)
[root@node-17 ansible]# ansible-playbook -C httpd_role.yml
PLAY [websrvs] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [httpd : install httpd] ********************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [httpd : config] ***************************************************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
TASK [httpd : index.html] ***********************************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [httpd : start service] ********************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
RUNNING HANDLER [httpd : restart] ***************************************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
PLAY RECAP **************************************************************************************************************************
192.168.26.37 : ok=6 changed=3 unreachable=0 failed=0
192.168.26.47 : ok=6 changed=3 unreachable=0 failed=0
//使用ansible-playbook 命令运行httpd_role.yml文件
[root@node-17 ansible]# ansible-playbook httpd_role.yml
PLAY [websrvs] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [httpd : install httpd] ********************************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [httpd : config] ***************************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [httpd : index.html] ***********************************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [httpd : start service] ********************************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
PLAY RECAP **************************************************************************************************************************
192.168.26.37 : ok=5 changed=0 unreachable=0 failed=0
192.168.26.47 : ok=5 changed=0 unreachable=0 failed=0
//查看远程主机服务状态
[root@node-47 ~]# ss -ntl
LISTEN 0 128 [::]:8888 [::]:*
-----------------------------------------------------------------------
[root@node-37 ~]# ss -ntl
LISTEN 0 128 [::]:8888 [::]:*
查看网页信息 |
//角色运行成功,最后确认下/ansible/roles/httpd/底下的目录结构
[root@node-17 ansible]# tree ./roles/httpd/
./roles/httpd/
├── files
│ ├── httpd.conf
│ └── index.html
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
└── vars
5 directories, 8 files
使用role角色安装nginx服务
//提前安装nginx服务,获取我们需要的配置文件nginx.conf
[root@node-17 nginx]# yum -y install nginx #centos7上
//将centos7上的nginx.conf文件拷至/roles/nginx/temlates/目录下
[root@node-17 nginx]# cp /etc/nginx/nginx.conf /data/ansible/roles/nginx/templates/nginx7.conf.j2
//提前安装nginx服务,获取我们需要的配置文件nginx.conf
[root@node-18 ~]# yum -y install nginx #centos8上
//将centos8上的nginx.conf文件拷至ansible主机上/roles/nginx/temlates/目录下
[root@node-18 ~]# scp /etc/nginx/nginx.conf 192.168.26.17:/data/ansible/roles/nginx/templates/
---------------------------------------------------------------------------
//查看templates目录得,两个文件已存在
[root@node-17 nginx]# ls ./templates/
nginx7.conf.j2 nginx8.conf.j2
---------------------------------------------
//编辑centos8你自己想要自定义的nginx配置文件
[root@node-17 nginx]# vim ./templates/nginx8.conf.j2
user {{ username }};
worker_processes {{ ansible_processor_vcpus+2 }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
---------------------------------------------
//编辑centos8你自己想要自定义的nginx配置文件
[root@node-17 nginx]# vim ./templates/nginx7.conf.j2
user {{ username }};
worker_processes {{ ansible_processor_vcpus+2 }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
---------------------------------------------
//创建编辑在nginx{7|8}.conf.j2中自定义的变量值
[root@node-17 nginx]# vim ./vars/main.yml
---
username: daemon
//进入nginx目录下
[root@node-17 ansible]# cd /roles/nginx/
//新建编辑tasks目录下的main.yml文件
[root@node-17 nginx]# vim ./tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- include: servive.yml
------------------------------------------------
//查看我们创建的main.yml文件,根据流程继续创建文件
[root@node-17 nginx]# cat ./tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- include: servive.yml
//新建编辑install.yml文件
[root@node-17 nginx]# vim ./tasks/install.yml
---
- name: install
yum: name=nginx
-----------------------------------------------
//新建编辑config.yml文件
[root@node-17 nginx]# vim ./tasks/config.yml
---
- name: config file
template: src=nginx7.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="7"
notify: restart
- name: config file
template: src=nginx8.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="8"
notify: restart
-----------------------------------------------
//新建编辑index.yml文件
[root@node-17 nginx]# vim ./tasks/index.yml
---
- name: index.html
copy: src=roles/httpd/files/index.html dest=/usr/shar
e/nginx/html
//此处index.html文件引用的是/httpd/files/下的文件
-----------------------------------------------
//新建编辑service.yml文件
[root@node-17 nginx]# vim ./tasks/service.yml
---
- name: start service
service: name=nginx state=started enabled=yes
//查看/nginx/目录下的结构树
[root@node-17 nginx]# tree
.
├── files
├── handlers
├── tasks
│ ├── config.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
│ ├── nginx7.conf.j2
│ └── nginx8.conf.j2
└── vars
5 directories, 7 files
//查看定义notify触发器名
[root@node-17 nginx]# cat ./tasks/config.yml
---
- name: config file
template: src=nginx7.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="7"
notify: restart
- name: config file
template: src=nginx8.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version=="8"
notify: restart
新建编辑重启服务handler文件
[root@node-17 nginx]# vim ./handlers/main.yml
---
- name: restart #与notify触发器定义的名字相同
service: name=httpd state=restarted
//记得要回到和/roles/目录同一个目录下再继续进行
[root@node-17 nginx]# cd ../../
//新建编辑调用角色的nginx_role.yml文件
[root@node-17 ansible]# vim /data/ansible/nginx_role.yml
- hosts: websrvs
remote_user: root
roles:
- nginx
//使用ansible-playbook -C 检查.yml文件可能出现的语法错误(干跑)
[root@node-17 ansible]# ansible-playbook -C nginx_role.yml
PLAY [websrvs] **********************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [192.168.26.37]
ok: [192.168.26.47]
TASK [nginx : install] **************************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [nginx : config file] **********************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [nginx : config file] **********************************************************************************
skipping: [192.168.26.47]
skipping: [192.168.26.37]
TASK [nginx : index.html] ***********************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
TASK [nginx : start service] ********************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
RUNNING HANDLER [nginx : restart] ***************************************************************************
changed: [192.168.26.47]
changed: [192.168.26.37]
PLAY RECAP **************************************************************************************************
192.168.26.37 : ok=6 changed=5 unreachable=0 failed=0
192.168.26.47 : ok=6 changed=5 unreachable=0 failed=0
//使用ansible-playbook 命令运行httpd_role.yml文件
[root@node-17 ansible]# ansible-playbook nginx_role.yml
PLAY [websrvs] **********************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [192.168.26.47]
ok: [192.168.26.37]
TASK [nginx : install] **************************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
TASK [nginx : config file] **********************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
TASK [nginx : config file] **********************************************************************************
skipping: [192.168.26.47]
skipping: [192.168.26.37]
TASK [nginx : index.html] ***********************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
TASK [nginx : start service] ********************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
RUNNING HANDLER [nginx : restart] ***************************************************************************
changed: [192.168.26.37]
changed: [192.168.26.47]
PLAY RECAP **************************************************************************************************
192.168.26.37 : ok=6 changed=5 unreachable=0 failed=0
192.168.26.47 : ok=6 changed=5 unreachable=0 failed=0
查看网页信息 |
//角色运行成功,最后确认下/ansible/roles/httpd/底下的目录结构
[root@node-17 ansible]# tree ./roles/nginx/
./roles/nginx/
├── files
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ └── service.yml
├── templates
│ ├── nginx7.conf.j2
│ └── nginx8.conf.j2
└── vars
└── main.yml
5 directories, 9 files