文章目录
关闭firewalld和SELINUX
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld
在控制节点的主机清单中添加受控主机
[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 ~]# vi /etc/ansible/inventory
192.168.194.132 ansible_password=redhat
192.168.194.133 ansible_password=redhat
192.168.194.134 ansible_password=redhat
[root@localhost ~]# echo 'redhat' |passwd --stdin root
更改ansible配置文件
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim ansible.cfg
// 在ansible.cfg里添加一行
inventory = /etc/ansible/inventory
测试连通性
[root@ansible ~]# ansible 192.168.194.139 -m ping
192.168.194.139 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
创建项目目录,方便管理
[root@localhost ~]# tree httpd
httpd
├── files
│ └── httpd-vhosts.conf
├── group-vars
├── host-vars
├── playbook.yml
└── vars
├── httpd_port1
└── httpd_port2
5 directories, 3 files
复制并配置yum源
---
- hosts: all
name: download CentOS 7 yum
tasks:
- name: copy
copy:
src: /tmp/yum/yum.repos.d/
dest: /etc/yum.repos.d/
- name: 修改yum源的配置文件
hosts: all
tasks:
- name: 修改
command: sed -i 's/8/7/g' /etc/yum.repos.d/CentOS-Base.repo
安装httpd服务
[root@ansible ~]# vim gg.yml
---
-name: install httpd
hosts: 192.168.194.139
tasks:
-name: install http
yum:
name: httpd
state: present
[root@localhost ~]# ansible-playbook gg.yml
PLAY [192.168.194.139] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
TASK [install httpd] ***********************************************************
changed: [192.168.194.139]
PLAY RECAP *********************************************************************
192.168.194.139 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ~]#
修改httpd的配置文件
---
hosts: 192.168.194.132
tasks:
-name: change configuration file
command: sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf
添加站点
[root@localhost ~]# vim httpd.yml
[root@localhost ~]# cat httpd.yml
---
- name: change configuration file
hosts: 192.168.194.139
tasks:
- name: change configuration file
command: sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf
- name: add VirtualHost
hosts: 192.168.194.139
tasks:
- name: add VirtualHost
shell: cd /var/www/html/ && mkdir test xxx && cd test && echo "hello tom" > index.html && cd ../xxx && echo "helo cwt" > index.html
这一步在localhost上手动编写虚拟主机配置文件
[root@localhost ~]# vim httpd-vhosts.j2
<VirtualHost *:{{ httpd_port1 }}>
DocumentRoot "/var/www/html/{{ web1 }}"
ServerName {{ web1 }}.example.com
ErrorLog "/var/log/httpd/{{ web1 }}.example.com-error_log"
CustomLog “/var/log/httpd/{{ web1 }}.example.com-access_log" common
</VirtualHost>
Listen {{ httpd_port2 }}
<VirtualHost *:{{ httpd_port2 }}>
DocumentRoot "/var/www/html/{{ web2 }}"
ServerName {{ web2 }}.example.com
ErrorLog "/var/log/httpd/{{ web2 }}.example.com-error_log"
CustomLog "/var/log/httpd/{{ web2 }}.example.com-access_log" common
</VirtualHost>
启动服务
- name: start httpd
hosts: 192.168.194.139
tasks:
- name: start httpd
service:
name: httpd
state: started
enabled: yes