一、主机准备

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. 用weex create 创建app项目 报 ERROR in index.web.js from UglifyJs 错误!

    用weex create创建一个APP项目,安装依赖后运行报 这个是package.json index.web.js 在dist目录下是build时生成的. 上面的答案没有给大家细节,不好意思致歉下 ...

  2. js 数组操作常用方法

    push():在数组后面加入元素,并返回数组的长度: unshift():在数组前面就如元素,并返回数组的长度: pop():删除最后一个元素: var arr =[1,2,3,4,5] ; arr. ...

  3. Spring MVC的高级配置

    1.文件上传配置 文件上传是项目中常用的一个功能,Spring MVC通过配置一个MultipartResolver来上传文件. 在Spring的控制器中,通过MultipartFile file 来 ...

  4. 使用tooltip显示jquery.validate.unobtrusive验证信息

    通过重写CSS实现使用tooltip显示jquery.validate.unobtrusive验证信息,效果如图: 1. 在ViewModel中定义验证规则 [Display(Name = " ...

  5. 慎用python的pop和remove方法

    申明:转载请注明出处!!! Python关于删除list中的某个元素,一般有两种方法,pop()和remove(). 如果删除单个元素,使用基本没有什么问题,具体如下. 1.pop()方法,传递的是待 ...

  6. linux 命令——44 top (转)

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是 一个动态显示过程,即可以通过用户按键来不断刷 ...

  7. 用TextKit实现表情混排

      Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们 ...

  8. python剑指offer 合并两个排序的链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. # -*- coding:utf-8 -*- # class ListNode: # def _ ...

  9. systemd 中的requires, wants, before, after

    man systemd.unit    man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...

  10. MyBatis的discriminator鉴别器根据字段值实现Java中的多态

    <select id="getModelById" resultMap="modelTypeMap"> SELECT id as id, model ...