Ansible 是一个配置管理和应用部署工具,功能类似于目前业界的配置管理工具 Chef,Puppet,Saltstack。Ansible 是通过 Python 语言开发。Ansible 平台由 Michael DeHaan 创建,他同时也是知名软件 Cobbler 与 Func 的作者。Ansible 的第一个版本发布于 2012 年 2 月。Ansible 默认通过 SSH 协议管理机器,所以 Ansible 不需要安装客户端程序在服务器上。您只需要将 Ansible 安装在一台服务器,在 Ansible 安装完后,您就可以去管理控制其它服务器。不需要为它配置数据库,Ansible 不会以 daemons 方式来启动或保持运行状态。Ansible 可以实现以下目标:

自动化部署应用
自动化管理配置
自动化的持续交付
自动化的(AWS)云服务管理。

根据 Ansible 官方提供的信息,当前使用 Ansible 的用户有:evernote、rackspace、NASA、Atlassian、twitter 等。

1、分别配置各自主机名,并配置hosts文件(能互相解析)

8.20、8.39、8.40
#vim /etc/hosts

# hostname node1.chinasoft.com
vim /etc/hosts
192.168.8.20 node1.chinasoft.com node1
192.168.8.39 node2.chinasoft.com node2

2、在ansible服务器8.40上配置ssh免密码访问

# ssh-keygen

# ssh-copy-id -i .ssh/id_rsa.pub root@node1.chinasoft.com
# ssh-copy-id -i .ssh/id_rsa.pub root@node2.chinasoft.com
测试是否成功
# ssh node1.chinasoft.com 'date';date
# ssh node2.chinasoft.com 'date';date

3、安装ansible服务
# yum install -y epel-relase
# yum install -y ansible1.9

配置服务器组
# vim /etc/ansible/hosts

[webservers]
node1.chinasoft.com
node2.chinasoft.com

[dbserver2]
node1.chinasoft.com
node2.chinasoft.com


4、常用服务及模块的使用
查看命令的帮助文档,如copy
# ansible-doc -s copy

简单的命令测试:
①ping响应
# ansible all -m ping
node2.chinasoft.com | success >> {
    "changed": false, 
    "ping": "pong"
}

node1.chinasoft.com | success >> {
    "changed": false, 
    "ping": "pong"
}
②各服务器时间
# ansible all -a 'date'
node2.chinasoft.com | success | rc=0 >>
Mon Apr 18 20:43:48 CST 2016

node1.chinasoft.com | success | rc=0 >>
Mon Apr 18 20:43:48 CST 2016

③文件拷贝
# ansible dbservers -m copy -a "src=/etc/fstab dest=/root/fstab"
验证拷贝是否成功
# ansible dbservers -a "ls /root"
node1.chinasoft.com | success | rc=0 >>
anaconda-ks.cfg
fstab
install.log
install.log.syslog

node2.chinasoft.com | success | rc=0 >>
anaconda-ks.cfg
fstab
install.log
install.log.syslog

④添加计划任务
# ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 192.168.8.102"'
node2.chinasoft.com | success >> {
    "changed": false, 
    "jobs": [
        "customjob", 
        "custom job"
    ]
}

node1.chinasoft.com | success >> {
    "changed": true, 
    "jobs": [
        "custom job"
    ]
}

# ansible all -a "crontab -l"
node1.chinasoft.com | success | rc=0 >>
#Ansible: custom job
*/3 * * * * /usr/sbin/ntpdate 192.168.8.102

node2.chinasoft.com | success | rc=0 >>
#Ansible: customjob
*/3 * * * * /usr/sbin/ntpdate 192.168.8.102

④在节点中添加组
# ansible-doc -s group

action: group
      gid                    # Optional `GID' to set for the group.
      name=                  # Name of the group to manage.
      state                  # Whether the group should be present or not on the remote host.
      system                 # If `yes', indicates that the group created is a system group.
添加mysql组
# ansible all -m group -a "gid=306 system=yes name=mysql"
node1.chinasoft.com | success >> {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}

node2.chinasoft.com | success >> {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}

