yum --enablerepo=aliyun_epel install ansible -y
ssh-keygen -P "" -t rsa -f /root/.ssh/id_rsa &> /dev/null
ssh-copy-id 192.168.43.29
ssh-copy-id 192.168.43.39
ssh-copy-id 192.168.43.49
mkdir -pv /project/playbook/roles/httpd/{files,handlers,templates,vars,tasks}
1) 网站的默认页面 /project/playbook/roles/httpd/files/index.html
www.xuepeng.com
2) /project/playbook/roles/httpd/handlers/main.yml 触发动作
- name: restart
service: name=httpd state=restarted
- name: check
shell: killall -0 httpd > /tmp/httpd.log
3 ) 执行的任务
install.yml
- name: install httpd
yum: name=httpd state=present
config7.yml
- name: config7
template: src=httpd7.conf.j2 dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version=="7"
notify:
- restart
- check
index.yml
- name: default page
copy: src=index.html dest=/var/www/html/
service.yml
- name: service
service: name=httpd state=started
main.yml
- include: install.yml
- include: config7.yml
- include: index.yml
- include: service.yml
4 ) 模板文件 /project/playbook/roles/httpd/templates/httpd7.conf.j2
将/etc/httpd/conf/httpd.conf 文件拷贝一份为 httpd7.conf.j2
Listen { {httpd7_port}}
5 ) 变量 /project/playbook/roles/httpd/vars/main.yml
httpd7_port: 8080
5 ) roles 文件
/project/playbook/httpd.role.yml
- hosts: httpdsrvs
remote_user: root
roles:
- httpd
(1)www.xuepeng.com,页面文件目录为/web/vhosts/xuepeng;错误日志为
/var/log/httpd/xuepeng.err,访问日志为/var/log/httpd/xuepeng.access
(2)www.pengpeng.com,页面文件目录为/web/vhosts/pengpeng;错误日志为 /var/log/httpd/pengpeng.err,访问日志为/var/log/httpd/pengpeng.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
yum install httpd -y
vim /etc/httpd/conf.d/www.xuepeng.com.conf
ServerName www.xuepeng.com
DocumentRoot "/web/vhosts/xuepeng"
ErrorLog "/var/log/httpd/xuepeng.err"
CustomLog "/var/log/httpd/xuepeng.access" combined
AllowOverride None
# Allow open access:
Require all granted
mkdir /web/vhosts/xuepeng/ -pv
echo `hostname` > /web/vhosts/xuepeng/index.html
httpd -t
systemctl start httpd
cp -p www.xuepeng.com.conf www.pengpeng.com.conf
vim www.pengpeng.com.conf
:%s@xuepeng@pengpeng@g
mkdir /web/vhosts/pengpeng
httpd -t
systemctl restart httpd
echo `hostname` > /web/vhosts/pengpeng/index.html