环境说明 :
控制节点:Redhat 8,安装Ansible和所需的python环境,手动关闭firewalld和SELINUX
受控主机:Redhat 7,安装所需的python环境,关闭firewalld和SELINUX
- 关闭firewalld和SELINUX
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld
[root@ansible ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
//保存并退出,然后重启机器即可
- 在控制节点的主机清单中添加受控主机
[root@ansible ~]# vim /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts //去掉注释并修改路径,这里我修改的路径为/etc/ansible/inventory,修改完毕后要手动创建清单文件
[root@ansible ~]# vim /etc/ansible/ansible.cfg
192.168.50.140 ansible_password=redhat //在清单中添加受管主机的IP地址以及远程登陆的密码,可以手动修改远登录的用户,如果不修改默认使用当前ansible主机的用户登录
- 测试连通性
[root@ansible ~]# ansible 192.168.50.140 -m ping
192.168.50.140 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- 修改虚拟主机的配置(使用变量)
[root@ansible ~]# ls
anaconda-ks.cfg hosts test.sh
hehe httpd-vhosts.conf vars
[root@ansible ~]# vim httpd-vhosts.conf
......
<VirtualHost *:{
{ httpd_port1 }}> 把端口改为变量
DocumentRoot "/var/www/html/web1"

本文详细介绍了如何使用Ansible进行自动化部署的过程,包括环境准备、受控主机设置、配置文件修改、虚拟主机配置以及playbook编写与执行。通过具体实例展示了如何实现httpd服务的自动化安装、配置和启动。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