# ansible all -a "tail -1 /etc/group"
node2.chinasoft.com | success | rc=0 >>
mysql:x:306:

node1.chinasoft.com | success | rc=0 >>
mysql:x:306:

⑥yum命令的使用
# ansible-doc -s yum
  action: yum
      conf_file              # The remote yum configuration file to use for the transaction.
      disable_gpg_check      # Whether to disable the GPG checking of signatures of packages being installed. Has an effect only if state is `present' or `latest'.
      disablerepo            # `Repoid' of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. When specifying multiple repos, separ
      enablerepo             # `Repoid' of repositories to enable for the install/update operation. These repos will not persist beyond the transaction. When specifying multiple repos, separa
      list                   # Various (non-idempotent) commands for usage with `/usr/bin/ansible' and `not' playbooks. See examples.
      name=                  # Package name, or package specifier with version, like `name-1.0'. When using state=latest, this can be '*' which means run: yum -y update. You can also pass a u
      state                  # Whether to install (`present', `latest'), or remove (`absent') a package.
      update_cache           # Force updating the cache. Has an effect only if state is `present' or `latest'.

安装httpd软件
# ansible all -m yum -a "name=httpd state=present"
查看是否安装了httpd
# ansible all -a "rpm -q httpd"
node1.chinasoft.com | success | rc=0 >>
httpd-2.2.15-47.el6.centos.4.x86_64

node2.chinasoft.com | success | rc=0 >>
httpd-2.2.15-47.el6.centos.4.x86_64

⑦查看服务状态
# ansible all -a "service httpd status"
node2.chinasoft.com | FAILED | rc=3 >>
httpd 已停

node1.chinasoft.com | FAILED | rc=3 >>
httpd 已停

启动httpd服务,并设置开机自启动
# ansible all -m service -a "state=started enabled=yes name=httpd"
node1.chinasoft.com | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}

node2.chinasoft.com | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}

# ansible all -a "service httpd status"
node1.chinasoft.com | success | rc=0 >>
httpd (pid  2575) 正在运行...

node2.chinasoft.com | success | rc=0 >>
httpd (pid  2371) 正在运行...

校验是否开机自启动
# ansible all -a "chkconfig --list httpd"
node1.chinasoft.com | success | rc=0 >>
httpd          0:off1:off2:on3:on4:on5:on6:off

node2.chinasoft.com | success | rc=0 >>
httpd          0:off1:off2:on3:on4:on5:on6:off

5、剧本的简单使用
①通过脚本添加组
# vim test.yaml
- hosts: all
  remote_user: root
  tasks:
    - name: add a group
      group: gid=1000 name=testgroup1 system=no
    - name: execute a commond
      command: /bin/date

执行剧本
# ansible-playbook test.yaml

PLAY [all] ********************************************************************

GATHERING FACTS *************************************************************** 
ok: [node2.chinasoft.com]
ok: [node1.chinasoft.com]

TASK: [add a group] *********************************************************** 
changed: [node2.chinasoft.com]
changed: [node1.chinasoft.com]

TASK: [execute a commond] ***************************************************** 
changed: [node2.chinasoft.com]
changed: [node1.chinasoft.com]

PLAY RECAP ******************************************************************** 
node1.chinasoft.com        : ok=3    changed=2    unreachable=0    failed=0   
node2.chinasoft.com        : ok=3    changed=2    unreachable=0    failed=0

②通过脚本修改httpd配置文件,修改端口为8080
# vim web.yaml

- hosts: all
  remote_user: root
  tasks:
    - name: ensure apache latest version
      yum: state=latest name=httpd
    - name: apache configure file
      copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes
      notify:
        - restart httpd
  handlers:
      - name: restart httpd
        service: name=httpd state=restarted

执行剧本

# ansible-playbook web.yaml

