ansible2

一种叫ad-hoc还有一种叫playbook。
1.ansible中ad-hoc的方式调用模块
ad-hoc是临时设定,利用ansible命令直接完成管理
简单来说,ad-hoc就是命令行,playbook就是ansible中的shell脚本

[root@bastion ansible]# ansible-doc -l

可以查看ansible所有的模块

[root@bastion ansible]# ansible-doc user

查看user模块

[root@bastion ansible]# ansible-doc  -s ping

-s,查看ping模块的playbook片段

[root@bastion ansible]# ansible prod -m ping -vvv

v加越多,解释越详细

[root@bastion ansible]# ansible prod -m shell -a 'useradd test' -C

-C,表示检测,并不执行这个操作,但是会检测此操作的可行性。

-K提示输入sudo的密码
-b执行sudo切换身份操作
例:

[root@bastion ansible]# ansible prod -m shell -a 'whoami' -b --become-user=devops

绿色成功仅查看
黄色成功并改变
红色执行失败
2.command模块和shell模块

[root@bastion ansible]# ansible-doc command -s
- name: Execute commands on targets
  command:
      argv:                  # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise
                               be interpreted incorrectly (for example "user name"). Only the string or the
                               list form can be provided, not both.  One or the other must be provided.
      chdir:                 # Change into this directory before running the command.
      cmd:                   # The command to run.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
      free_form:             # The command module takes a free form command to run. There is no actual parameter named 'free form'.
      removes:               # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # If set to `yes', append a newline to stdin data.
      strip_empty_ends:      # Strip empty lines from the end of stdout/stderr in result.
      warn:                  # Enable or disable task warnings.

常用的是chdir,cmd,creates,removes

[root@bastion ansible]# ansible prod -m command -a 'chdir=/etc cat passwd'
172.25.250.12 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync

可以查看/etc/passwd的内容
但是如果去掉chdir

[root@bastion ansible]# ansible prod -m command -a 'cat passwd'
172.25.250.11 | FAILED | rc=1 >>
cat: passwd: No such file or directorynon-zero return code
172.25.250.12 | FAILED | rc=1 >>
cat: passwd: No such file or directorynon-zero return code

报错,chdir就是切换工作目录

[root@bastion ansible]# ansible prod -m command -a 'creates=/mnt/file touch /mnt/file'

创建了一个文件

[root@bastion ansible]# ansible prod -m command -a 'creates=/mnt/file rm -rf /mnt/file'

由于文件存在所以跳过,不执行

[root@bastion ansible]# ansible prod -m command -a 'removes=/mnt/file rm -rf /mnt/file'

由于文件存在所以执行,删除了文件
3.script模块

[root@bastion ansible]# vim /mnt/test.sh
[root@bastion ansible]# sh /mnt/test.sh 
bastion.exam.com

编写一个脚本

[root@bastion ansible]# cat /mnt/test.sh 
#!/bin/bash
echo $HOSTNAME

如何在受控主机执行?

[root@bastion ansible]# ansible prod -m script -a '/mnt/test.sh'

ansible + 组 + -m + 模块(script)+ -a ‘执行的脚本’

4.copy模块
把主机的文件复制到受控主机里
!!!如果不在自己私有化的ansible目录中,会读取不到自己设置的文件
ansible 的机制就是不在自己的就选择默认的

[root@bastion ansible]# ansible prod -m copy -a "src=/mnt/xixi dest=/mnt/"

src=sourcr起点
dest=destnation终点
从哪来到哪去

[root@bastion ansible]# ansible prod -m copy -a "src=/mnt/xixi dest=/mnt/ mode=755"

复制的同时赋予755权限

[root@bastion ansible]# ansible prod -m copy -a "src=/mnt/xixi dest=/mnt/ mode=755 owner=devops group=devops"

复制的时候指定所有人和所有组

[root@bastion ansible]# ansible prod -m copy -a 'content="hello xixi\n" dest=/mnt/hehe mode=755 owner=devops group=devops'

content=指定内容,复制到一个新文件中。\n是换行符

[root@bastion ansible]# ansible prod -m copy -a 'content="hello xixi hahahaha\n" dest=/mnt/hehe mode=755 owner=devops group=devops backup=yes'

backup=yes
不加的话复制过去,如果文件名一样的话会覆盖,加了的话,就会自动把源文件进行备份
5.fetch模块
fetch=抓取,和copy刚好相反,把受控主机的文件复制过来
src=源文件
dest=本机目录

[root@bastion ansible]# ansible prod -m fetch -a 'src=/etc/hostname dest=/mnt/aha flat=yes'

从prod组抓取,flat=yes表示基本名称功能。
加了flat表示最后des生成的是一个文件,不加的话会覆盖掉
5.file模块
主要作用是设定文件属性

[root@bastion ansible]# ansible prod -m file -a 'path=/mnt/haha state=touch'

建立文件,state=touch

[root@bastion ansible]# ansible prod -m file -a 'path=/mnt/haha state=absent'

state=absent(缺席)
删除文件

[root@bastion ansible]# ansible prod -m file -a 'src=/mnt/hahafile dest=/mnt/hehefile state=link'

形成链接
recurse=yes=chmod -r递归修改
6.ansible中的压缩管理

[root@bastion ansible]# ansible prod -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=devops mode=755'

archive打包压缩
path=要打包的路径
dest=要打包到哪里
format=格式

[root@bastion ansible]# ansible prod -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt copy=no mode=777'

unarchieve解压
src=source来源
copy=no时,会从受控主机里寻找需要解压的文件,反之则会从ansible主机寻找需要解压的文件
mode=777在此不生效。因为copy=no后是从受控主机中寻找src源文件的,而=yes则是从ansible主机中复制文件给受控主机的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值