Linux--CE--ansible常用模块(1)

本文详细介绍了在Linux CentOS系统中使用Ansible进行自动化运维时的几个关键模块,包括command、shell、raw、script模块的区别和使用示例,以及file模块的文件和目录操作,copy模块的文件复制,fetch模块的文件获取,和synchronize模块的目录同步功能。这些模块在服务器管理中起到重要作用。
摘要由CSDN通过智能技术生成

1.command,shell,raw,script模块的作用和区别

#:   command可以在受管主机上执行shell命令,但是不支持环境变量和操作符,也无法使用管道符,并要求受管主机安装python;

        shell模块调用的、bin/sh指令执行,要求在受管主机安装python;

        raw不需要受管主机上安装python,直接使用远程shell运行命令,将结果返回给主控服务器;

#:command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo "I amcommand module"

(1)首先去被控主机中去创建command.sh脚本:可以在所有被控主机创建,也可以指定哪个被控主机创建,我这里指定在node1主机中进行创建:

[root@rhces1 ~]# vim command.sh
[root@rhces1 ~]# chmod +x command.sh 
[root@rhces1 ~]# ./command.sh 
I am command module
[root@rhces1 ~]# cat command.sh 
#!/bin/bash
echo "I am command module"
[root@rhces1 ~]# 

(2)在控制主机中执行command.sh脚本:

[root@rhce-128 ~]# ansible node -m command -a "bash /home/student/command.sh"
node2.example.com | CHANGED | rc=0 >>
I am command module
node1.example.com | CHANGED | rc=0 >>
I am command modual
[root@rhce-128 ~]#

#:shell模块执行命令 ls /root | grep txt

[root@rhces1 ~]# touch file{1..2}.txt
[root@rhces1 ~]# ls /root|grep txt
file1.txt
file2.txt
[root@rhces1 ~]#

-----------------------------------------------

[root@rhces2 ~]# touch file{1..2}.txt
[root@rhces2 ~]# ls /root | grep txt
file1.txt
file2.txt
[root@rhces2 ~]#
[root@rhce-128 ~]# ansible node -m shell -a "ls /root|grep txt chdir=/root"
node2.example.com | CHANGED | rc=0 >>
file1.txt
file2.txt
node1.example.com | CHANGED | rc=0 >>
file1.txt
file2.txt
[root@rhce-128 ~]#

#:raw模块执行pwd命令

[root@rhce-128 ~]# ansible node -m raw -a "pwd"
node2.example.com | CHANGED | rc=0 >>
/root
Shared connection to node2.example.com closed.

node1.example.com | CHANGED | rc=0 >>
/root
Shared connection to node1.example.com closed.

[root@rhce-128 ~]#

#:script模块执行 script.sh文件,文件的内容为 echo "I am script module"

[root@rhce-128 ~]# vim /root/script.sh
[root@rhce-128 ~]# chmod +x script.sh 
[root@rhce-128 ~]# ansible node -m script -a "script.sh chdir=/root"
node2.example.com | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to node2.example.com closed.\r\n", 
    "stderr_lines": [
        "Shared connection to node2.example.com closed."
    ], 
    "stdout": " I am script module\r\n", 
    "stdout_lines": [
        " I am script module"
    ]
}
node1.example.com | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to node1.example.com closed.\r\n", 
    "stderr_lines": [
        "Shared connection to node1.example.com closed."
    ], 
    "stdout": " I am script module\r\n", 
    "stdout_lines&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值