发表于 2018-7-29 06:51:24

ansible的简单使用格式:

  ansible的简单使用格式:
  Ansible服务器192.168.182.254
  ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS
  ansible <host-pattern> [-m module_name] [-a args]
  ansible的常用模块:
  获取所有模块列表;
  ansible-doc -l
  获取模块参数:
  ansible-doc -s module_name
  command模块:在远程主机运行命令;模块可省略,默认为command模块。
  例如:
  ansible test -a &quot;adduser test1&quot; 添加用户
  shell模块:在远程主机在shell进程下运行命令,支持shell的特性,如管道,重定向等;会在子shell中运行命令
  例如:
  ansible test -a &quot;echo &quot;123123&quot; | passwd --stdin test1&quot; 批量修改密码
  copy模块:Copies files to remote locations.复制本地文件至远程服务器,并且能够改属性等
  用法:
  (1)src= dest=
  src=源文件路径,可以使用绝对路径也可以使用相对路径,如果路径是一个目录,递归的复制目录;,如果路径以“/”结束,只有在该目录的内容复制到目的地。否则,如果它不以“/”结束,与所有内容复制目录本身;
  dest=远程路径,必须是绝对路径,如果src是目录,dest一定也是目录
  例如:
  ansible test -m copy -a 'src=/etc/fstab dest=/tmp/fstab'
  (2)content= dest=
  owner=,group=,mode= 可以指明目标文件的属主,属组,权限
  content=源文件,是将数据流覆盖填充到dest=远程目标文件中;
  例如:
  ansible test -m copy -a 'content=&quot;hello there\n&quot; mode=400 dest=/tmp/tmpfile'
  注意:单引号和双引号的嵌套;
  cron:管理任务计划
  minute=( 0-59, , /2, etc )
  day=( 1-31, , /2, etc )
  month=( 1-12, , /2, etc )
  weekday=( 0-6 for Sunday-Saturday, , etc )
  hour=( 0-23, , /2, etc )
  job= #需要执行的任务
  name= #必须给的选项
  state=
  present:创建
  absent:删除
  例如:
  添加任务计划:
  ansible all -m cron -a &quot;minute=/5 job='/sbin/ntpdate 172.16.0.1 &> /dev/null' name=Synctime&quot;
  删除任务计划:
  ansible all -m cron -a &quot; t name=Synctime&quot;
  查看任务计划的命令:crontab -l
  真实的位置在:/var/spool/cron下面
  fetch模块:从远程主机上获取文件到本地主机,只能fetch文件,不能fetch目录;
  dest=本地主机的目录,只能是目录,拉取文件到本地时会以IP为目录名,在其中保留文件的目录结构,通常用来备份;
  src=只能是远程主机上的文件,不能是目录
  例如:
  ansible all -m fetch -a &quot;src=/tmp/test/123 dest=/tmp/&quot;
  file模块: 设置文件属性
  用法:
  (1)创建链接文件:path=目标路径(必给项) src=源文件 state=link
  例如:
  ansible all -m file -a &quot;src=/tmp/fstab path=/tmp/fstab.link state=link&quot;
  (2)修改属性:path= owner= mode= group=
  (3)创建目录:path= state=directory
  例如:
  ansible all -m file -a &quot;path=/tmp/test state=directory&quot;
  注意:创建目录可递归创建。
  hostname模块:管理主机名
  name=
  pip模块: Python的包管理工具
  yum模块:yum程序包管理
  name=:程序包名称,可以带版本号
  state=
  present,latest
  absent
  例如:
  安装软件包
  ansible all -m yum -a &quot;name=lrzsz&quot;
  ansible all -m yum -a &quot;name=httpd &quot;
  删除软件包:
  ansible all -m yum -a &quot;name=lrzsz state=absent&quot;
  service模块:管理服务
  name=
  state=
  started:启动
  stopped:停止
  restarted:重启
  enabled=是否开机启动
  runlevel=
  例如:
  ansible all -m service -a &quot;name=httpd state=started&quot;
  url模块:
  user模块:管理用户账号
  name=
  system={yes|no}
  uid=
  shell= 可选设置默认shell,默认为/bin/bash
  group=
  groups=
  comment= 可选设置描述,默认为空
  home= 可选设置家目录,默认创建目录
  state=
  present 创建
  absent 删除
  setup模块:用来获取facts变量
  group模块:用来添加或删除组
  name= (必给的)
  state=
  present
  absent
  system=
  gid=
  script模块:执行脚本
  -a &quot;/PATH/TO/SCRIPT_FILE&quot;
  template模块:基于模板方式生成一个文件复制到远程主机
  src=
  dest=
  owner=
  group=
  mode=
  等等
页: [1]
查看完整版本: ansible的简单使用格式: