设为首页 收藏本站
查看: 1230|回复: 0

[经验分享] ansible常用模块参数

[复制链接]

尚未签到

发表于 2018-7-28 14:16:13 | 显示全部楼层 |阅读模式
  ansible常用基础模块示例
  查看ansible支持的模块  =======================
  [root@master ~]# ansible-doc -l
  查看模块支持的参数
  # ansible-doc <模块名称>
  [root@master ~]# ansible-doc ping
  查看该模块有哪些参数可以使用
  [root@master ~]# ansible-doc  -s  <模块名称>
  如:
  ansible-doc  -s user  查看user模块有哪些参数可以用
  推荐常用模块博客:http://blog.csdn.net/pushiqiang/article/details/78249665
  1、ping模块
  2、command模块
  3、shell 模块
  4、copy模块
  5、cron模块
  6、fetch模块
  7、file模块
  8、hostname模块
  9、yum模块
  10、service模块
  11、 uri模块
  12、group模块
  13、user模块
  14、script模块
  15、setup模块
  16、authorized_key模块
  17、synchronize模块(使用rsync同步文件。使用rsync 模块,系统必须安装rsync 包,否则无法使用这个模块)
  ansible模块的说明:
  # ansible <pattern> -m <module_name> [-a <arguments>]
  1、ping模块=====================
  作用:检测被管理端是否在线
  [root@Allen ~]# ansible test -m ping
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: false,
  &quot;ping&quot;: &quot;pong&quot;
  }
  ping的方法有三种:
  1、all   代表全部的管理节点,即我们/etc/ansible/hosts中添加的全部被管理的主机
  2、指定分组   如:自定义分组test  下面截图会列出来
  3、指定单个主机ip或者域名    即我们/etc/ansible/hosts中添加的主机名或ip
  2、command模块 =====================
  作用:在被管理端执行命令,不支持重定向,管道 ,默认模块
  指定test组(这个组是配置文件中指定的)
  [root@Allen ~]# ansible test -m command -a 'uptime'    //-a指定要传递给模块的参数
  172.16.20.114 | SUCCESS | rc=0 >>
  18:19:18 up  1:40,  2 users,  load average: 0.00, 0.00, 0.00
  all代表所有主机:
  [root@Allen ~]# ansible all -m command -a 'uptime'
  172.16.20.115 | SUCCESS | rc=0 >>
  18:20:30 up 22 min,  3 users,  load average: 0.00, 0.00, 0.00
  用ip具体指向某一台服务器
  [root@Allen ~]# ansible 172.16.20.114 -m command -a 'uptime'
  172.16.20.114 | SUCCESS | rc=0 >>
  18:20:31 up  1:41,  2 users,  load average: 0.00, 0.00, 0.00
  [root@Allen ~]# ansible test -m command -a 'date'
  172.16.20.114 | SUCCESS | rc=0 >>
  Mon Jul 31 18:21:16 CST 2017
  [root@Allen ~]# ansible test -m command -a 'ls /tmp'
  172.16.20.114 | SUCCESS | rc=0 >>
  ansible_0TxRxj
  yum.log
  [root@Allen ~]# ansible test -m command -a 'df -Th'
  172.16.20.115 | SUCCESS | rc=0 >>
  Filesystem           Type   Size  Used Avail Use% Mounted on
  /dev/mapper/VolGroup-lv_root
  ext4    50G  1.1G   46G   3% /
  tmpfs                tmpfs  932M     0  932M   0% /dev/shm
  /dev/sda1            ext4   477M   38M  414M   9% /boot
  /dev/mapper/VolGroup-lv_home
  ext4    45G   52M   43G   1% /home
  [root@Allen ~]# ansible test -m command -a 'touch /tmp/a.txt'
  [WARNING]: Consider using file module with state=touch rather than running touch
  //警告意思:建议用file模块创建一个文件 不建议用command运行touch命令。类似salt的cmd.run命令。是一个万能的命令,就连删除根都可以
  172.16.20.114 | SUCCESS | rc=0 >>
  [root@Allen ~]# ansible all -a '/bin/echo hello'
  172.16.20.114 | SUCCESS | rc=0 >>
  hello
  参数:========================
  chdir=<Directory>
  [root@Allen ~]# ansible test -m command -a 'chdir=/tmp ls ./'
  172.16.20.114 | SUCCESS | rc=0 >>
  ansible_OQrj0Q
  b.txt
  3、shell 模块==========================
  作用:在被管理端执行命令 支持重定向,管道
  先看一下command的结果:
  [root@Allen ~]# ansible test -m command -a 'echo &quot;hello ansible&quot; > /tmp/b.txt'
  172.16.20.115 | SUCCESS | rc=0 >>
  hello ansible > /tmp/b.txt
  172.16.20.114 | SUCCESS | rc=0 >>
  hello ansible > /tmp/b.txt
  再看一下shell的结果:
  [root@Allen ~]# ansible test -m shell -a &quot;echo 'hello ansible2' > /tmp/ddd.txt&quot;
  172.16.20.114 | SUCCESS | rc=0 >>
  172.16.20.115 | SUCCESS | rc=0 >>
  结果显示:如果是command她就在屏幕上打印出来,如果是shell,他就重定向到文件,不在屏幕上打印
  [root@Allen ~]# ansible test -m command -a &quot;cat /tmp/b.txt&quot;
  172.16.20.114 | SUCCESS | rc=0 >>
  hello ansible
  172.16.20.115 | SUCCESS | rc=0 >>
  hello ansible
  [root@Allen ~]# ansible test -m shell -a 'ls /tmp'
  172.16.20.114 | SUCCESS | rc=0 >>
  ansible_HAgoqq
  b.txt
  172.16.20.115 | SUCCESS | rc=0 >>
  ansible_so6OnF
  a.txt
  b.txt
  yum.log
  参数:
  chdir=<Directory>
  [root@Allen ~]# ansible test -m shell -a 'chdir=/tmp ls ./'
  172.16.20.114 | SUCCESS | rc=0 >>
  ansible_PsJuUc
  bb.txt
  4.copy模块===========================
  作用:拷贝ansible管理端的文件到远程主机的指定位置
  常见参数有:
  dest=    指明拷贝文件的目标目录位置,使用绝对路径,如果源是目录,则目标也要是目录,如果目标文件已存在,会覆盖原有内容
  src=     指明本地路径下的某个文件,可以使用相对路径和绝对路径,支持直接指定目录,如果源是目录,则目标也要是目录。如果路径用/结尾了,那么只复制目录里面的内容。如果没有用/来结尾,则包含目录在内的整个内容都复制,类似rsync
  mode=    指明复制时,目标文件的权限
  owner=   指明复制时,目标文件的属主
  group=   指明复制时,目标文件的属组
  content= 指明复制到目标主机上的内容,不能与src一起使用,相当于复制content指明的数据,到目标文件中
  [root@Allen ~]# ansible test -m copy -a &quot;src=/etc/hosts dest=/tmp mode=755 owner=root group=root&quot;
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;checksum&quot;: &quot;7335999eb54c15c67566186bdfc46f64e0d5a1aa&quot;,
  &quot;dest&quot;: &quot;/tmp/hosts&quot;,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;mode&quot;: &quot;0755&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;path&quot;: &quot;/tmp/hosts&quot;,
  &quot;size&quot;: 158,
  &quot;state&quot;: &quot;file&quot;,
  &quot;uid&quot;: 0
  }
  [root@Allen ~]# ansible test -m copy -a 'content=&quot;hello linux&quot; dest=/tmp/c.txt mode=600'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;checksum&quot;: &quot;223ce1d650508823f9dd51d8cb4b527ad3d03ca7&quot;,
  &quot;dest&quot;: &quot;/tmp/c.txt&quot;,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;md5sum&quot;: &quot;c5fe55563f6ea61e2b28be7c8a5835c2&quot;,
  &quot;mode&quot;: &quot;0600&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;size&quot;: 11,
  &quot;src&quot;: &quot;/root/.ansible/tmp/ansible-tmp-1501510008.59-35638455893367/source&quot;,
  &quot;state&quot;: &quot;file&quot;,
  &quot;uid&quot;: 0
  }
  [root@Allen ansible]#ansible all -m copy -a 'content=&quot;hello ansible\nHi lifu&quot; dest=/tmp/test.ansible'
  到远程主机上查看:
  [root@localhost tmp]# cat test.ansible
  hello ansible
  Hi lifu[root@localhost tmp]#
  5.cron模块============================
  作用:管理计划任务的模块
  常见参数有:
  minute=  指明计划任务的分钟,支持格式:0-59,*,*/2等,与正常cron任务定义的一样的语法,省略时,默认为*,也就是每分钟都执行
  hour=    指明计划任务的小时,支持的语法:0-23,*,*/2等,省略时,默认为*,也就是每小时都执行
  day=     指明计划任务的天,支持的语法:1-31,*,*/2等,省略时,默认为*,也就是每天都执行
  month=   指明计划任务的月,支持的语法为:1-12,*,*/2等,省略时,默认为*,也就是每月都执行
  weekday= 指明计划任务的星期几,支持的语法为:0-6,*等,省略时,默认为*,也就是每星期几都执行
  reboot   指明计划任务执行的时间为每次重启之后
  name=    给该计划任务取个名称,必须要给明。每个任务的名称不能一样。
  job=     执行的任务是什么,当state=present时才有意义
  state=present|absent   表示这个任务是创建还是删除,present表示创建,absent表示删除,默认是present
  创建计划任务
  [root@Allen ~]# ansible test -m cron -a 'minute=*/5 name=Ajob job=&quot;/usr/sbin/ntpdate 172.16.8.100 &> /dev/null&quot; state=present'
  172.16.20.115 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;envs&quot;: [],
  &quot;jobs&quot;: [
  &quot;Ajob&quot;
  ]
  }
  删除计划任务
  [root@Allen ~]# ansible test -m cron -a 'minute=*/5 name=Ajob job=&quot;/usr/sbin/ntpdate 172.16.8.100 &> /dev/null state=absent&quot;'
  172.16.20.115 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;envs&quot;: [],
  &quot;jobs&quot;: [
  &quot;Ajob&quot;
  ]
  }
  查看计划任务
  [root@Allen ~]# ansible test -m shell -a &quot;crontab -l&quot;
  172.16.20.115 | SUCCESS | rc=0 >>
  #Ansible: Ajob
  */5 * * * * /usr/sbin/ntpdate 172.16.8.100 &> /dev/null
  删除任务计划
  [root@Allen ~]# ansible test -m shell -a &quot;crontab -r&quot;
  172.16.20.115 | SUCCESS | rc=0 >>
  6.fetch模块   ===========================
  作用:从远程主机拉取文件到本地。一般情况下,只会从一个远程节点拉取数据
  常见参数有:
  dest=  从远程主机上拉取的文件存放在本地的位置,一般只能是目录
  src=   指明远程主机上要拉取的文件,只能是文件,不能是目录
  [root@Allen ~]# ansible test -m fetch -a 'src=/etc/passwd dest=/tmp'
  172.16.20.115 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;checksum&quot;: &quot;41b88ce7a15c857acd8d1d71d9b0e9f985643ba6&quot;,
  &quot;dest&quot;: &quot;/tmp/172.16.20.115/etc/passwd&quot;,
  &quot;md5sum&quot;: &quot;76945c2dd3a7b0b83f64ef91e6604bcf&quot;,
  &quot;remote_checksum&quot;: &quot;41b88ce7a15c857acd8d1d71d9b0e9f985643ba6&quot;,
  &quot;remote_md5sum&quot;: null
  }
  作用:用于设定远程主机上的文件属性
  常见参数有:
  path=   指明对哪个文件修改其属性
  src=   指明path=指明的文件是软链接文件,其对应的源文件是谁,必须要在state=link时才有用
  state=directory|link|absent   表示创建的文件是目录还是软链接
  owner=   指明文件的属主
  group=   指明文件的属组
  mode=   指明文件的权限
  创建软链接的用法:
  src=  path=  state=link
  修改文件属性的用法:
  path=  owner=  mode=  group=
  创建目录的用法:
  path=  state=directory
  删除文件:
  path=  state=absent
  7、file模块========================
  创建软连接:
  [root@Allen ~]# ansible test -m file -a 'src=/etc/passwd path=/tmp/passwd.link state=link'
  172.16.20.115 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;dest&quot;: &quot;/tmp/passwd.link&quot;,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;mode&quot;: &quot;0777&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;size&quot;: 11,
  &quot;src&quot;: &quot;/etc/passwd&quot;,
  &quot;state&quot;: &quot;link&quot;,
  &quot;uid&quot;: 0
  }
  查看刚创建的/tmp下的软连接
  [root@Allen ~]# ansible all -m shell -a 'ls -l /tmp/passwd.link'
  172.16.20.114 | SUCCESS | rc=0 >>
  lrwxrwxrwx 1 root root 11 Aug  1 18:45 /tmp/passwd.link -> /etc/passwd
  删除文件
  [root@Allen ~]# ansible test -m file -a 'path=/tmp/cc.txt state=absent'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: True,
  &quot;path&quot;: &quot;/tmp/cc.txt&quot;,
  &quot;state&quot;: &quot;absent&quot;
  }
  修改文件属性
  [root@Allen ~]# ansible test -m file -a 'path=/tmp/bb.txt mode=700 owner=root group=root'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;mode&quot;: &quot;0700&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;path&quot;: &quot;/tmp/bb.txt&quot;,
  &quot;size&quot;: 14,
  &quot;state&quot;: &quot;file&quot;,
  &quot;uid&quot;: 0
  }
  创建目录(可以递归创建,直接加上文件名即可)
  如果state=directory,那么如果目录不存在,那么所有的子目录将被创建(而且提供权限的创建)
  如果state=file,文件将不会被创建
  [root@Allen ~]# ansible test -m file -a 'path=/tmp/bj state=directory'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;mode&quot;: &quot;0755&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;path&quot;: &quot;/tmp/bj&quot;,
  &quot;size&quot;: 4096,
  &quot;state&quot;: &quot;directory&quot;,
  &quot;uid&quot;: 0
  }
  创建文件
  [root@Allen ~]# ansible all -m file -a 'name=d.txt state=touch'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;dest&quot;: &quot;d.txt&quot;,
  &quot;gid&quot;: 0,
  &quot;group&quot;: &quot;root&quot;,
  &quot;mode&quot;: &quot;0644&quot;,
  &quot;owner&quot;: &quot;root&quot;,
  &quot;size&quot;: 0,
  &quot;state&quot;: &quot;file&quot;,
  &quot;uid&quot;: 0
  }
  删除目录(可以递归删除,无需任何参数,直接加上)
  [root@Allen ~]# ansible test -m file -a 'path=/tmp/bj state=absent'
  172.16.20.114 | SUCCESS => {
  &quot;changed&quot;: true,
  &quot;path&quot;: &quot;/tmp/bj&quot;,
  &quot;state&quot;: &quot;absent&quot;
  }
  [root@Allen ~]# ansible test -m shell -a 'ls -l /tmp'
  172.16.20.114 | SUCCESS | rc=0 >>
  total 20
  drwx------ 2 root root 4096 Jul 31 22:40 ansible_KVKNun
  -rwx------ 1 root root   14 Jul 31 21:42 bb.txt
  -rw------- 1 root root   11 Jul 31 22:07 c.txt
  -rw-r--r-- 1 root root   15 Jul 31 21:58 ddd.txt
  -rwxr-xr-x 1 root root  158 Jul 31 21:51 hosts
  lrwxrwxrwx 1 root root   11 Jul 31 22:27 passwd.link -> /etc/passwd
  8.hostname模块============================
  作用:管理远程主机上的主机名
  常用参数有
  name=  指明主机名
  [root@Allen ~]# ansible test -m shell -a 'hostname'  //查看主机名
  172.16.20.114 | SUCCESS | rc=0 >>
  Allen.ansible.com
  [root@Allen ~]# ansible test -m hostname -a 'name=Allen'   //更改主机名
  172.16.20.114 | SUCCESS => {
  &quot;ansible_facts&quot;: {
  &quot;ansible_domain&quot;: &quot;&quot;,
  &quot;ansible_fqdn&quot;: &quot;Allen&quot;,
  &quot;ansible_hostname&quot;: &quot;Allen&quot;,
  &quot;ansible_nodename&quot;: &quot;Allen&quot;
  },
  &quot;changed&quot;: true,
  &quot;name&quot;: &quot;Allen&quot;
  }
  9.yum模块==============================
  作用:基于yum机制,对远程主机管理程序包
  常用参数有:
  name=     指明程序包的名称,可以带上版本号,不指明版本,就是默认最新版本
  name=httpd
  name=httpd-2.2.15

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542632-1-1.html 上篇帖子: Ansible运行setup模块卡住不动 下篇帖子: ansible主机清单Inventory说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表