奇忠诚 发表于 2018-7-30 12:09:50

ansible 模块学习

  Ansible通过模块的方式来完成一些远程的管理工作。可以通过ansible-doc -l查看所有模块,可以使用ansible-doc -s module来查看某个模块的参数,也可以使用ansible-doc help module来查看该模块更详细的信息。默认的模块位置在/usr/share/ansible。下面列出一些常用的模块:
  1. setup
  可以用来查看远程主机的一些基本信息:
  ansible -i /etc/ansible/hosts test -m setup
  2.ping
  可以用来测试远程主机的运行状态:
  ansible test -m ping
  3.file
  设置文件的属性
  file模块包含如下选项:
  force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
  group:定义文件/目录的属组
  mode:定义文件/目录的权限
  owner:定义文件/目录的属主
  path:必选项,定义文件/目录的路径
  recurse:递归的设置文件的属性,只对目录有效
  src:要被链接的源文件的路径,只应用于state=link的情况
  dest:被链接到的路径,只应用于state=link的情况
  state:
  directory:如果目录不存在,创建目录
  file:即使文件不存在,也不会被创建
  link:创建软链接
  hard:创建硬链接
  touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
  absent:删除目录、文件或者取消链接文件
  ansible -i /root/hosts all-m setup   查看远程主机的信息
  ansible -i hostsall-m file -a "path=/tmp/test state=touch"远程创建/tmp/test文件
  ansible -i hostsall-m file -a "src=/etc/fstab dest=/tmp/fstab state=link"在远程主机上建立软连接。/etc/fstab---------->/tmp/fstab
  lrwxrwxrwx. 1 root   root   10 Oct 16 15:28 fstab -> /etc/fstab
  ansible -i hosts all-m file -a "path=/tmp/fstab state=absent"   删除目录文件 或者取消链接
  192.168.1.60 | success >> {
  "changed": true,
  "path": "/tmp/fstab",
  "state": "absent"
  }
  192.168.1.63 | success >> {
  "changed": true,
  "path": "/tmp/fstab",
  "state": "absent"
  4.copy
  复制文件到远程主机
  copy模块包含如下选项:
  backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
  content:用于替代"src",可以直接设定指定文件的值
  dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
  directory_mode:递归的设定目录的权限,默认为系统默认权限
  force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
  others:所有的file模块里的选项都可以在这里使用
  src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
  # cat copy.yml
  - hosts: test1
  tasks:
  - name: copy the file
  copy: src=/root/purge_relay_logs.sh   dest=/rootowner=root group=root mode=755
  # ansible-playbook-i /root/ansible/host/testcopy.yml
  PLAY ******************************************************************
  GATHERING FACTS ***************************************************************
  ok:
  ok:
  TASK: *********************************************************
  changed:
  changed:
  PLAY RECAP ********************************************************************
  192.168.1.63               : ok=2    changed=1    unreachable=0    failed=0
  192.168.1.76               : ok=2    changed=1    unreachable=0    failed=0
  5.command
  在远程主机上执行命令
  command模块包含如下选项:
  creates:一个文件名,当该文件存在,则该命令不执行
  free_form:要执行的linux指令
  chdir:在执行指令之前,先切换到该指定的目录
  removes:一个文件名,当该文件不存在,则该选项不执行
  executable:切换shell来执行指令,该执行路径必须是一个绝对路径
  示例:
  ansible test -a "/sbin/reboot"
  6.shell
  切换到某个shell执行指定的指令,参数与command相同。
  示例:
  ansible test -m shell -a "somescript.sh >> somelog.txt"
  7.service
  用于管理服务
  该模块包含如下选项:
  arguments:给命令行提供一些选项
  enabled:是否开机启动yes|no
  name:必选项,服务名称
  pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行
  runlevel:运行级别
  sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟
  state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
  示例:
  ansible test -m service -a "name=httpd state=started enabled=yes"
  ansible test -m service -a "name=foo pattern=/usr/bin/foo state=started"
  ansible test -m service -a "name=network state=restarted args=eth0"
  8.cron
  用于管理计划任务
  包含如下选项:
  backup:对远程主机上的原任务计划内容修改之前做备份
  cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
  day:日(1-31,*,*/2,……)
  hour:小时(0-23,*,*/2,……)
  minute:分钟(0-59,*,*/2,……)
  month:月(1-12,*,*/2,……)
  weekday:周(0-7,*,……)
  job:要执行的任务,依赖于state=present
  name:该任务的描述
  special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
  state:确认该任务计划是创建还是删除
  user:以哪个用户的身份执行
  示例:
  ansible test -m cron -a 'name="check dirs" hour="5,2" job="ls -alh > /dev/null"'
  ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
  ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root" job="YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate" cron_file=ansible_yum-autoupdate'
  ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
  ansible -i hostsall -m cron -a 'name="sync time for ntp server"   minute='*/3' job="/usr/sbin/ntpdate cn.pool.ntp.org"'
  192.168.1.60 | success >> {
  "changed": true,
  "jobs": [
  "sync time for ntp server"
  ]
  }
  192.168.1.63 | success >> {
  "changed": true,
  "jobs": [
  "sync time for ntp server"
  ]
  }
  crontab-l
  #Ansible: sync time for ntp server
  */3 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
  ansible -i hosts all-a 'crontab -l'
  192.168.1.60 | success | rc=0 >>
  #Ansible: sync time for ntp server
  */3 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
  192.168.1.63 | success | rc=0 >>
  #Ansible: sync time for ntp server
  */3 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
  9.filesystem
  在块设备上创建文件系统
  选项:
  dev:目标块设备
  force:在一个已有文件系统的设备上强制创建
  fstype:文件系统的类型
  opts:传递给mkfs命令的选项
  10.yum
  使用yum包管理器来管理软件包
  选项:
  config_file:yum的配置文件
  disable_gpg_check:关闭gpg_check
  disablerepo:不启用某个源
  enablerepo:启用某个源
  list
  name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
  state:状态(present,absent,latest)
  示例:
  ansible test -m yum -a 'name=httpd state=latest'
  ansible test -m yum -a 'name="@Development tools" state=present'
  ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
  11.user
  管理用户
  home:
  groups:
  uid
  password:
  name:
  createhome:
  system:
  remove:
  state:
  shell:
  # cat user.yml
  - name: create user for test group
  hosts: test
  user: root
  gather_facts: false
  vars:(引入变量)
  - user: "toy" (变量名称)
  tasks:
  - name: create `user` on test
  user: name="`user`"
  gather_facts 的作用是搜集远端机器的相关信息
  ansible-playbook-i /root/hostsuser.yml
  PLAY *********************************************
  TASK: *****************************************************
  changed:
  changed:
  PLAY RECAP ********************************************************************
  192.168.1.60               : ok=1    changed=1    unreachable=0    failed=0
  192.168.1.63               : ok=1    changed=1    unreachable=0    failed=0
  12.group
  管理组
  13.synchronize
  使用rsync同步文件
  archive
  checksum
  delete
  dest
  src
  dest_port
  existing_only: skip createing new files on receiver
  links
  owner
  mode:(push, pull)
  recursive
  rsync_path
  times:Preserve modification times
  示例:
  src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
  src=some/relative/path dest=/some/absolute/path archive=no links=yes
  src=some/relative/path dest=/some/absolute/path checksum=yes times=no
  src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
  14.mount
  配置挂载点
  选项:
  dump
  fstype:必选项,挂载文件的类型
  name:必选项,挂载点
  opts:传递给mount命令的参数
  passno
  src:必选项,要挂载的文件
  state:必选项
  present:只处理fstab中的配置
  absent:删除挂载点
  mounted:自动创建挂载点并挂载之
  umounted:卸载
  示例:
  name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
  name=/srv/disk src='LABEL=SOME_LABEL' state=present
  name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
  ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
  ansible test -a 'losetup /dev/loop0 /disk.img'
  ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
  ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
  15.raw
  类似command,但可以传递管道
  # ansible-i ~/hosts all -m raw -a "rpm -qa |grep xinetd"(使用raw模块查看远程服务器上是否安装了xinetd服务,说明没有安装)
  192.168.1.61 | FAILED | rc=1 >>
  b
  # ansible -i ~/hosts all-m yum -a "name=xinetd state=latest"(使用yum模块进行安装yum源里最新版的xinetd软件)
  192.168.1.61 | success >> {
  "changed": true,
  "msg": "",
  "rc": 0,
  "results": [

  "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.skyshe.cn\n * extras: mirrors.pubyun.com\n * updates: mirrors.pubyun.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package xinetd.x86_64 2:2.3.14-39.el6_4 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch            Version                  Repository    >  ]
  }
  # ansible-i ~/hosts all -m raw -a "rpm -qa |grep xinetd" (再次使用raw模块查看xinetd是否安装)
  192.168.1.61 | success | rc=0 >>
  xinetd-2.3.14-39.el6_4.x86_6
  ansible-i ~/hosts all -m shell -a "service xinetd restart"(使用shell模块启动xinetd服务)
  192.168.1.61 | success | rc=0 >>
  Stopping xinetd:
  Starting xinetd:
  # ansible-i ~/hosts all -m service -a "name=xinetd state=restarted" (同样你也可以使用service模块进行服务的启动,关闭和重启)
  192.168.1.61 | success >> {
  "changed": true,
  "name": "xinetd",
  "state": "started"
  }
  ansible all -m raw -a 'yum -y install python-simplejson'批量安装软件
  ansible -i /root/hosts all -m copy -a"src=/root/test.sh dest=/tmp"
  ansible -i /root/ansible/host/test all-m copy -a "src=/root/appdest=/tmp"   copy整个目录 远端服务器会生成app的目录
  192.168.1.61 | FAILED >> {
  "failed": true,
  "md5sum": "5d0cb5573fa5ead11bfcc701a2bb0551",
  "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  }
  192.168.1.60 | FAILED >> {
  "failed": true,
  "md5sum": "5d0cb5573fa5ead11bfcc701a2bb0551",
  "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  }
  192.168.1.63 | FAILED >> {
  "failed": true,
  "md5sum": "5d0cb5573fa5ead11bfcc701a2bb0551",
  "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  }
  # ansible -i hosts all -m script -a "/tmp/test.sh" 这个是因为本地没有test.sh这个脚本 这条命令的意思是在远程机器上执行本地的脚本
  192.168.1.60 | FAILED => file or module does not exist: /tmp/test.sh
  192.168.1.63 | FAILED => file or module does not exist: /tmp/test.sh
  # ansible -i /root/host/hosts all-m shell -a "/root/test.sh"用shell是在远程机器上执行远程机器上的脚本
  192.168.1.66 | FAILED | rc=127 >>
  /bin/sh: /root/test.sh: No such file or directory
  192.168.1.60 | success | rc=0 >>
  2014-10-29 script testing success!
  192.168.1.63 | FAILED | rc=127 >>
  /bin/sh: /root/test.sh: No such file or directory
页: [1]
查看完整版本: ansible 模块学习