nics 发表于 2018-7-30 11:00:28

运维工具Ansible—常用模块的使用

  常用模块使用
  首先在使用模块之前我们可以查看一下模块有多少,不过大多数用不到。下面主要看几个常用模块
# ansible-doc -l | wc -l  
262
  1、setup
  ##用来查看远程主机的基本信息
# ansible test -m setup  2、ping
  ##用来测试远程主机的运行状态
# ansible test -m ping  3、file
  ##设置文件属性
# ansible-doc -s file  
- name: Sets attributes of files
  
action: file
  
force                  # 需要在两种情况下创建软连接,一种是源文件不存在,但之后建立的情况下;另一种是目标软连接已存在,需要先取消之前的软链,然后创建新的软链:有两个选项:yes|no
  
group                  # 定义文件/目录的所属组
  
mode                   # 定义文件/目录的权限
  
owner                  # 定义文件/目录的所属主
  
path=                  # 必选项,定义文件/目录的路径
  
recurse                # 递归设置文件的属性,只对目录有效
  
src                  # 要被链接的源文件路径,只应用于state=link的情况有效
  
state                  # 状态
  
      directory      #如果目录不存在则创建目录
  
      file                #即使文件不存在,也不会被创建
  
      link                #创建软连接
  
      hard                #创建硬链接
  
      touch               #如果文件不存在,则会创建一个新的文件;如果文件或目录存在,则更新其最后的修改时间
  
      absent            #删除目录、文件或者取消链接文件
  
(END)
  将远程主机/etc/passwd 文件链接到 /root 目录下:
# ls  
Python-2.7.5shellsoft
  
# ansible test -m file -a "src=/etc/passwd dest=/root/passwd state=link"
  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "dest": "/root/passwd",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0777",
  
    "owner": "root",
  
    "size": 11,
  
    "src": "/etc/passwd",
  
    "state": "link",
  
    "uid": 0
  
}
  

  
# ll
  
total 12
  
drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5
  
lrwxrwxrwx1 root root   11 Dec 16 15:35 passwd -> /etc/passwd
  
drwxr-xr-x3 root root 4096 Dec7 15:06 shell
  
drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
  删除创建的软连接
# ansible test -m file -a 'path=/root/passwd state=absent'  
192.168.1.2| success >> {
  
    "changed": true,
  
    "path": "/root/passwd",
  
    "state": "absent"
  
}
  

  
# ll
  
total 12
  
drwxr-xr-x 18 1000 1000 4096 Nov 26 11:11 Python-2.7.5
  
drwxr-xr-x3 root root 4096 Dec7 15:06 shell
  
drwxr-xr-x 12 root root 4096 Dec 16 15:32 soft
  创建文件
# ansible test -m file -a 'path=/root/1 state=touch'  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "dest": "/root/1",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0644",
  
    "owner": "root",
  
    "size": 0,
  
    "state": "file",
  
    "uid": 0
  
}
  

  
# ls
  
1Python-2.7.5shellsoft
  4、cope
  ##复制本地文件到远程主机,进行统一部署
  用法:
  backup          # 用于复制时,是否备份远程主机上的目标文件
  content         # 用于替代 ‘src’ ,可以直接设定指定文件的值
  dest=         # 必选项,要将源文件复制到远程主机的绝对路径,如果源文件是一个目录,那么远程路径也必须是一个目录
  directory_mode         # 递归设定目录的权限,默认为系统默认权限
  force                  # 强制覆盖。如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖;如果设置为no,则只有当目录主机的目录位置不存在该文件时,才复制。默认为yes
  group                  # 定义文件/目录的所属组
  mode                   # 定义文件/目录的权限
  owner                  # 定义文件/目录的所属主
  src                  # 要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用 '/' 结尾,则只复制目录里的内容,如果没有使用 '/' 来结尾,则包含目录在内的整个内容全部复制。
  实例:
  讲本地文件复制到远程主机
#ansible test -m copy -a "src=/root/1 dest=/tmp/1mode=600 "  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
  
    "dest": "/tmp/1",
  
    "gid": 0,
  
    "group": "root",
  
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
  
    "mode": "0600",
  
    "owner": "root",
  
    "size": 0,
  
    "src": "/root/.ansible/tmp/ansible-tmp-1450252219.51-80486766978488/source",
  
    "state": "file",
  
    "uid": 0
  
}
  

  
# ll /tmp/1
  
-rw------- 1 root root 0 Dec 16 15:50 /tmp/1
  5、command
  ##远程主机上执行命令
  creates:一个文件名,当该文件存在,则该命令不执行
  free_form:要执行的linux指令
  chdir:在执行指令之前,先切换到该目录
  removes:一个文件名,当该文件不存在,则该选项不执行
  executable:切换shell来执行指令,该执行路径必须是一个绝对路径
# ansible test -m command -a 'ls /home'  
192.168.1.2| success | rc=0 >>
  
redis
  
zabbix
  实例:
  使用 creates 参数,判断一个文件是否存在,存在的话,就跳过后面的执行命令
# ansible test -m command -a 'creates=/tmp/1 ls -l /etc/passwd'  
192.168.1.2| success | rc=0 >>
  
skipped, since /tmp/1 exists
  

  
# ansible test -m command -a 'creates=/tmp/2 ls -l /etc/passwd'
  
