zoukankan      html  css  js  c++  java
  • ansible基础元素

    一.模块安装

    #服务端
    
    yum install ansible
    
    #客户端
    
    yum install -y libselinux-python (被监控的机器可安装)
    
     

    二. 常用模块

    command

      不指定模块默认command模块

      #chdir---在执行这个命令前,先切换目录  
    
      [root@m01 ansible]# ansible 172.16.1.31 -m command -a "chdir=/etc/  pwd"
      172.16.1.31 | SUCCESS | rc=0 >>
      /etc
    
      #creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
      [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf hostname"
      172.16.1.41 | SUCCESS | rc=0 >>
      skipped, since /etc/rsyncd.conf exists
    
      [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.123456 hostname"
      172.16.1.41 | SUCCESS | rc=0 >>
      backup
    
      #参数:removes---判断一个文件是否存在,如果不存在,后面的命令就不会执行
      [root@m01 ansible]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.conf hostname"
      172.16.1.41 | SUCCESS | rc=0 >>
      backup
    
      [root@m01 ansible]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.1212213123 hostname"
      172.16.1.41 | SUCCESS | rc=0 >>
      skipped, since /etc/rsyncd.1212213123 does not exist
    
      #参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息
      ansible 172.16.1.41 -m command -a "ls"
      172.16.1.41 | SUCCESS | rc=0 >>
      anaconda-ks.cfg
      dead.letter

    shell模块(万能模块)
      参数:chdir---在执行这个命令前,先切换目录
      参数:creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
      参数:removes---判断一个文件是否存在,如果不存在,后面的命令就不会执行
      参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息

      [root@m01 ansible]# ansible 172.16.1.41 -m shell -a "ls;pwd"
      172.16.1.41 | SUCCESS | rc=0 >>
      anaconda-ks.cfg


      说明:shell模块可以满足command模块所有功能,并且可以支持识别特殊字符信息 < > | ;

    script---专门运行脚本模块
      参数:chdir---在执行莫个命令前,先切换目录
      参数:creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
      参数:removes---判断一个文件是否存在,如果不存在,后面的命令就不会执行
      参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息

    文件类型模块:
    copy----复制模块
      参数:backup---对数据信息进行备份

    [root@m01 ansible]# ansible 172.16.1.41 -m copy -a "src=/tmp/file01.txt dest=/tmp/ backup=yes"
    172.16.1.41 | SUCCESS => {
    "backup_file": "/tmp/file01.txt.71887.2018-04-02@23:33:19~", 
    "changed": true, 
    "checksum": "029b054db136cc36d5605e3818305825ff4b8ffb", 
    "dest": "/tmp/file01.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "434660b5ad7deeba8815349f71409405", 
    "mode": "0644", 
    "owner": "root", 
    "size": 6, 
    "src": "/root/.ansible/tmp/ansible-tmp-1522683197.05-52744169892601/source", 
    "state": "file", 
    "uid": 0
    }
      #参数:src---定义要推送数据信息
      #参数:dest---定义将数据推送到远程主机什么目录中
    [root@m01 ansible]# touch /tmp/file01.txt
    [root@m01 ansible]# ansible 172.16.1.41 -m copy -a "src=/tmp/file01.txt dest=/tmp/"
    172.16.1.41 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/file01.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1522682948.27-60532389065095/source", 
    "state": "file", 
    "uid": 0
    }
    [root@m01 ansible]# ansible 172.16.1.41 -m shell -a "ls -l /tmp/"
    172.16.1.41 | SUCCESS | rc=0 >>
    total 24
    -rw-r--r-- 1 root root 0 Apr 2 23:29 file01.txt

    file----文件属性修改/目录创建/文件创建
      参数:owner---设置复制后的文件属主权限
      参数:group---设置复制后的文件属组权限
      参数:mode---设置复制后的文件权限(600 755)
      

    ansible 172.16.1.41 -m file -a "dest=/tmp/file01.txt owner=oldboy group=oldboy mode=600"
    172.16.1.41 | SUCCESS => {
    "changed": true, 
    "gid": 500, 
    "group": "oldboy", 
    "mode": "0600", 
    "owner": "oldboy", 
    "path": "/tmp/file01.txt", 
    "size": 6, 
    "state": "file", 
    "uid": 500
    }
    
    参数:state---用于指定创建目录或文件
    创建文件
    ansible 172.16.1.41 -m file -a "dest=/tmp/file01.txt state=touch"
    172.16.1.41 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/file01.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
    }
    
    创建目录:
    ansible 172.16.1.41 -m file -a "dest=/tmp/dir01 state=directory"
    172.16.1.41 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir01", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
    }

    包管理模块类型
    yum---安装软件包模块

    #name:执行要安装软件的名称,以及软件的版本
    #state:installed安装 absent(卸载)
    ansible 172.16.1.41 -m yum -a "name=iftop state=installed"
    ansible 172.16.1.41 -m yum -a "name=iftop state=absent"
    
    list:指定软件名称,查看软件是否可以安装,以及是否已经安装过了
    ansible 172.16.1.41 -m yum -a "list=iftop"

    系统模块类型
    service---管理服务状态模块
      name: 指定要管理的服务名称(管理的服务一定在chkconfig中可以看到)
      state:stopped started restarted reloaded
      enabled:yes表示服务开机自启动 no表示服务开机不要自动启动

    ansible 172.16.1.41 -m service -a "name=crond state=started enabled=yes"

    cron---定时任务模块

    * * * * * /bin/sh /server/scripts/test.sh &>/dev/null
    
    minute=0-59 * */n , - hour day month weekday job='/bin/sh /server/scripts/test.sh &>/dev/null'

    添加定时任务

    ansible 172.16.1.41 -m cron -a "minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'"
    ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'"

    删除定时任务

    ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' state=absent"
    ansible 172.16.1.41 -m cron -a "name=oldboy01 state=absent"

    注释定时任务

    ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=yes"
    ansible 172.16.1.41 -m cron -a "name=oldboy01 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=no"
    day
    hour
    minute
    month
    weekday
    name:任务名
    special_time :指定什么时候执行
    state:确认任务计划是创建还是删除   absent删除
    user  哪个用户

    总结ansible颜色信息:
    绿色:查看远程主机信息,不会对远程主机系统做任何修改
    红色:执行操作出现异常错误
    黄色:对远程主机系统进行修改操作
    粉色:警告或者忠告信息

    [root@m01 ~]# ansible -i /etc/ansible/h test -m ping
    10.0.0.5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    10.0.0.6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }

    ansible_ssh_user #指定用户
    ansible_ssh_pass #指定密码
    ansible_ssh_port #指定端口号

      

    [root@m01 ~]# cat /etc/ansible/hosts 
    [test]
    lb1 ansible_ssh_host=10.0.0.5 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    
    [root@m01 ~]# ansible test -m ping
    lb1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    
    
    [root@m01 ~]# cat /etc/ansible/hosts 
    [lb]
    lb1 ansible_ssh_host=172.16.1.5 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    lb2 ansible_ssh_host=172.16.1.6 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    [web]
    web1 ansible_ssh_host=172.16.1.7 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    web2 ansible_ssh_host=172.16.1.8 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    web3 ansible_ssh_host=172.16.1.9 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_ssh_port=22
    
    [root@m01 ~]# ansible lb:web -m ping
    lb1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    web2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    web1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    web3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    lb2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }


    ansible <host-pattern> [options]
      -v                  #输出详细信息, -vvv 可得到执行过程所有信息
      -i PATH, --inventory=PATH   #指定inventory信息 默认/etc/ansible/hosts
      -f NUM, --forks=NUM      # 并发线程数, 默认5个线程
      --private-key=PRIVATE_KEY_FILE # 指定秘钥文件
      -m NAME, --module-name=NAME #指定执行使用的模块
      -M DIRECTORY         #指定模块存路径,默认/usr/share/absible
      -a 'ARGUMENTS', --args='ARGUMENTS' #模块参数
      -k, --ask-pass SSH        # 认证密码
      -K, --ask-sudo-pass sudo   #用户的密码(--sudo时使用)
      -o, --one-line          #标准输出至一行
      -s, --sudo            #相对于linuxsudo
      -t DIRECTORY          #输出信息至DIRECTORY目录下, 结果文件以远程主机名命名
      -T SECONDS, --timeout=SECONDS #指定连接远程主机的最大超时, 单位是秒
      -B NUM             #后台执行命令 超NUM秒后中止正在执行的任务
      -P NUM, --poll=NUM     # 定期返回后台任务进度
      -u USERNAME, --user=USERNAME #指定远程主机以USERNAME运行命令
      -U SUDO_USERNAME      # 使用sudo ,相当于linux下的sudo命令
      -c CONNECTION        #指定连接方式
      -l SUBSET            # 指定运行主机
      -l~REGEX            # 指定运行主机正则
      --list-hosts          # 列出符合条件的主机列表 不执行任何命令

    # 检查lb 组所有的主机是否存活

    ansible lb -f 2 -m ping
    
    ansible lb -f 2 -m ping -vvv


    #列出lb组所有主机列表

    [root@m01 ~]# ansible lb --list 
    hosts (2):
    lb1
    lb2
    [root@m01 ~]# ansible lb --list-hosts
    hosts (2):
    lb1
    lb2
    ansible lb -a "df "
    ansible lb -m command -a "df "

    -f #并发数是cpu的偶数倍 2核 10个线程

    #安装

    ansible lb -m yum -a "name=redhat-lsb state=present"
    ansible lb -m yum -a "name=redhat-lsb state=installed"

    #安装最新版

    ansible lb -m yum -a "name=redhat-lsb state=latest"

    #卸载

    ansible lb -m yum -a "name=redhat-lsb state=absent"
    ansible lb -m yum -a "name=redhat-lsb state=removed"
    - state
     Whether to install (`present' or `installed', `latest'), or remove
     (`absent' or `removed') a package.
     `present' and `installed' will simply ensure that a desired package is
     installed.
     `latest' will update the specified package if it's not of the latest
     available version.
     `absent' and `removed' will remove the specified package.
     (Choices: absent, installed, latest, present, removed)[Default: present]
    

      

     

    #查看系统版本号

    [root@m01 ~]# ansible lb -m command -a 'lsb_release -a'
    lb2 | SUCCESS | rc=0 >>
    LSB Version::base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
    Distributor ID:CentOS
    Description:CentOS release 6.5 (Final)
    Release:    6.5
    Codename:Final

    #安装 nginx

    [root@m01 ~]# ansible lb -m yum -a "name=nginx state=present"
    
    [root@m01 ~]# ansible lb -m shell -a "chkconfig --list nginx"
    lb2 | SUCCESS | rc=0 >>
    nginx 0:off    1:off    2:off    3:off    4:off    5:off    6:off

    #启动nginx 并设置为开机启动

    [root@m01 ~]# ansible lb -m service -a "name=nginx state=started enabled=yes"
    lb1 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "nginx", 
    "state": "started"
    }
    
    [root@m01 ~]# ansible lb -m shell -a "/etc/init.d/nginx status"
    lb1 | SUCCESS | rc=0 >>
    nginx (pid 30057) is running...

    #检查是否开机自启动

     [root@m01 ~]# ansible lb -a "chkconfig --list nginx"
     lb1 | SUCCESS | rc=0 >>
     nginx 0:off    1:off    2:on    3:on    4:on    5:on    6:off
  • 相关阅读:
    javaScript常用运算符和操作符总结
    JavaScript-基本语法和数据类型
    javascript基本特点,组成和应用
    常用布局-列宽度是固定宽度还是自适应
    web设计之无懈可击
    CSS布局定位基础-盒模型和定位机制
    Ubuntu(Linux)系统WPS文字不能输入中文如何解决
    ggplot2点图+线性趋势+公式+R2+p值
    GTEx数据库-TCGA数据挖掘的好帮手
    limma, edgeR, deseq2,genefilter计算差异R包的foldchange方法差别
  • 原文地址:https://www.cnblogs.com/augustyang/p/10085324.html
Copyright © 2011-2022 走看看