Ansible 学习记录 (五)常用模块详解

ansible 模块
  command 模块

    注解:不支持管道和重定向
    ansible test -m command -a 'echo redhat | passwd --stdin mageedu'
      - chdir
        cd into this directory before running the command
        [Default: None]

    样例:
    跳到指定目录,执行脚本文件
    ansible test -m command './zhangfengxue.sh chdir=/tmp/'

  shell模块

    注解:支持管道,支持重定向;使用awk 遇到$1 使用\ 进行转义

    ansible test -m shell -a 'echo redhat | passwd --stdin mageedu'

    执行状态:
      绿色:执行成功,并且不需要对目标做变更
      黄色:执行成功,并且需要对目标主机做变更
      红色:执行失败

  copy模块:

    注解:下发文件,下发目录,指定权限,备份,
    ansible test -m copy -a 'src=/root/125.sh dest=/root/3337.sh'

    ansible test -m copy -a 'src=/root/125.sh dest=/root/3337.sh owner=mageedu'

    ansible test -m copy -a "src=/tmp/test  dest=/usr/local/src/"
    常用参数:
      - backup---备份
      - mode---权限
      - owner---所有者
      - group---所属组
    综合样例:
      ansible test -m copy -a 'src=/root/125.sh dest=/root/3337.sh backup=yes owner=admin group=admin mode=0600'

  fetch模块:

    注解:只能拉取文件,不能拉取目录;目标位置有“/”和无“/”的区别
    常用参数:
      src
      dest
      flat
    综合样例:
      ansible test -m fetch -a "src=/etc/hosts dest=/tmp/testdir/ flat=yes"
      ansible test -m fetch -a "src=/etc/hosts dest=/tmp/host flat=yes"

  file模块:
    注解:操作远程主机的文件;设置文件属性;
    常用参数:
      path
      src
      dest
      mode
      state  #touch、link
    综合样例:
      ansible test -m file -a 'path=/opt/3337.sh mode=600'

      ansible test -m file -a 'src=/opt/3337.sh dest=/tmp/77.sh mode=644 state=touch'

      ansible test -m file -a 'src=/opt/3337.sh dest=/tmp/88.sh mode=644 state=link'

  hostname模块:
      注解:修改远程主机名称
    常用参数:
      name
    综合样例:
      ansible test -m hostname -a "name=webserver"

  pip模块:
    注解:python 的包管理工具
    常用参数:
      name
    综合样例:
      ansible test -m pip -a "name=bottle"

  ping模块:

     注解:检测主机存活

   综合样例:

      ansible test -m ping

   yum模块:

      注解:rpm包软件管理工具
    常用参数:
      name
      state
      list
    综合样例:
      ansible test -m yum -a 'name=httpd state=latest'

      ansible test -m yum -a 'name=httpd state=absent'

      ansible test -m yum -a 'name=httpd enablerepo=testing state=present'

      ansible test -m yum -a 'name=httpd-2.2.29 state=present'

         ansible test -m yum -a ‘name=* state=latest’

      ansible test -m yum -a ‘name=http://192.168.56.13/centos7.4/Packages/httpd-2.2.29-1.4.rpm state=present’

      ansible test -m yum -a ‘name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present’

      ansible test -m yum -a ‘name="@Development tools" state=present’

  service模块:
    控制服务的启动/停止
    常用参数:
      sleep----重启间隔
      state----服务状态
      name----服务名称
    综合样例:

      ansible test -m service -a 'name=httpd state=started'

      ansible test -m service -a 'name=httpd state=stopped'

      ansible test -m service -a 'name=httpd state=restarted'

      ansible test -m service -a 'name=httpd state=reloaded'

      ansible test -m service -a 'name=httpd enabled=yes'

      ansible test -m service -a 'name=httpd  pattern=/usr/local/httpd/bin/httpd state=started'

      ansible test -m service -a 'name=network state=restarted args=eth0'


  user模块:
    用户的管理
    常用参数:
      name
      home
      shell
      user
      group
      comment
      uid
      gid
      comment
      groups

      append

      move_home
    综合样例:

      ansible test -m user -a 'name=johnd comment="John Doe" uid=1040 group=admin'

      ansible test -m user -a 'name=admin home=/opt/admin uid=1000 gid-1000 shell=/bin/sh comment="admin lines" group=root'

      ansible test -m user -a 'name=johnd state=absent remove=yes'

      ansible test -m user -a 'name=ccs home=/opt/ccs move_home=yes'

      ansible test -m user -a 'name=ccs groups=admin,pop append=yes'

  raw:

    注解:不需要python支持,能够执行远程shell;

    综合样例;

      ansible test -m raw -a "chmod a+x /tmp/testfile" 

      ansible test -m raw -a "/tmp/testfile"

  cron:

    注解:计划任务

    常用参数:

      name

      minute

      hour

      day

      month

      weekday

      job

    综合样例:

      ansible test -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 192.168.134.10"'
      ansible test -m cron -a "name='for test' weekday='2' minute='0' hour='12' user='root' job='cat /etc/passwd >/root/111' cron_file='test ansible'"

  group:

    注解:添加用户组、删除用户组

    常用参数:

      name

      gid

      state

    综合样例:

      ansible test -m group -a "name=zfx state=present"

      ansible test -m group -a "name=zfx gid=4000 state=present"

      ansible test -m group -a "name=zfx state=absent"

  script:

    注解:在远程主机上执行本地主机上的脚本文件(等于=下发到远程主机+执行)

    综合样例:

      ansible test -m script -a "/tmp/testfile" 

  template:

    注解:允许在传输的文件中jinja变量数据

    常用参数:

      和copy模块参数完全相同

    综合样例:

      ansible test -m template -a “src=/temp/foo.j2 dest=/etc/file.conf owner=foo group=foo mode=0644“

   wait_for:

    注解:等待一个动作的变化,如端口激活,文件存在,超时

    常用参数:

      port

      path

      delay

      state

    综合样例;

      ansible test -m wait_for -a "port=8000 delay=10"

      ansible test -m wait_for -a "path=/tmp/foo"

      ansible test -m wait_for -a "path=/var/lock/file.lock state=absent"

  assemble:

    注解:源文件或目录被远程主机的目标文件include进去

    常用参数;

      src

      dest

      archive

    综合样例;

 

      ansible test -m assemble -a "src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf"

      ansible test -m assemble -a 'src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf delimiter='### START FRAGMENT ###''

      ansible test -m assemble -a 'src=/etc/ssh/conf.d/ dest=/etc/ssh/sshd_config validate='/usr/sbin/sshd -t -f %s''

   synchronize

    注解:类似于rsync,将本地文件下发到远程主机

    常用参数:

      src

      dest

      archive

    综合样例:

      ansible test -m synchronize -a "src=/root/testrsync dest=/tmp/. "

      ansible test -m synchronize -a "src=/root/testrsync dest=/tmp/. archive=no"

 

posted @ 2018-01-31 17:12  血与火的洗礼  阅读(149)  评论(0编辑  收藏  举报