192.168.1.2 | success | rc=0 >>
  
-rw-r--r-- 1 root root 1882 Dec 14 20:11 /etc/passwd
  6、shell
  ##与command不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw
  实例1:
# ansible test -m shell -a 'cat /etc/passwd| grep root'  
192.168.1.2| success | rc=0 >>
  
root:x:0:0:root:/root:/bin/bash
  
operator:x:11:0:operator:/root:/sbin/nologin
  7、synchronize
  使用rsync同步文件。使用rsync 模块,系统必须安装rsync 包,否则无法使用这个模块
  用法:
  archive                # 是否采用归档模式同步,即以源文件相同属性同步到目标地址
  checksum               # 是否效验
  compress               #
  copy_links             # 同步的时候是否复制连接
  delete               # 删除源中没有而目标存在的文件
  dest=                  # 目标地址
  dest_port            # 目标接受的端口
  dirs                   # 以非递归的方式传输目录
  existing_only          # Skip creating new files on receiver.
  group                  # Preserve group
  links                  # Copy symlinks as symlinks.
  mode                   # 模式,rsync 同步的方式 PUSH\PULL
  recursive            # 是否递归 yes/no
  rsync_opts             # 使用rsync 的参数
  rsync_path             # 服务的路径(源码编译时需指定)
  rsync_timeout          # Specify a --timeout for the rsync command in seconds.
  set_remote_user      # put user@ for the remote paths. If you have a custom ssh                                  config to define the remote user for
  src=                   # 源,同步的数据源
  检查机器上是否装过rsync:
#which rsync  没有装过的主机可以远程安装rsync包:
#ansible test -m yum -a 'name=rsync state=latest'  实例:
  1、将ansible 服务端/root/ 下的hello 文件同步到test的/tmp:
# ansible test -msynchronize -a 'src=/root/hello dest=/tmp/'  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "cmd": "rsync --delay-updates -F --compress --archive --rsh 'ssh-S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/hello\" \"root@121.42.141.231:/tmp/\"",
  
    "msg": "<f+++++++++ hello\n",
  
    "rc": 0,
  
    "stdout_lines": [
  
      "<f+++++++++ hello"
  
    ]
  
}
  
# ls /tmp/hello
  
/tmp/hello
  8、cron
  cron 模块,用于管理计划任务
  用法:
  backup               # 在对远程主机上的原计划任务修改之前做备份(也就是先备份再修改)
  cron_file            # 如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
  day                  # 天(1-31,*,*/2, ……)
  hour                   # 小时( 0-23, *, */2, ……)
  job                  # T要执行的任务,依赖于state=present
  minute               # 分钟(0-59,*,*/2,……)
  month                  # 月(1-12,*,*/2,……)
  name                   # 该任务的描述
  reboot               # If the job should be run at reboot. This option
  special_time         #指定什么时候执行,参数:      reboot,yearly,annually,monthly,weekly,daily,hourly
  state                  # 确认该任务计划是创建还是删除
  user                   #以哪个用户的身份执行
  weekday                #周(0-7,*,……)
  实例:
  1、给test 主机创建一个计划任务:
#ansible test -m cron -a "name='echo ' hour=2 user=root job='echo 1 >> /root/hello' "  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "jobs": [
  
      "echo "
  
    ]
  
}
  
# ansible test -m shell -a "crontab -l"
  
192.168.1.2 | success | rc=0 >>
  
* * * * 1 sh /root/shell/apache_log.sh
  
#Ansible: echo
  
* 2 * * * echo 1 >> /root/hello
  2、删除刚才创建的计划任务
#ansible test -m cron -a "name='echo' hour=2 user=root job='echo 1 >> /root/hello' state=absent"  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "jobs": []
  
}
  

  
# ansible test -m shell -a "crontab -l"                                     192.168.1.2 | success | rc=0 >>
  
* * * * 1 sh /root/shell/apache_log.sh
  9、service
  service 模块作用于一些服务,比如对某些服务器的启动、重启、停止、重载等的管理
  arguments            # 给命令行提供一些选项
  enabled                # 是否开机启动yes|no
  name=                  # 必选项,服务名称
  pattern                # 定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行
  runlevel               # 运行级别
  sleep                  # 如果执行了restarted,在则stop和start之间沉睡几秒钟
  state                  # 对当前服务执行启动,停止、重启、重新加载等操作               (started,stopped,restarted,reloaded)
  实例:
  实例:远程启动http
# ss -nl  
State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port
  
LISTEN   0      80                        *:3306                     *:*
  
LISTEN   0      128                     *:111                      *:*
  
LISTEN   0      128                     *:22                     *:*
  
LISTEN   0      128                     *:43131                  *:*
  
# ansible test -m service -a "name=httpd state=started enabled=yes"
  
192.168.1.2 | success >> {
  
    "changed": true,
  
    "enabled": true,
  
    "name": "httpd",
  
    "state": "started"
  
}
  
# ss -nl
  
State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port
  
LISTEN   0      80                        *:3306                     *:*
  
LISTEN   0      128                     *:111                      *:*
  
LISTEN   0      128                     *:80                     *:*
  
LISTEN   0      128                     *:22                     *:*
  
LISTEN   0      128                     *:43131                  *:*
  其他常用的模块还有yum   userfilesystem mount
页: [1]
查看完整版本: 运维工具Ansible—常用模块的使用