Ansible Ad-Hoc(点对点模式)

官方文档:https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html

图片[1]-Ansible Ad-Hoc(点对点模式)-不念博客

简介

ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令,一般测试调试时用的多,ad-hoc简而言之,就是”临时命令“。

常用模块

1、command 模块(默认模块)

默认模块,没有shell强大,基本上shell模块都可以支持command模块的功能。

【1】帮助

ansible-doc command
# 推荐使用下面这个
ansible-doc command -s

【2】参数解释

  • free_form——必须参数,指定需要远程执行的命令。需要说明一点,free_form 参数与其他参数(如果想要使用一个参数,那么则需要为这个参数赋值,也就是name=value模式)并不相同。比如,当我们想要在远程主机上执行 ls 命令时,我们并不需要写成”free_form=ls” ,这样写反而是错误的,因为并没有任何参数的名字是 free_form,当我们想要在远程主机中执行 ls 命令时,直接写成 ls 即可。因为 command 模块的作用是执行命令,所以,任何一个可以在远程主机上执行的命令都可以被称为 free_form。
  • chdir——此参数的作用就是指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。
  • creates——看到 creates,你可能会从字面上理解这个参数,但是使用这个参数并不会帮助我们创建文件,它的作用是当指定的文件存在时,就不执行对应命令,比如,如果 /testdir/test文件存在,就不执行我们指定的命令。
  • removes——与 creates 参数的作用正好相反,它的作用是当指定的文件不存在时,就不执行对应命令,比如,如果 /testdir/tests 文件不存在,就不执行我们指定的命令,此参数并不会帮助我们删除文件。

【3】示例演示

# 上面命令表示在 web 主机上执行 ls 命令,因为使用的是 root 用户,所以默认情况下,ls 出的结果是 web 主机中 root 用户家目录中的文件列表。
ansible web -m command -a "ls"

# chdir 参数表示执行命令之前,会先进入到指定的目录中,所以上面命令表示查看 web 主机上 /testdir 目录中的文件列表,返回显示有2个文件。
ansible web -m command -a "chdir=/testdir ls"

# 下面命令表示 /testdir/testfile1 文件存在于远程主机中,则不执行对应命令。/testdir/testfile3 不存在,才执行”echo test”命令。
ansible web -m command -a "creates=/testdir/testfile1 echo test"

# 下面命令表示 /testdir/testfile3 文件不存在于远程主机中,则不执行对应命令。/testdir/testfile1 存在,才执行”echo test”命令。
ansible web -m command -a "removes=/testdir/testfile1 echo test"

2、shell 模块

shell模块 [执行远程主机的shell/python等脚本]。

【1】查看帮助

ansible-doc shell -s

【2】示例演示

# -o:一行显示
# 安装httpd
ansible web -m shell -a 'yum -y install httpd' -o

# 查看时间
ansible web -m shell -a 'uptime' -o

3、script 模块

script模块 [在远程主机执行主控端的shell/python等脚本 ]。

【1】查看帮助

ansible-doc script -s

【2】参数解释

  • free_form——必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。
  • chdir——此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。
  • creates——使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
  • removes——使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。

【3】示例演示

# 下面命令表示 ansible 主机中的 /testdir/testscript.sh 脚本将在 web 主机中执行,执行此脚本之前,会先进入到 web 主机中的 /opt 目录
ansible web -m script -a "chdir=/opt /testdir/testscript.sh"

# 下面命令表示,web主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 web 主机中执行。
ansible web -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh"

# 下面命令表示,web 主机中的 /testdir/testfile1 文件存在,ansible 主机中的 /testdir/testscript.sh 脚本则会在 web 主机中执行。
ansible ansible-demo3 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"

4、raw 模块

raw模块 [类似于command模块、支持管道传递]。

【1】查看帮助

ansible-doc raw -s

【2】示例演示

ansible web -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print \$2}' |awk -F: '{print \$2}'"

5、copy 模块

copy 模块 从主控端复制文件到被控端。

【1】查看帮助

ansible-doc copy -s

【2】示例演示

