【ansible】使用ansible安装nginx
一、主机准备
ServerIP:10.10.10.102
ClientIP: 10.10.10.103,10.10.10.104
二、安装ansible
yum -y install ansible
三、配置免密登录
1.在Server上制作公钥和私钥
[root@localhost roles]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:fsarmF7FOVq6/s2ka/VjhEIjbnEcPoQvobEqL1dV63g root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | . | | . o + | | + B o | | o =.X. | | . oSO*o . | | . . ..+*E.o . | | o . .=.+o.o | | . o + +.* + | | o .+.++=.o. . | +----[SHA256]-----+ [root@localhost roles]#
2.将Server上的公钥复制到Client上
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@10.10.10.103
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@10.10.10.104
#写入成功后会在10.10.10.103目录~/.ssh/下生成authorized_keys #验证免密登录是否成功,回车后能登录成功,说明设置成功 ssh root@10.10.10.103
四、编写ansible相关配置文件
1.ansible的目录结构如下
├── ansible.cfg
├── hosts
├── install_nginx.yml
└── roles
└── install_nginx
├── files
│ ├── install_nginx.sh
│ └── nginx-1.14.0.tar.gz
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
2.相关配置文件内容如下:
/etc/ansible/hosts文件:
[Client] 10.10.10.103 10.10.10.104
/etc/ansible/install_nginx.yml
[root@CentOS7 ansible]# more install_nginx.yml --- - hosts: Client roles: - install_nginx
/etc/ansible/roles/install_nginx/files/install_nginx.sh
[root@CentOS7 files]# more install_nginx.sh #!/bin/bash cd /usr/local/nginx-1.14.0 ./configure --prefix=/usr/local/nginx make && make install /usr/local/nginx/sbin/nginx
/etc/ansible/roles/install_nginx/tasks/main.yml
[root@CentOS7 tasks]# more main.yml --- - name: Install Software yum: name={{ item }} state=latest with_items: - gcc - gcc-c++ - zlib-devel - pcre-devel - openssl - openssl-devel - name: Copy nginx.tar.gz copy: src=nginx-{{ nginx_version }}.tar.gz dest={{ nginx_dir }}/nginx-{{ nginx_version }}.tar.gz owner=root group=root - name: Copy install_nginx.sh copy: src=install_nginx.sh dest=/tmp/install_nginx.sh - name: uncompress nginx.tar.gz shell: tar -xf {{ nginx_dir }}/nginx-{{ nginx_version }}.tar.gz -C {{ nginx_dir }}/ - name: install nginx shell: /bin/bash /tmp/install_nginx.sh
/etc/ansible/roles/install_nginx/vars/main.yml
[root@CentOS7 vars]# more main.yml nginx_dir: /usr/local nginx_version: 1.14.0
五、使用ansible安装nginx并启动
[root@CentOS7 ansible]# cd /etc/ansible
[root@CentOS7 ansible]# ansible-playbook install_nginx.yml PLAY [Client] ****************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************* ok: [10.10.10.103] ok: [10.10.10.104] TASK [install_nginx : Install Software] **************************************************************************************************************************** ok: [10.10.10.103] => (item=[u'gcc', u'gcc-c++', u'zlib-devel', u'pcre-devel', u'openssl', u'openssl-devel']) ok: [10.10.10.104] => (item=[u'gcc', u'gcc-c++', u'zlib-devel', u'pcre-devel', u'openssl', u'openssl-devel']) TASK [install_nginx : Copy nginx.tar.gz] *************************************************************************************************************************** changed: [10.10.10.103] changed: [10.10.10.104] TASK [install_nginx : Copy install_nginx.sh] *********************************************************************************************************************** changed: [10.10.10.103] changed: [10.10.10.104] TASK [install_nginx : uncompress nginx.tar.gz] ********************************************************************************************************************* [WARNING]: Consider using unarchive module rather than running tar changed: [10.10.10.103] changed: [10.10.10.104] TASK [install_nginx : install nginx] ******************************************************************************************************************************* changed: [10.10.10.103] changed: [10.10.10.104] PLAY RECAP ********************************************************************************************************************************************************* 10.10.10.103 : ok=6 changed=4 unreachable=0 failed=0 10.10.10.104 : ok=6 changed=4 unreachable=0 failed=0
六、验证
在安装完成后,可以到Client验证nginx是否安装完成!
如下是在10.10.10.104上的验证结果:
[root@CentOS7 local]# ps -ef|grep nginx root 35688 1 0 04:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 35689 35688 0 04:24 ? 00:00:00 nginx: worker process root 43485 19737 0 18:34 pts/2 00:00:00 grep --color=auto nginx [root@CentOS7 local]#
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