1、准备坏境
redhat8.2 | IP | 运用 |
---|
master | 192.168.8.129 | ansible自动化工具 | node1 | 192.168.8.132 | httpd mysql php |
2、ansible安装
//安装网络仓库
[root@master ~]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2595 100 2595 0 0 22964 0 --:--:-- --:--:-- --:--:-- 22964
//安装epel源
[root@master ~]
//安装ansible
[root@master ~]
//查看ansible版本
[root@master ~]
ansible 2.9.23
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Dec 5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]
[root@master project]
192.168.8.132 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@master project]
[root@master project]
192.168.8.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "4966466ad015ef3d2a3cc0b8252d43efbdcf2c94",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
3、创建一个play文件
[root@master project]
- name: install package //利用循环语句下载三个服务
hosts: httpd
vars_files:
- vars/lamp.yml //指定变量的文件位置
tasks:
- name: install package
dnf: //运用dnf模块
name: '{{ item }}' //下载服务的列表
loop: '{{ packages }}' //指定packages列表
- name: service is running //利用循环语句开启三个服务
hosts: httpd
vars_files:
- vars/lamp.yml
tasks:
- name: restart service
service: //服务模块
name: '{{ item }}'//启动服务名字
state: started //开启服务
enabled: yes //开机自启
loop: '{{ services }}'//指定services列表
- name: copy //利用copy模块修改配置文件
hosts: httpd
vars_files:
- vars/lamp.yml
tasks:
- name: copy
copy:
src: '{{ item.src }}'//复制列表中原地址的位置
dest: '{{ item.dest }}'//目标地址的位置
loop: '{{ srcanddest }}'//指定srcanddest列表
- name: mkdir/www/abc //这里可以不用使用循环语句,直接用file模块创建访问路径更方便,循环语句主要利用在大量重复一个动作比较合适,节省时间反复去完成,只需要定义列表即可
hosts: httpd
vars_files:
- vars/lamp.yml
vars:
power: True//定义是
tasks:
- name: mkdir/www/abc
file: //利用file模块创建访问点的路径
path: '{{ item }}'//创建文件的路径
state: directory
loop: '{{ mkdir}}'
when: power//定义power如果是就执行如果否就不执行
- name: index.php//利用template模块发送测试文件
hosts: httpd
tasks:
- name: index.php
template:
src: /opt/project/index.php
dest: /www/abc/index.php
- name: restarted service //利用循环语句重启服务
hosts: httpd
vars_files:
- vars/lamp.yml
tasks:
- name: restart service
service:
name: '{{ item }}'
state: restarted
loop: '{{ services }}'
[root@master project]
[root@master project]
packages: //下载服务列表
- httpd
- mariadb*
- php*
services: //启动服务列表
- httpd
- mariadb
- php-fpm
srcanddest: //复制文件路径列表
- src: /etc/httpd/conf/httpd.conf
dest: /etc/httpd/conf/httpd.conf
- src: /etc/php-fpm.d/www.conf
dest: /etc/php-fpm.d/www.conf
mkdir: //创建访问路径列表
- /www/abc
[root@master project]
[root@master project]
PLAY [install package] *************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [install package] *************************************************************************
changed: [192.168.8.132] => (item=httpd)
changed: [192.168.8.132] => (item=mariadb*)
changed: [192.168.8.132] => (item=php*)
PLAY [service is running] **********************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [restart service] *************************************************************************
changed: [192.168.8.132] => (item=httpd)
changed: [192.168.8.132] => (item=mariadb)
changed: [192.168.8.132] => (item=php-fpm)
PLAY [copy] ************************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [copy] ************************************************************************************
changed: [192.168.8.132] => (item={'src': '/etc/httpd/conf/httpd.conf', 'dest': '/etc/httpd/conf/httpd.conf'})
changed: [192.168.8.132] => (item={'src': '/etc/php-fpm.d/www.conf', 'dest': '/etc/php-fpm.d/www.conf'})
PLAY [mkdir/www/abc] ***************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [mkdir/www/abc] ***************************************************************************
ok: [192.168.8.132] => (item=/www/abc)
PLAY [index.php] *******************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [index.php] *******************************************************************************
ok: [192.168.8.132]
PLAY [restarted service] ***********************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [192.168.8.132]
TASK [restart service] *************************************************************************
changed: [192.168.8.132] => (item=httpd)
changed: [192.168.8.132] => (item=mariadb)
changed: [192.168.8.132] => (item=php-fpm)
PLAY RECAP *************************************************************************************
192.168.8.132 : ok=12 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
利用copy模块覆盖配置文件,达到修改配置文件效果,详细配置文件修改请参考这篇文章ansible部署lamp架构
4、验证实验效果
如果出现以下画面就部署成功了,如果失败请检查配置文件和确保防火墙已关闭
|