ansible的安装部署及简单应用的更多相关文章

  1. saltstack安装部署以及简单实用

    一,saltstack简介:     SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流,以毫秒为单位. SaltSta ...

  2. dubbo-admin管理控制台的安装部署(最简单)

    Dubbo-admin最简单的安装部署,十分钟就能搞定! 网上找的安装教程虽说详细,但是就是因为详细操作起来而显得繁琐.今天,我帮大家跳过这些繁琐的步骤,简单快捷的安装部署dubbo-admin. 1 ...

  3. 02_ Flume的安装部署及其简单使用

    一.Flume的安装部署: Flume的安装非常简单,只需要解压即可,当然,前提是已有hadoop环境 安装包的下载地址为:http://www-us.apache.org/dist/flume/1. ...

  4. elasticsearch kibana的安装部署与简单使用(一)

    1.先说说es 我早两年使用过es5.x的版本,记得当时部署还是很麻烦,因为es是java写的,要先在机器上部署java环境jvm之类的一堆东西,然后才能安装es 但是现在我使用的是目前最新的7.6版 ...

  5. Ansible Ubuntu 安装部署

    一.安装: $ sudo apt-get install ansible 二.配置: a.基本配置 $ cd /etc/ansible/ $ sudo cp hosts hosts_back 备份一个 ...

  6. HBase的安装部署以及简单使用

    一:下载安装 1.下载安装 2.开启hadoop与zookeeper 3.修改配置文件hbase-env export JAVA_HOME=/opt/modules/jdk1.7.0_67 expor ...

  7. 071 HBase的安装部署以及简单使用

    一:下载安装 1.下载安装 2.开启hadoop与zookeeper 3.修改配置文件hbase-env export JAVA_HOME=/opt/modules/jdk1.7.0_67 expor ...

  8. 大数据(13) - Spark的安装部署与简单使用

    一 .Spark概述 官网:http://spark.apache.org 1.        什么是spark Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校 ...

  9. Hive安装部署及简单测试 网页《一》

    1.首先关闭机器上之前配置的分布式Hadoop 命令: (在hadoop的安装目录中)  sbin/stop-dfs.sh              关闭: yarn   命令:  sbin/stop ...

随机推荐

  1. Ubuntu16.04创建electronic-wechat启动器图标

    步骤 将最新的electronic-wechat二进制包,下载解压,并移动到/opt/下面.sudo cp -r electronic-wechat/ /opt/electronic-wechat/ ...

  2. 2019.3.18考试&2019.3.19考试&2019.3.21考试

    2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...

  3. Luogu 2540 斗地主增强版(搜索,动态规划)

    Luogu 2540 斗地主增强版(搜索,动态规划) Description 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游 ...

  4. Flash:使用FileReference上传在Firefox上遇到的问题终于解决了

    以前使用的是这样的一句话:var uploadURL:URLRequest = new URLRequest();uploadURL.url = "upload.asp"; 测试发 ...

  5. react与fetch

    JavaScript 中的 ajax 很早之前就有一个诟病----复杂业务下的 callback 嵌套的问题.promise 正是 js 中解决这一问题的钥匙. 接下来我们在react项目中应用到的f ...

  6. Missing artifact com.github.pagehelper:pagehelper:jar:3.4.2-fix的解决方法(最简单的方法)

    在网上看的淘淘商城的项目,自己在配置maven项目的时候遇见了这个异常,按照网上教程试了试,一重启各种异常. 后来直接,就更改了自己的maven仓库就ok了. 解决方法: 对比一下,你就能够发现问题, ...

  7. mysql命令备份和还原

    1.导出整个数据库 mysqldump -u用户名 -p密码  数据库名 > 导出的文件名  C:\Users\jack> mysqldump -uroot -pmysql sva_rec ...

  8. CentOS6.x下源码安装MySQL5.5

    1. 更新yum源:http://www.cnblogs.com/vurtne-lu/p/7405931.html 2. 卸载原有的mysql数据库 [root@zabbix ~]# yum -y r ...

  9. UVA - 12487 Midnight Cowboy(LCA+思维)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. 矩阵NumPy

    常量: np.pi π 创建矩阵数组 import numpy as np # array=np.array([[1,2,3],[5,6,7]]) #定义一个2行3列的矩阵数组.2行=2维 # pri ...