Ansible Ad-hoc命令执行模块实战教程

2024-04-18 0 395
目录
  • Ad-hoc
    • Ad-hoc简介
    • Ad-hoc命令说明
    • Ad-hoc示例
  • 命令执行模块
    • 1. command模块
    • 2. shell模块
    • 3. raw模块
    • 4. script模块

Ad-hoc

Ad-hoc简介

  • Ad-hoc是Ansible下临时执行的一条命令,对于复杂的命令会使用playbook。Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块。如command,file,copy,shell等
  • 帮助查询
    • ansible-doc -l 列出所有模块
    • ansible-doc -s module 查看某个模块的参数
    • ansible-doc module 查看某个模块更详细的信息

Ad-hoc命令说明

  • 命令说明ansible 主机或组 -m 模块名 -a '模块参数' ansible参数
    • 主机和组:就是你要在哪些主机上执行这个命令,必须是配置文件里面定义好的
    • 模块名:可以通过ansibel-doc -l查看目前安装的模块,然后过滤出你想要的,默认不指定时,使用的时command模块,可以在配置文件里面找到 "module_name = command"改掉这个
    • 模块参数:可以通过ansible-doc 来查看模块需要使用哪些参数,以及具体用法
    • ansible参数:可以通过ansible 的帮助查到,有很多参数可以使用,比如是否提权,是否输入密码等等

Ad-hoc示例

[devops@node1 ansible]$ ansible node1 -m shell -a \’whoami\’
node1 | CHANGED | rc=0 >>
root
# 使用ad-hoc创建一个目录
[devops@node1 ansible]$ ansible node1 -m shell -a \’mkdir /ansibel\’
[WARNING]: Consider using the file module with state=directory rather than running \’mkdir\’. If you
need to use command because file is insufficient you can add \’warn: false\’ to this command task or
set \’command_warnings=False\’ in ansible.cfg to get rid of this message.
node1 | CHANGED | rc=0 >>

这里我们可以看到,使用shell模块创建一个目录的时候他有一个警告,大概就是说让我们考虑使用file模块去创建目录,但是这个是警告,并不是报错,这个目录也是被创建出来了

[devops@node1 ansible]$ sudo ls /ansibel -d
/ansibel

这就是一些简单的使用案例

命令执行模块

1. command模块

该模块通过-a跟上要执行的命令可以直接执行,不过命令里面不能带一些符号(|,>,<,&),否则会不成功。

# 正常的
[devops@node1 ansible]$ ansible all -m command -a \’whoami\’
node1 | CHANGED | rc=0 >>
root
node2 | CHANGED | rc=0 >>
root
# 错误的
[devops@node1 ansible]$ ansible all -m command -a \’whoami > /opt/who.txt\’
node2 | FAILED | rc=1 >>
whoami: extra operand ‘>\’
Try \’whoami –help\’ for more information.non-zero return code
node1 | FAILED | rc=1 >>
whoami: extra operand ‘>\’
Try \’whoami –help\’ for more information.non-zero return code

看到了吧,我想把输出内容重定向到某个文件内他是会报错的,那我们如果有这种需求改怎么做呢?来看下一个模块

2. shell模块

shell模块的用法基本和command一样,不过他是通过/bin/sh执行,所以shell模块可以执行任何命令,报错刚刚command不能执行的重定向操作

# 不带重定向
[devops@node1 ansible]$ ansible node1 -m shell -a \’whoami\’
node1 | CHANGED | rc=0 >>
root
# 带重定向
[devops@node1 ansible]$ ansible node1 -m shell -a \’whoami > /opt/who.txt\’
node1 | CHANGED | rc=0 >>
# 查看文件内容
[devops@node1 ansible]$ cat /opt/who.txt
root

这个模块确实是可以执行一些command执行不了的操作,shell模块还有一些选项

  • chdir: 在执行命令前,先切换到指定的目录,默认工作目录是用户的home目录
  • creates:一个文件名,当该文件存在,那么命令就不会执行
  • removes:一个文件名,当该文件不存在,那么命令不会执行后面这两个可能有点难以理解,我们通过实验来看看creates实验:

# 我们刚刚不是在node1上创建了一个/opt/who.txt吗?node2上是没有的,那么我们选择就来指定这个文件
[devops@node1 ansible]$ ansible all -m shell -a \’creates=/opt/who.txt whoami\’
node1 | SUCCESS | rc=0 >>
skipped, since /opt/who.txt exists
node2 | CHANGED | rc=0 >>
root

看到了吧,node1上有这个文件,那么它是没有去执行的,他直接跳过了,但是node2上没有这个文件,那么它执行了whoami这个命令

removes实验:

[devops@node1 ansible]$ ansible all -m shell -a \’removes=/opt/who.txt whoami\’
node1 | CHANGED | rc=0 >>
root
node2 | SUCCESS | rc=0 >>
skipped, since /opt/who.txt does not exist

通过这2个实验应该能理解这俩选项的作用了吧

3. raw模块

这个模块的用法和shell是一样的,不同点在于它没有chdir,creates和removes选项,其他都是一样一样的

4. script模块

这个模块就比较有意思了,他是将你主控端的脚本直接在被控端上执行,注意,他并不会将这个文件传过去

[devops@node1 ansible]$ cat test.sh
#!/bin/bash
ifconfig |grep netmask | awk -F\” \” \'{print $2}\’

注意看这个脚本,是在node1上的,也就是主控端,我们来使用过scripts模块来执行一下这个脚本,看看它是不是没有将文件传过去

[devops@node1 ansible]$ ansible all -m script -a \’test.sh\’
node2 | CHANGED => {
\”changed\”: true,
\”rc\”: 0,
\”stderr\”: \”Shared connection to node2 closed.\\r\\n\”,
\”stderr_lines\”: [
\”Shared connection to node2 closed.\”
],
\”stdout\”: \”/home/devops/.ansible/tmp/ansible-tmp-1708832697.440758-6677-176602102106477/test.sh: line 2: ifconfig: command not found\\r\\n\”,
\”stdout_lines\”: [
\”/home/devops/.ansible/tmp/ansible-tmp-1708832697.440758-6677-176602102106477/test.sh: line 2: ifconfig: command not found\”
]
}
node1 | CHANGED => {
\”changed\”: true,
\”rc\”: 0,
\”stderr\”: \”Shared connection to node1 closed.\\r\\n\”,
\”stderr_lines\”: [
\”Shared connection to node1 closed.\”
],
\”stdout\”: \”192.168.100.210\\r\\n192.168.200.131\\r\\n127.0.0.1\\r\\n192.168.122.1\\r\\n\”,
\”stdout_lines\”: [
\”192.168.100.210\”,
\”192.168.200.131\”,
\”127.0.0.1\”,
\”192.168.122.1\”
]
}

这里可以看到,他执行成功了,返回的是主机上所有的IP地址,然后我们去node2上看看这个文件是否不存在,不在node1上看是因为这个文件本来就是node1上写的,因为node1就是主控节点,它同时也是被控

[devops@node1 ansible]$ ansible node2 -m shell -a \’find / -name test.sh\’
node2 | CHANGED | rc=0 >>

我们可以看到,使用find去查到这个文件名他没有任何的输出,那么就是没有找到,也就是没有这个文件

到此这篇关于AnsibleAd-hoc命令执行模块的文章就介绍到这了,更多相关AnsibleAd-hoc模块内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!

您可能感兴趣的文章:

  • 集群运维自动化工具ansible的安装与使用(包括模块与playbook使用)

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 linux shell Ansible Ad-hoc命令执行模块实战教程 https://www.u-9.cn/jiaoben/linuxshell/186917.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务