使用 ansible 安装 NGINX
官方文档:https://docs.ansible.com/ansible/latest/index.html
1.修改主机名
hostname master1 #192.168.67.11
hostname master2 #192.168.67.12
2.ssh-keygen做免密
[root@master1 etc]# ssh-keygen -t rsa
一路回车
[root@master1 etc]# ssh-copy-id root@192.168.67.12
3.添加域名解析
[root@master1 etc]# cat <<EOF >> /etc/hosts
192.168.67.11 master1
192.168.67.12 master2
EOF
[root@master1 etc]# ssh root@192.168.67.12
[root@master2 etc]# cat <<EOF >> /etc/hosts
192.168.67.11 master1
192.168.67.12 master2
EOF
yum安装epel源和ansible #参考官方安装文档:https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-rhel-centos-or-fedora
[root@master1 etc]# yum install -yq epel-release && sudo yum install -yq ansible
[root@master1 ansible-nginx]# ansible --version
ansible 2.9.25
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
编写写安装nginx的yml文件 #参考示例:https://github.com/ansible/ansible/blob/stable-2.11/examples/ansible.cfg 示例:https://github.com/ansible/ansible-examples
[root@master1 etc]# mkdir $HOME/ansible-nginx/tasks/
[root@master1 etc]# cat <<EOF > $HOME/ansible-nginx/deploy.yml
- hosts: nginx
tasks:
- include: 'tasks/install_nginx.yml'
EOF
[root@master1 etc]# cat <<EOF > $HOME/ansible-nginx/tasks/install_nginx.yml
- name: NGINX | Installing NGINX repo rpm
yum:
name: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- name: NGINX | Installing NGINX
yum:
name: nginx
state: latest
- name: NGINX | Starting NGINX
service:
name: nginx
state: started
EOF
[root@master1 etc]# mv /etc/ansible/hosts /etc/ansible/hosts.backup
[root@master1 etc]# cat <<EOF > /etc/ansible/hosts
[nginx]
master2
EOF
Ansible 提示输入 SSH 密码,输出如下。recap 中显示 failed=0 这条信息,意味着部署成功了。
[root@master1 etc]# ansible-playbook --ask-pass $HOME/ansible-nginx/deploy.yml
SSH password:
PLAY [nginx] ************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [master2]
TASK [NGINX | Installing NGINX repo rpm] ********************************************************************************************
changed: [master2]
TASK [NGINX | Installing NGINX] *****************************************************************************************************
changed: [master2]
TASK [NGINX | Starting NGINX] *******************************************************************************************************
changed: [master2]
PLAY RECAP **************************************************************************************************************************
master2 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看效果
[root@master2 kubelet]# ps -ef | grep nginx
root 48337 1 0 15:04 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 48338 48337 0 15:04 ? 00:00:00 nginx: worker process
nginx 48339 48337 0 15:04 ? 00:00:00 nginx: worker process
nginx 48340 48337 0 15:04 ? 00:00:00 nginx: worker process
nginx 48341 48337 0 15:04 ? 00:00:00 nginx: worker process
root 48480 14520 0 15:05 pts/1 00:00:00 grep --color=auto nginx