1、环境准备
1.1、主机准备
http1 192.168.10.17
http2 192.168.10.18
1.2、设置免密码登陆
ssh-keygen -t rsa -C root@qq.com -f ~/.ssh/id_rsa -P ""
yum install sshpass -y
sshpass -proot ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@192.168.10.17
sshpass -proot ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@192.168.10.18
1.3、配置主机清单
]# vi /etc/ansible/hosts
[httpd]
192.168.10.17
192.168.10.18
1.4、编写playbood流程
1、定义play
2、定义task、(Installed、Configure、Init、Systemd)
2、编写安装配置 httpd 服务的 playbook 文件
cat << 'CAT_END' > install_httpd.yaml
- hosts: httpd
tasks:
- name: Install httpd Server
yum:
name: httpd
state: present
- name: Configure httpd Server
copy:
src: ./httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
backup: yes
notify: restart httpd server
- name: Init httpd Server
copy:
src: ./index.html.j2
dest: /var/www/html/test.html
- name: Systemd httpd Server
systemd:
name: httpd
state: started
enabled: yes
handlers:
- name: restart httpd server
systemd:
name: httpd
state: restarted
CAT_END
3、检查 playbook 语法
ansible-playbook install_httpd.yaml --syntax-check
4、准备所需要的配置文件
4.1、index.html.j2
echo "This is web test page。" >index.html.j2
4.2、httpd.conf.j2
# 自行到测试机器,拷贝配置文件
scp root@192.168.10.16:/etc/httpd/conf/httpd.conf httpd.conf.j2
# 修改配置文件
sed -i '/^Listen/c Listen 0.0.0.0:80' httpd.conf.j2
5、开始部署
]# ansible-playbook install_httpd.yaml
PLAY [httpd] *************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.18]
ok: [192.168.10.17]
TASK [Install httpd Server] **********************************************************************************************************************************
changed: [192.168.10.18]
changed: [192.168.10.17]
TASK [Configure httpd Server] ********************************************************************************************************************************
changed: [192.168.10.18]
changed: [192.168.10.17]
TASK [Init httpd Server] *************************************************************************************************************************************
changed: [192.168.10.17]
changed: [192.168.10.18]
TASK [Systemd httpd Server] **********************************************************************************************************************************
changed: [192.168.10.18]
changed: [192.168.10.17]
RUNNING HANDLER [restart httpd server] ***********************************************************************************************************************
changed: [192.168.10.17]
changed: [192.168.10.18]
PLAY RECAP ***************************************************************************************************************************************************
192.168.10.17 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.10.18 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6、访问服务器对应的 web 页面测试
~]# curl 192.168.10.17
This is web test page。
]# curl 192.168.10.18
This is web test page。