zoukankan      html  css  js  c++  java
  • ansible的简单安装与操作

    ansible的简单介绍

      ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

      ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
    ansible安装
    1、安装epel源
    yum install -y wget # 安装wget
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 下载epel源文件
    
    2、安装ansible
    yum install ansible -y

    ansible的命令格式

    ansible -h
    
    Usage: ansible <host-pattern> [options]
    -a MODULE_ARGS, --args=MODULE_ARGS # 模块的参数
     -C, --check  # 会去执行,但是不做任何的改变,干跑,白跑
     -f FORKS, --forks=FORKS # 指定进程数,做并发
     --list-hosts #列出主机
     -m MODULE_NAME # 模块名称
     --syntax-check #检查语法
     -k, --ask-pass      ask for connection password #指定密码

    查看ansible生成的配置文件

    rmp -ql ansible  #查看ansible生成的配置文件
    
    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles  #空文件
    
    
    进入/etc/ansible/hosts文件,在里面添加ip地址

    ssh认证方式:

      分为密码和密钥

        密钥的认证方式:

          生成密钥:ssh-keygen  (这里可以连续回车就可以)

          ssh-copy-id root@10.0.0.132 # 将秘钥文件复制到远程主机

          此时进行远程登陆时就不需要再输密码.

    验证远程主机连接

      ansible ip地址 -m ping  # 验证是否连接远程主机

    192.168.169.131 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }    #成功

      ansible all -m ping      #验证hosts文件中绑定的所有机器是否已经连接

    192.168.169.131 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.169.134 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.169.132 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }

      ansible ip1,ip2 -m ping    #查看部分机器

    192.168.169.131 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.169.132 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }

      在ansible生成的hosts配置文件中对ip进行分组,如 :

     

      可以对分组内的ip进行的操作如下 :

    ansible web --list-hosts      # 用来获取符合条件的主机
    ansible web -m ping          # 探测组内的机器
    ansible web,db -m ping     # 获取db和web的并集
    ansible 'web:&db' -m ping  # 获取db和web的交集
    ansible 'web:!db' -m ping   # 获取db和web的差集,在web中但是不在db中的
    ansible 'web:db' -m ping    # 获取db和web的并集

      获取ansible中模块的信息 :

        ansible-doc -s command

    -j, --json #以json的方式返回所有模块的信息
    -l # 列出所有的模块
    -s, --snippet # 以片段式显示模块的帮助信息,去掉-s会显示全部信息
    ansible-doc -l |wc -l #统计ansible的模块

    ansible中的部分模块

      1commond模块

        ansible-doc -s command

    argv:            # Allows the user to provide the command as a list vs. a string.  Only the string or the list form can be provided, not both.  One or the other must be provided.
      chdir:           # Change into this directory before running the command.      #切换目录
      creates:         # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be  run.   # 如果存在,就不执行,如果不存在,就执行
      free_form:       # (required) The command module takes a free form command to run.  There is no parameter actually named 'free form'. See the examples!
      removes:         # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.     # 如果不存在,就不执行,如果存在,就执行
      stdin:           # Set the stdin of the command directly to the specified value.
      warn:            # If command_warnings are on in ansible.cfg, do not warn about this particular line if set to `no'.
    ansible web -m command -a "pwd"
    ansible web -m command -a "ls /tmp"
    ansible web -m command -a "chdir=/tmp pwd"    # 切换目录,一般做编译安装
    ansible web -m command -a "creates=/tmp pwd"  # 不被执行,因为/tmp已经存在,
    ansible web -m command -a "creates=/tmp2 pwd" # 被执行,因为/tmp2目录不存在
    ansible web -m command -a "creates=/tmp2 mkdir /data" # 会被执行,因为/tmp2目录不存在
    ansible web -m command -a "removes=/tmp2 pwd"   # 不被执行,因为/tmp2目录不存在
    ansible web -m command -a "removes=/tmp pwd"  # 会被执行,因为/tmp已经存在

      2.shell模块

    ansible web -m shell -a "echo '密码'|passwd --stdin 用户名" # 给用户设置密码
    ansible ip地址 -m shell -a "bash a.sh" # 执行shell脚本
    ansible ip地址 -m shell -a "./a.sh"
    ansible ip地址 -m shell -a "/root/a.sh"
    ansible ip地址 -m shell -a "/root/a.py" # 执行python脚本
    ansible ip地址 -m shell -a "python a.py"

      3.script模块

    ansible db -m script -a "/root/a.sh"                    # 执行的是本地的脚本,管控机上的脚本
    ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判断是远程主机是否存在,如果存在,就不执行,如果不存在,就执行
    ansible db -m script -a "removes=/root/a.sh /root/a.sh" # 判断的主机是否存在,如果存在,就执行,如果不存在,就不执行

      4.copy模块

    backup  # 创建备份文件,以时间戳结尾
    content # 直接写内容
    dest    # 目标地址
    group   #文件的属组
    mode    #文件的权限 W2 R4  X1
    owner  #文件的属主
    src    #原文件
    ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh" # 复制文件
     ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755" # 复制文件,并修改文件的权限
     ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex"  #复制文件,修改文件的权限,属主,根据md5值来判断
     ansible db -m copy -a "src=/etc/init.d dest=/tmp/" # 复制文件夹
     ansible db -m copy -a "src=/etc/init.d/ dest=/tmp/" # 复制文件夹下面的所有文件
     ansible db -m copy -a "src=/etc/init.d dest=/tmp/ owner=alex " # 复制文件夹,并改变文件夹的属性,文件夹的文件的属性也会跟着改变
     ansible db -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/a.sh" # 直接写文字,覆盖写入,要慎用
     ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex backup=yes"  #备份文件,如果远程机器上没有要备份的文件,即使指定了backup=yes 也不会去备份文件

      5.file模块

    access_time # 访问事件
    group  # 属组
    mode   #权限
    owner  #属主
    path   #路径
    src    # 原文件,link和hard的时候使用
    state:
        directory 文件夹
        file
        touch 空文件
        link  软连接
        hard  硬链接
        absent 删除
    ansible db -m file -a "path=/tmp/baoyuan state=directory" # 创建一个目录
    ansible db -m file -a "path=/tmp/baoyuan state=directory owner=alex mode=644" # 创建一个目录,并制定属主,权限
    ansible db -m file -a "path=/tmp/baoyuan.txt state=touch owner=alex mode=644" # 创建一个文件,并制定属主,权限
    ansible db -m file -a "path=/tmp/f src=/etc/fstab state=link" # 创建一个软连接
    ansible db -m file -a "path=/tmp/f state=absent" # 删除
  • 相关阅读:
    Base64的解码和编码
    [WebService]代理类中对枚举类型的序列化
    Linq中的Where与SkipWhile
    Html 5中自定义data*特性
    实用TSQL之生成当前索引数据库中的外键上
    使用Post/Redirect/Get实现Asp.net防止表单重复提交
    用SquishIt最小化Css与Javascript文件
    Asp.net MVC 3 中 Unobtrusive javascript 与Ajax
    HTML5中custom data*特性与asp.net mvc 3 表单验证
    实现Asp.net MVC中AjaxOnly特性
  • 原文地址:https://www.cnblogs.com/lilei1996/p/10679850.html
Copyright © 2011-2022 走看看