zoukankan      html  css  js  c++  java
  • Ansible

                                                   临渊羡鱼不如退而结网 

    
    
    

    我在这里只介绍下怎么去使用,具体想要深入了解可以去ansible中文权威指南,很详细http://www.ansible.com.cn/docs/intro_configuration.html


    [root@master .ssh]# cat /etc/issue CentOS release 6.8 (Final) 首先在新的机器上安装epel源 1.epel简介: https://fedoraproject.org/wiki/EPEL/zh-cn rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 以上URL请按实际情况修改 2.查看是否安装成功 rpm -q epel-release 3.导入key: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 4.修改/etc/yum.repos.d/epel.repo文件 在[epel]最后添加一条属性 priority=11 vi /etc/yum.repos.d/epel.repo 意思是yum先去官方源查,官方没有再去epel的源找 5.重建缓存 yum makecache

    这里我们使用yum安装Ansible

    yum install ansible 

    与批量管理机器配置公钥私钥实现无密码登陆,这里我使用自己的一台测试机做示范

    在A机器创建公钥私钥对

    [root@master .ssh]# ssh-keygen -t rsa
    敲三下回车
    把A机器上的id_rsa.pub复制到B机下,在B机的authorized_keys里
    cat id_rsa.pub >> .ssh/authorized_keys
    ssh-copy-id -i ~/.ssh/id_rsa.pub 172.30.1.208 就可以了

    测试下,连接B机器

    成功,不需要输入密码
    [root@master .ssh]# ssh 172.30.1.208
    Last login: Tue Feb 21 10:11:39 2017 from master.1.cn
    [root@client ~]# 

     现在开始修改Ansible配置

    [root@master .ssh]# cat /etc/ansible/hosts 
    # This is the default ansible 'hosts' file.
    #
    # It should live in /etc/ansible/hosts
    #
    #   - Comments begin with the '#' character
    #   - Blank lines are ignored
    #   - Groups of hosts are delimited by [header] elements
    #   - You can enter hostnames or ip addresses
    #   - A hostname/ip can be a member of multiple groups
    
    # Ex 1: Ungrouped hosts, specify before any group headers.
    
    ## green.example.com
    ## blue.example.com
    ## 192.168.100.1
    ## 192.168.100.10
    
    # Ex 2: A collection of hosts belonging to the 'webservers' group
    
    ## [webservers]
    ## alpha.example.org
    ## beta.example.org
    ## 192.168.1.100
    ## 192.168.1.110
    
    # If you have multiple hosts following a pattern you can specify
    # them like this:
    
    ## www[001:006].example.com
    
    # Ex 3: A collection of database servers in the 'dbservers' group
    
    ## [dbservers]
    ## 
    ## db01.intranet.mydomain.net
    ## db02.intranet.mydomain.net
    ## 10.25.1.56
    ## 10.25.1.57
    
    # Here's another example of host ranges, this time there are no
    # leading 0s:
    
    ## db-[99:101]-node.example.com
    [test]               #添加一个test ip为172.30.1.208 , 我要操作的客户端
    172.30.1.208  #

    示例 Ansible有一个非常有用的模块是 command,相信很多人都需要它!Command模块就是“远程执行命令”

     相比Ansible的内置模块而言,Command模块无法通过返回值来判断命令是否执行成功。

     但Ansible添加了creates 和 removes 的属性,以此检查命令是否成功执行或者是否应该被执行。

     如果你定义了 creates 属性,当文件已存在时,它不会再执行。相反如果定义了 removes 属性,

     则只有文件存在的情况下命令才会被执行

    测试执行

    ansible test -m command -a 'date' //command和shell模块可以在被控端执行命令

              (失败了)

    172.30.1.208 | FAILED | rc=0 >>
    failed to resolve remote temporary directory from ansible-tmp-1487642084.32-198317429406356: `( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487642084.32-198317429406356 `" && echo ansible-tmp-1487642084.32-198317429406356="` echo ~/.ansible/tmp/ansible-tmp-1487642084.32-198317429406356 `" )` returned empty string

     

    查找失败原因

    为了提升效率,ansible使用了SSH的ControlPersist、ControlMaster特性。而该特征,应该需要在5.6或以上版本才能实现,Centos6.x上默认版本为5.3,在实际测试过程中,会经常报上面的错误

    解决方案

    1.提高SSH版本,在测试后期,我们换用了Centos7,当然也可以不换操作系统,只升级openssh。(openssh升级脚本:

    http://www.cnblogs.com/shenjianyu/p/6397150.html

    )

    2.禁用该功能特性。

    在配置文件修改

    # vim /etc/ansible/ansible.cfg
    [ssh_connection]
    ssh_args = -o ControlMaster=no -o ControlPersist=no

    再次测试 成功

    [root@master .ssh]# ansible test -m command -a 'date' 
    172.30.1.208 | SUCCESS | rc=0 >>
    Tue Feb 21 10:04:53 CST 2017
    
    [root@master .ssh]#ansible test -m command -a 'mkdir /ssj'
    172.30.1.208 | SUCCESS | rc=0 >>

    编写一个简单的playbook

    下面有一个安装tree命令的脚本

    #!/bin/bash
    # 安装tree
    yum install tree -y

    将其转换为一个完整的playbook后:

    ---
    - hosts: all
    
      tasks:
       - name: "安装tree"
         command: yum install -y tree

    将以上内容放在一个名为playbook.yml的文件中,直接调用ansible-playbook命令,即可运行,运行结果和脚本运行结果一致:

    # ansible-playbook ./playbook.yml
    [root@master ansible]# ansible-playbook ./playbook.yml
    
    PLAY [all] *********************************************************************
    
    TASK [setup] *******************************************************************
    ok: [172.30.1.208]
    
    TASK [安装Apache] ****************************************************************
    changed: [172.30.1.208]
     [WARNING]: Consider using yum module rather than running yum
    
    
    PLAY RECAP *********************************************************************
    172.30.1.208               : ok=2    changed=1    unreachable=0    failed=0  

    再举个例子,拷贝脚本到客户端,执行脚本

    [root@master ansible]# vim /home/touch.sh
    #!/bin/bash
    for i in `seq 10`
    do
    `touch /home/user$i`
    done

    执行playbook报错(失败,貌似是说对应机器没有这个文件)

     1 [root@master ansible]# ansible-playbook ./playbook.yml
     2 
     3 PLAY [all] *********************************************************************
     4 
     5 TASK [setup] *******************************************************************
     6 ok: [172.30.1.208]
     7 
     8 TASK [安装Apache] ****************************************************************
     9 changed: [172.30.1.208]
    10  [WARNING]: Consider using yum module rather than running yum
    11 
    12 
    13 TASK [执行脚本] ********************************************************************
    14 fatal: [172.30.1.208]: FAILED! => {"changed": true, "cmd": ["sh", "/home/touch.sh"], "delta": "0:00:00.003191", "end": "2017-02-22 09:03:14.381231", "failed": true, "rc": 127, "start": "2017-02-22 09:03:14.378040", "stderr": "sh: /home/touch.sh: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
    15         to retry, use: --limit @/etc/ansible/playbook.retry
    16 
    17 PLAY RECAP *********************************************************************
    18 172.30.1.208               : ok=2    changed=1    unreachable=0    failed=1   
     1 [root@master ansible]# ansible test -m copy -a 'src=/home/touch.sh dest=/home'   #-m 指令  -a路径  dest目的路径
     2 172.30.1.208 | SUCCESS => {
     3     "changed": true, 
     4     "checksum": "6678dda6e6661840b5cb135f66053a3d254bd739", 
     5     "dest": "/home/touch.sh", 
     6     "gid": 0, 
     7     "group": "root", 
     8     "md5sum": "f3603ea1b89bcafa6c43809c45a5fa11", 
     9     "mode": "0644", 
    10     "owner": "root", 
    11     "secontext": "unconfined_u:object_r:user_home_dir_t:s0", 
    12     "size": 59, 
    13     "src": "/root/.ansible/tmp/ansible-tmp-1487725606.17-235469749218236/source", 
    14     "state": "file", 
    15     "uid": 0
    16 }
     1 [root@master ansible]# ansible-playbook ./playbook.yml             成功
     2 
     3 PLAY [all] *********************************************************************
     4 
     5 TASK [setup] *******************************************************************
     6 ok: [172.30.1.208]
     7 
     8 TASK [安装Apache] ****************************************************************
     9 changed: [172.30.1.208]
    10  [WARNING]: Consider using yum module rather than running yum
    11 
    12 
    13 TASK [执行脚本] ********************************************************************
    14 changed: [172.30.1.208]
    15 
    16 PLAY RECAP *********************************************************************
    17 172.30.1.208               : ok=3    changed=2    unreachable=0    failed=0   

    在客户端查看下

    [root@client home]# ls
    ansi  ansible  rzrk  sjy  touch.sh  user1  user10  user2  user3  user4  user5  user6  user7  user8  user9

     Ansible 下各模块命令介绍

    copy模块:

        目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上

        命令:ansible 10.1.1.113 -m copy -a 'src=/root/a.sh dest=/tmp/'

    file模块:

        目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root

        命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"

    cron模块:

        目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间

        命令:ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'

    group模块:

        目的:在所有节点上创建一个组名为nolinux,gid为2014的组 

        命令:ansible all -m group -a 'gid=2014 name=nolinux'

    user模块:

        目的:在指定节点上创建一个用户名为nolinux,组为nolinux的用户

        命令:ansible 10.1.1.113 -m user -a 'name=nolinux groups=nolinux state=present'

    yum模块:

        目的:在指定节点上安装 lrzsz 服务

        命令:ansible all -m yum -a "state=present name=httpd"

    service模块:

        目的:启动指定节点上的 puppet 服务,并让其开机自启动

        命令:ansible 10.1.1.113 -m service -a 'name=puppet state=restarted enabled=yes'

    script模块:

        目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)

        命令:ansible 10.1.1.113 -m script -a '/root/a.sh'

    ping模块:

        目的:检查指定节点机器是否还能连通

        命令:ansible 10.1.1.113 -m ping

    command模块:

        目的:在指定节点上运行hostname命令

        命令:ansible 10.1.1.113 -m command -a 'hostname'

    raw模块:

        目的:在10.1.1.113节点上运行hostname命令

        命令:ansible 10.1.1.113 -m raw-a 'hostname|tee'

    get_url模块:

        目的:将http://10.1.1.116/favicon.ico文件下载到指定节点的/tmp目录下

        命令:ansible 10.1.1.113 -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'

    synchronize模块:

        目的:将主控方/root/a目录推送到指定节点的/tmp目录下

        命令:ansible 10.1.1.113 -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'

  • 相关阅读:
    python子网拆分IP段
    动画制作库tween样例学习
    npm淘宝镜像
    nodejs webpack json 大文件,编译,out of memory
    python处理经过gzip压缩的网页内容
    判断百度某一经纬度的地图颜色值python
    考试总结及注意事项
    2152:聪聪可可(点分治)
    3687: 简单题(bitset)
    4514: [Sdoi2016]数字配对
  • 原文地址:https://www.cnblogs.com/shenjianyu/p/6422864.html
Copyright © 2011-2022 走看看