ansible部署apache
安装ansble
Ansible 包不在 CentOS 8 默认的软件包仓库中。因此,我们需要执行以下命令启用 EPEL 仓库
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
启用 epel 仓库后,执行以下 dnf 命令安装 Ansible:
yum install -y ansible*
2.配置HOSTS 每个节点都配置
master | 192.168.1.11 |
---|
node02 | 192.168.1.23 |
[root@master /]# vim /etc/hosts
192.168.1.11 master
192.168.1.23 node02
3.配置单向免密登录 只在管理节点进行操作 并测试
ssh-keygen
ssh-copy-id root@192.168.1.23
scp /etc/hosts root@192.168.1.23 #复制hosts文件到客户端
将/etc/ansible/* 文件复制到家目录
cp -a /etc/ansinle /home/用户/
5.修改配置文件
[root@master /]# vim /home/whd/ansible/ansible.cfg
inventory = /home/whd/ansible/hosts
# 配置主机清单
[root@master /]# vim /home/whd/ansible/hosts
[server]
root@192.168.1.11
[node]
192.168.1.23
验证
[root@master ansible]# ansible all -m ping
root@192.168.1.11 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: root@192.168.1.11: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
192.168.1.23 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
# 出现错误 ,客户端主机权限拒绝
# 解决方法
[root@master ansible]# ansible all -m ping --ask-pass --ask-become-pass #加入 --ask-pass --ask-become-pass
root@192.168.1.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.1.23 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
#ok
编写apache yaml文件
[root@master ansible]# vim apache.yaml
---
- name:
hosts: server
tasks:
- name: install httpd server
yum:
name: httpd*
state: present
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- name: isstall php
yum:
name: php*
name: php-fpm*
state: present
- name: start php-fpm
service:
name: php-fpm
state: started
enabled: yes
## 执行yaml文件
~~~3
[root@master ansible]# ansible-playbook apache.yaml
[WARNING]: While constructing a mapping from /home/whd/ansible/apache.yaml, line 18, column 9, found a
duplicate dict key (name). Using last defined value only.
PLAY [server] ********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [root@192.168.1.11]
TASK [install httpd server] ******************************************************************************
ok: [root@192.168.1.11]
TASK [start httpd] ***************************************************************************************
ok: [root@192.168.1.11]
TASK [isstall php] ***************************************************************************************
ok: [root@192.168.1.11]
TASK [start php-fpm] *************************************************************************************
ok: [root@192.168.1.11]
PLAY RECAP ***********************************************************************************************
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ba95c12dccb07d0442b046ae5fa3f2f.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/afc0321ec9979695ce3b9a8329eba36c.png)