# -a,--args:后面接参数
ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777'

# backup=yes/no:文件存在且文件内容不一样是否备份,默认不备份
ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777 backup=yes'

6、fetch 模块

copy 模块从被控端复制文件到主控端,正好跟copy相反。

【1】查看帮助

ansible-doc fetch -s

【2】示例演示

# 跟copy支持的参数差不多,src:远端主机的目录,dest:主控端目录,其实真正存放的目录在:/tmp/192.168.182.129/tmp/up.sh,会按每台主机分组存放
#  This `must' be a file, not a directory:只支持单个文件获取
ansible 192.168.182.129 -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"

7、unarchive 模块(解包模块)

unarchive 模块是解包模块。

【1】查看帮助

ansible-doc unarchive -s

【2】参数解释

  • copy——默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件。
  • src——源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no。
  • dest——远程主机上的目标路径。
  • mode——设置解压缩后的文件权限。

【3】示例演示

ansible 192.168.182.129 -m unarchive -a 'src=/testdir/ansible/data.tar.gz dest=/tmp/tmp/'

8、archive模块(打包模块)

unarchive 模块是打包模块。

【1】查看帮助

ansible-doc archive -s

【2】示例演示

# path:主控端目录,format:压缩格式,dest:被控端目录文件'
ansible 192.168.182.129 -m archive -a 'path=/tmp/ format=gz dest=/tmp/tmp/t.tar.gz'

9、user 模块

【1】查看帮助

ansible-doc user -s

【2】示例演示

# 创建用户(present:默认,可以不写)
ansible web -m user -a 'name=test state=present'

# 删除用户(absent)
ansible web -m user -a 'name=test state=absent'

# 修改密码
# 步骤一、生成加密密码
echo '777777'|openssl passwd -1 -stdin

# 步骤二、修改秘密
ansible web -m user -a 'name=test password="$1$Jo5FD9Jr$2QB.BuybbtR35ga4O5o8N."'

# 修改shell
ansible web -m user -a 'name=test shell=/sbin/noglogin append=yes'

10、group 模块

【1】查看帮助

ansible-doc group -s

【2】示例演示

# 创建
ansible 192.168.182.129 -m group -a 'name=testgroup system=yes'
# 删除
ansible 192.168.182.129 -m group -a 'name=testgroup state=absent'

11、yum 模块

【1】查看帮助

ansible-doc yum -s

【2】示例演示

# 升级所有包
ansible web -m yum -a 'name="*" state=latest'

# 安装apache
ansible web -m yum -a 'name="httpd" state=latest'

12、service 模块

【1】查看帮助

ansible-doc service -s

【2】示例演示

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

ansible web -m service -a 'name=httpd state=started enabled=yes'

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

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

ansible web -m service -a 'name=httpd state=started enabled=no'

13、file 模块

【1】查看帮助

ansible-doc file -s

【2】示例演示

# 创建文件
ansible web -m file -a 'path=/tmp/88.txt mode=777 state=touch'

# 创建目录
ansible web -m file -a 'path=/tmp/99 mode=777 state=directory'

# 删除
ansible web -m file -a 'path=/tmp/99 state=absent'

14、setup 模块

【1】查看帮助

ansible-doc setup -s

【2】示例演示

ansible web -m setup

ansible web -m setup -a 'filter=ansible_all_ipv4_addresses'

15、cron 模块

【1】查看帮助

ansible-doc cron -s

【2】示例演示

# 创建定时任务
ansible 192.168.182.129 -m cron -a 'minute=* weekday=1,3,5,6,7 job="/usr/bin/wall FBI warning" name=warningcron'

# 关闭定时任务
ansible 192.168.182.129 -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron'

# 删除定时任务
ansible 192.168.182.129 -m cron -a ' job="/usr/bin/wall FBI warning" name=warningcron state=absent'

16、hostname 模块

【1】查看帮助

ansible-doc hostname -s

【2】示例演示

ansible 192.168.182.129 -m hostname -a 'name=192.168.182.129'
© 版权声明
THE END
喜欢就支持一下吧
点赞81赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容