一、主机准备

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 ]----+
| . |
| . 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.
./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.

五、使用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= changed= unreachable= failed=
10.10.10.104 : ok= changed= unreachable= failed=

六、验证

在安装完成后,可以到Client验证nginx是否安装完成!

如下是在10.10.10.104上的验证结果:

[root@CentOS7 local]# ps -ef|grep nginx
root : ? :: nginx: master process /usr/local/nginx/sbin/nginx
nobody : ? :: nginx: worker process
root : pts/ :: grep --color=auto nginx
[root@CentOS7 local]#

【ansible】使用ansible安装nginx的更多相关文章

  1. Ansible 使用 Playbook 安装 Nginx

    思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发 [root@localhost ~]$ cd /etc/ansible/ [root@localhost ansibl ...

  2. ansible roles实践——安装nginx

    1.创建roles 在/etc/ansible/roles目录下 1.1 手动创建需要的目录 1.2 使用命令创建,用不到的目录可以创建为空目录,但不可以不创建. 创建目录[root@master] ...

  3. ansible的playbook进行yum批量安装nginx最新版本

    环境:centos7 版本:nginx最新版本 软件: ansible 作用: 进行批量执行不同机器上,进行安装nginx版本 检查脚本是否正确: [root@ansible-test ansible ...

  4. ansible安装nginx

    ansible安装nginx(实现回滚发布功能:下一篇博客.没想到写长了) 一.准备工作 1.准备两台机器 sai: 192.168.131.132  ——> ansible的服务端 luojy ...

  5. ansible案例-安装nginx

    一.创建目录: mkidr -p playbook/{files,templates}   二.自定义index.html文件 $ vim playbook/templates/index.html. ...

  6. 使用Ansible安装部署nginx+php+mysql之安装nginx(1)

    使用Ansible安装nginx 1.nginx.yaml文件 --- - hosts: clong remote_user: root gather_facts: no tasks: # 安装epe ...

  7. Ansible 入门指南 - 安装及 Ad-Hoc 命令使用

    安装及配置 ansible Ansilbe 管理员节点和远程主机节点通过 SSH 协议进行通信.所以 Ansible 配置的时候只需要保证从 Ansible 管理节点通过 SSH 能够连接到被管理的远 ...

  8. ansible指路篇-安装及基本命令使用

    ansible指路篇-安装及基本命令使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是ansible ansible是新出现的自动化运维工具,基于Python开发,集合 ...

  9. ansible示例,离线安装etcd

    一.基础介绍 ========================================================================================== 1. ...

随机推荐

  1. spring mvc <mvc;resources>

    spring mvc 的<mvc;resources mapping="***" location="***">标签是在spring3.0.4出现的 ...

  2. Spring @Autowired使用介绍

    参考博客: https://blog.csdn.net/u013412772/article/details/73741710 引用文章地址: https://my.oschina.net/Helio ...

  3. Android自定义控件练手——简单的时钟

    首先这应该是一个老生常谈的设计了,但是毕竟身为小白的自己都没动手做过,不动手怎么提高自己呢,所以在这梅林沉船闲暇之际,我就把我的设计流程与思路记录下来.首先来看看效果图吧: 如上图就是一个简单并没有美 ...

  4. 飞塔NGFW-FortiGate-5.2(BYOL)

    平台: FortiGate 类型: 虚拟机镜像 软件包: basic software FortiGate ips security UTM vpn 反垃圾邮件 网页过滤 服务优惠价: 按服务商许可协 ...

  5. mybatis 异常处理:Invalid bound statement (not found)

    mybatis 的使用过程中提示错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ...

  6. Jsoup查找dom元素

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  7. linux 命令——39 grep (转)

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  8. 基于Dockerfile 构建redis5.0.0(包括持久化)及RedisDestopManager 监控

    一 创建Dockerfile [root@zxmrlc docker]# mkdir redis [root@zxmrlc docker]# cd redis && touch Doc ...

  9. 从暴力匹配到KMP算法

    前言 现在有两个字符串:\(s1\)和\(s2\),现在要你输出\(s2\)在\(s1\)当中每一次出现的位置,你会怎么做? 暴力匹配算法 基本思路 用两个指针分别指向当前匹配到的位置,并对当前状态进 ...

  10. [超详细] Python3爬取豆瓣影评、去停用词、词云图、评论关键词绘图处理

    爬取豆瓣电影<大侦探皮卡丘>的影评,并做词云图和关键词绘图第一步:找到评论的网页url.https://movie.douban.com/subject/26835471/comments ...