Redhat8.2 | IP | 运用 |
---|---|---|
master | 192.168.8.129 | ansible运维工具 |
node1 | 192.168.8.132 | apache(httpd) |
node2 | 192.168.8.133 | mysql数据库 |
node3 | 192.168.8.134 | php |
//安装网络仓库
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
% 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 ~]# dnf -y install epel-release
//安装ansible
[root@master ~]# dnf -y install ansible
//查看ansible版本
[root@master ~]# ansible --version
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]# ls //可以不用移动到/opt/project目录,看个人喜好,如果你没有移动,默认文件应该在/etc/ansible
ansible.cfg inventory
[root@master project]# cat inventory
[httpd]
192.168.8.132 ansible_user=root ansible_password=1 //用户和密码
[mysql]
192.168.8.133 ansible_user=root ansible_password=1
[php]
192.168.8.134 ansible_user=root ansible_password=1
[root@master project]#
[root@master project]# ansible all -m ping
192.168.8.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.8.132 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.8.133 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@master project]#
[root@master project]# ls
ansible.cfg inventory
[root@master project]# mkdir playbook
[root@master project]# vim playbook/lamp.yml
[root@master project]# cat playbook/lamp.yml
--- //文件开头
- name: CentOS-Base.repo //项目的名字
hosts: all //执行任务的主机IP
tasks: //任务列表
- name: CentOS-Base.repo //任务的描述
template: //利用remplate模块
src: /etc/yum.repos.d/CentOS-Base.repo //被发送的文件路径
dest: /etc/yum.repos.d/CentOS-Base.repo //指定文件的位置
- name: no firewalld //关闭防火墙
hosts: all
tasks:
- name: no firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: selinux disabled //修改防火墙selinux=disabled
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: install httpd //安装httpd服务
hosts: httpd
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: copy //利用copy模块修改httpd的配置文件
copy:
src: /etc/httpd/conf/httpd.conf
dest: /etc/httpd/conf/httpd.conf
- name: httpd service //开机自启httpd服务
service:
name: httpd
state: started
enabled: yes
- name: install mariadb //安装mysql服务
hosts: mysql
tasks:
- name: install mariadb
yum:
name: mariadb*
state: latest
- name: mariadb service //开机自启mysql服务
service:
name: mariadb
state: started
enabled: yes
- name: install php //安装php服务
hosts: php
tasks:
- name: install php
yum:
name: php*
state: latest
- name: copy //利用copy模块修改php配置文件
copy:
src: /etc/php-fpm.d/www.conf
dest: /etc/php-fpm.d/www.conf
- name: php service //开机自启php服务
service:
name: php-fpm
state: started
enabled: yes
- name: mkdir /www/abc //创建根目录
file:
path: /www/abc
state: directory
- name: copy //将写好的配置文件发送到根目录
copy:
src: /opt/project/index.php
dest: /www/abc/
- name: restarted httpd //重启服务
hosts: httpd
tasks:
- name: restarted httpd
service:
name: httpd
state: restarted
- name: restarted php-fpm
hosts: php-fpm
tasks:
- name: restarted php-fpm
service:
name: php-fpm
state: restarted
[root@master project]#
httpd和php配置文件需要先下载下来,进行修改,然后才能copy模块进行覆盖达到修改配置文件的效果,具体参考:Ansible部署lamp架构配置文件
//playbook有着严格的编写要求,我也是一直报错检查了好久编写文件
[root@master project]# ansible-playbook playbook/lamp.yml
PLAY [CentOS-Base.repo] ***************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.133]
ok: [192.168.8.132]
ok: [192.168.8.134]
TASK [CentOS-Base.repo] ***************************************************************************************************
ok: [192.168.8.133]
ok: [192.168.8.134]
ok: [192.168.8.132]
PLAY [no firewalld] *******************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]
ok: [192.168.8.133]
ok: [192.168.8.134]
TASK [no firewalld] *******************************************************************************************************
ok: [192.168.8.133]
ok: [192.168.8.132]
ok: [192.168.8.134]
TASK [selinux disabled] ***************************************************************************************************
ok: [192.168.8.132]
ok: [192.168.8.134]
ok: [192.168.8.133]
PLAY [install httpd] ******************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]
TASK [install httpd] ******************************************************************************************************
ok: [192.168.8.132]
TASK [copy] ***************************************************************************************************************
ok: [192.168.8.132]
TASK [httpd service] ******************************************************************************************************
ok: [192.168.8.132]
PLAY [install mariadb] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.133]
TASK [install mariadb] ****************************************************************************************************
ok: [192.168.8.133]
TASK [mariadb service] ****************************************************************************************************
ok: [192.168.8.133]
PLAY [install php] ********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.134]
TASK [install php] ********************************************************************************************************
ok: [192.168.8.134]
TASK [copy] ***************************************************************************************************************
ok: [192.168.8.134]
TASK [php service] ********************************************************************************************************
ok: [192.168.8.134]
TASK [mkdir /www/abc] *****************************************************************************************************
ok: [192.168.8.134]
TASK [copy] ***************************************************************************************************************
changed: [192.168.8.134]
PLAY [restarted httpd] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]
TASK [restarted httpd] ****************************************************************************************************
changed: [192.168.8.132]
[WARNING]: Could not match supplied host pattern, ignoring: php-fpm
PLAY [restarted php-fpm] **************************************************************************************************
skipping: no hosts matched
PLAY RECAP ****************************************************************************************************************
192.168.8.132 : ok=11 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.8.133 : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.8.134 : ok=11 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0