一、主机准备

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. oracle报错:ORA-01658(转自52斋347)

    在oracle里创建表,报出错:ORA-01658: 无法为表空间space中的段创建 INITIAL 区:或者: ORA-01658: unable to create INITIAL extent ...

  2. java基础概念整理综合 及补充(jdk1.8)

    2018 java基础 笔记回顾摘要 一 1,html 与 注释: <!--   -->  注释不能嵌套 代码都得有注释. 2,空格符:   3,css选择的优先级: id选择器 > ...

  3. Garmin APP开发之入门

    Garmin开发-入门 先附上几个已经开发完成的app日历 up down 翻月 start 回到当前月(就差农历了) 秒表和定时器一体app界面比较简单,但是实用,长按菜单键可以切换秒表和定时器,有 ...

  4. .net密码找回

    using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using Syst ...

  5. 使用后台程序的第一个表单Form

    参考手册:http://www.yiichina.com/doc/guide/2.0/start-forms 1.创建模型:advanced\backend\models\moxing.php 此模型 ...

  6. linux 命令——4 mkdir (转)

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...

  7. HBuilder发行App(Android和ios)

    怎样将开发好的app测试和上架,此文包括Android和ios打包.测试和上架的大概过程.内容有些简陋,因为此过程踏坑无数,特此留念. 特此声明:内容不全仅供参考. 介绍两个参考网站: 1. http ...

  8. IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变

    监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...

  9. React后台管理系统-file-uploader组件

    1.React文件上传组件github地址: https://github.com/SoAanyip/React-FileUpload 2.Util里边新建file-uploader文件夹,里边新建i ...

  10. 解决使用Application Loader上传ipa提示“上传appstore失败”

    试了好多次使用Application Loader上传ipa,一直提示上传失败,用其他mac电脑却可以,那就是环境有问题,笔者试过重装xcode,都无法解决问题, 查看日志类似是jdk版本问题,换了所 ...