博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Ansible基础 - 04命令执行模块

Posted on 2022-01-07 14:46  Kingdomer  阅读(144)  评论(0编辑  收藏  举报

Ansible基础 - 04命令执行模块

一、概述

command、 shell、 script、 raw

二、Command模块: 在远程主机执行命令

[root@cl-server ~]# ansible cl -m command -a 'ip a'

[root@cl-server ~]# ansible cl -m command -a 'chdir=/tmp ls'

[soupman@cl-server ~]$ ansible cl-node03 -m command -a "uname -n" 
cl-node03 | CHANGED | rc=0 >>
cl-node03

 

### 在 /tmp 目录下,有 aa.txt, bb.txt 两个文件
### 当aa.txt存在时,不执行命令 [root@cl-server ~]# ansible cl-node01 -a 'chdir=/tmp creates=aa.txt ls' cl-node01 | SUCCESS | rc=0 >> skipped, since aa.txt exists ### 当aa.txt不存在时,不执行命令; aa.txt 存在时,执行命令 [root@cl-server ~]# ansible cl-node01 -a 'chdir=/tmp removes=aa.txt ls' cl-node01 | CHANGED | rc=0 >> aa.txt ### 当cc.txt不存在,aa.txt存在时,执行命令 [root@cl-server ~]# ansible cl-node01 -a 'chdir=/tmp creates=cc.txt removes=aa.txt ls' cl-node01 | CHANGED | rc=0 >> aa.txt ### 当bb.txt不存在,aa.txt存在时,执行命令; 关系是 & [root@cl-server ~]# ansible cl-node01 -a 'chdir=/tmp creates=bb.txt removes=aa.txt ls' cl-node01 | SUCCESS | rc=0 >> skipped, since bb.txt exists

 

### command模块执行脚本, 执行的是远程主机上的脚本。
[soupman@cl-server cp]$ ansible cl-node03 -m command -a "sh /home/soupman/add-line.sh"
cl-node03 | CHANGED | rc=0 >>

[soupman@cl-server cp]$ ansible cl-node03 -m command -a "/home/soupman/add-line.sh"
cl-node03 | FAILED | rc=13 >>
[Errno 13] 权限不够

 

三、Shell模块

[root@cl-server ~]# ansible cl -m shell -a 'cat /etc/passwd '
cl-node02 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
[root@cl-server ~]# ansible cl -m shell -a 'cat /etc/passwd | grep root' [root@cl-server ~]# ip a | grep "inet 192" | awk '{print $2}' | awk -F'/' '{print $1}' 192.168.234.6

[root@cl-server playbooks]# ansible cl-node03 -m shell -a "ip a | grep 'inet 192' | awk '{print $2}' | awk -F'/' '{print $1}'" cl-node03 | CHANGED | rc=0 >> inet 192.168.234.13/24 brd 192.168.234.255 scope global noprefixroute br0 [root@cl-server playbooks]# ansible cl-node03 -m shell -a "ip a | grep 'inet 192' | awk '{print \$2}' | awk -F'/' '{print \$1}'" cl-node03 | CHANGED | rc=0 >> 192.168.234.13

 

[soupman@cl-node03 tmp]$ ll
总用量 188992
-rw-r--r-- 1 soupman soupman       176 11月 16 16:17 foo.conf
lrwxrwxrwx 1 root    root           13 11月 16 16:26 foo.link -> /tmp/foo.conf

[root@cl-server playbooks]# ansible cl-node03 -m command -a 'ls /tmp/foo.* -l'
cl-node03 | FAILED | rc=2 >>
ls: 无法访问/tmp/foo.*: 没有那个文件或目录non-zero return code

[root@cl-server playbooks]# ansible cl-node03 -m shell -a 'ls /tmp/foo.* -l'
cl-node03 | CHANGED | rc=0 >>
-rw-r--r-- 1 soupman soupman 176 11月 16 16:17 /tmp/foo.conf
lrwxrwxrwx 1 root    root     13 11月 16 16:26 /tmp/foo.link -> /tmp/foo.conf

 

Shell模块, 执行远程主机上的指定脚本。

### 脚本未添加执行权限
[soupman@cl-server ~]$ ansible cl-node03 -m shell -a "/home/soupman/add-line.sh"
cl-node03 | FAILED | rc=126 >>
/bin/sh: /home/soupman/add-line.sh: 权限不够non-zero return code
### 脚本添加执行权限,成功
[root@cl-server ~]# ansible cl-node03 -m shell -a '/home/soupman/add-line.sh'

[soupman@cl-server ~]$ ansible cl-node03 -m shell -a "sh /home/soupman/add-line.sh"
cl-node03 | CHANGED | rc=0 >>

 

四、script 模块:在远程主机执行Ansible控制端的脚本

[root@cl-server ~]# vi df.sh
[root@cl-server ~]# ansible cl -m script -a '~/df.sh'

### Script模块: 执行Ansible服务器上的脚本
[soupman@cl-server ~]$ ansible cl-node03 -m script -a "/home/soupman/add-line.sh"
cl-node03 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to cl-node03 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to cl-node03 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

  

五、raw模块: 支持管道传递 

[soupman@cl-server cp]$ ansible cl-node03 -m raw -a "ip a| grep 'inet 192' | awk '{print \$2}' | awk -F'/' '{print \$1}'"
cl-node03 | CHANGED | rc=0 >>
192.168.234.13
Shared connection to cl-node03 closed.