06-Ansible常用模块-command模块

简介: 06-Ansible常用模块-command模块

一、概述
command 模块可以帮助我们在远程主机上执行命令。
注意:使用 command 模块在远程主机中执行命令时,不会经过远程主机的 shell 处理,在使用 command 模块时,如果需要执行的命令中含有重定向、管道符等操作时,这些符号也会失效,比如”<”, “>”, “|”, “;” 和 “&” 这些符号,如果你需要这些功能,可以参考后面介绍的 shell 模块。还有一点需要注意,如果远程节点是 windows 操作系统,则需要使用 win_command 模块。
执行 ansible 时,不加 -m 默认使用 command ,可以在 /etc/ansible/ansible.cfg 中修改。

# default module name for /usr/bin/ansible
#module_name = command

二、常用参数
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 文件不存在,就不执行我们指定的命令,此参数并不会帮助我们删除文件。
三、示例

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "ls"
ansible-demo3 | SUCCESS | rc=0 >>
anaconda-ks.cfg
CentOS7-Base-163.repo
Centos-7.repo

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

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "chdir=/testdir ls"
ansible-demo3 | SUCCESS | rc=0 >>
testfile1
testfile2

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

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile1 exists

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test

上面命令表示 /testdir/testfile1 文件存在于远程主机中,则不执行对应命令。/testdir/testfile3 不存在,才执行”echo test”命令。

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile3 does not exist

上面命令表示 /testdir/testfile3 文件不存在于远程主机中,则不执行对应命令。/testdir/testfile1 存在,才执行”echo test”命令。

目录
相关文章
|
12月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
305 0
|
2月前
|
缓存 Shell Linux
[ansible]常用内置模块
[ansible]常用内置模块
|
3月前
|
Shell 应用服务中间件 Linux
Ansible的常用模块
Ansible的常用模块
63 6
|
3月前
|
Shell 数据安全/隐私保护
Ansible Ad-hoc,命令执行模块
Ansible Ad-hoc,命令执行模块
33 1
|
3月前
|
运维 Linux 应用服务中间件
Linux之自动化运维工具ansible、ansible模块(2)
Linux之自动化运维工具ansible、ansible模块(2)
|
3月前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
5月前
|
算法 安全 Linux
Ansible 中的copy 复制模块应用详解
Ansible 中的copy 复制模块应用详解
319 1
|
12月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
177 0
|
11月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
121 1
|
11月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
91 0