vike681 发表于 2018-1-3 07:28:25

自动化运维工具ansible一些常用命令

  1. ansible-doc命令:获取模块列表,及模块使用格式;
  ansible-doc -l :获取列表
  ansible-doc -smodule_name :获取指定模块的使用信息。
  2.ansible 命令格式
  ansible<host-pattern>[-f forks] [-m module_name][-a args]
  <host-pattern>
  指明管控主机,以模式形式表示或者直接给定 IP ,必须事先定义在文件中; all 设置所有
[-f forks]
  指明每批管控多少主机,默认为 5 个主机一批次
[-m module_name]
  使用何种模块管理操作,所有的操作都需要通过模块来指定
[-a args]
  指明模块专用参数; args 一般为 key=value 格式
  注意:command模块的参数非为kv格式,而是直接给出要执行的命令即可;
  注意: <host-pattern> 默认读取 /etc/ansible/hosts ,也可以指明自定义文件路径
  -iPATH, --inventory=PATH:指明使用的host inventory文件路径;
  常用模块 (module_name) :
  1) command:默认模块 ,可省略。在远程主机上进行操作命令
  -a'COMMAND'
  注意: comand 模块的参数非 key=value 格式,直接给出要执行的命令
# ansible all -m command -a'ifconfig'
  2)user:
  -a 'name=state={present ( 创建 ) |absent ( 删除 ) }force= ( 是否强制操作删除家目录 )   system=uid=shell= home='
# ansible all -m user -a 'name=ansible state=present'
  3)group:
  -a 'name= state={present|absent}gid=system= ( 系统组 ) '
# ansible all -m group -a 'name=mygroup state=presentsystem=true'
  4)cron:
  -a'name= state=minute=hour= day=month=weekday= job='
# ansible all -m cron -a 'name='Time' state=presentminute='*/5' job='/usr/sbin/ntpdate 172.168.0.1 &> /dev/null''
  5)ping:
  无参数
# ansible all -m ping
  6) file: 文件管理
  -a 'path=mode=owner= group= state={file|directory|link|hard|touch|absent}src= (link ,链接至何处 ) '
# ansible all -m file -a 'path=/tmp/testdirstate=directory'
# ansible all -m file -a 'path=/tmp/test.txt state=touchmod=600 owner=user1'
  7)copy:
  -a 'dest= ( 远程主机上路径 )   src= ( 本地主机路径 )   content= ( 直接指明内容 )owner=group=mode='
# ansible web -m copy -a 'src=/etc/yum.repos.d/aliyun.repodest=/etc/yum.repos.d/'
  8)template
  -a'dest= src=\'#\'" content=owner= group=mode='
  9)yum:
  -a 'name=conf_file= ( 指明配置文件 )state={present|latest|absent} enablerepo= disablerepo='
# ansible all -m yum 'name=httpd state=present'
  10)service:
  -a 'name= state={started|stopped|restarted} enabled= ( 是否开机自动启动 )   runlevel='
# ansible all -m service -a 'name=httpd state=started'
  11)shell:
  -a 'COMMAND'    运行 shell 命令
# ansible all -m shell -a echo "123456789" |passwd --stdin user1'
  12)script:
  -a '/PATH/TO/SCRIPT' 运行脚本
# ansible all -m script -a '/tmp/a.sh'
  13) setup:获取指定主机的facts 变量 ;
页: [1]
查看完整版本: 自动化运维工具ansible一些常用命令