设为首页 加入收藏

TOP

ansible常用模块的介绍与使用(一)
2023-07-23 13:38:52 】 浏览:155
Tags:ansible 常用模

ansible常用模块的介绍与使用

ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块rawcommandshell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

[root@ansible ~]# ansible all -m ping
192.168.118.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.118.131 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.118.131 port 22: No route to host",
    "unreachable": true
}
#没连通

ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。

//查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 192.168.118.130 -a 'ls /tmp'
192.168.118.130 | CHANGED | rc=0 >>
ansible_command_payload_3bvoyhhc
ks-script-889z9ogn
ks-script-9gz23mj9
vmware-root_893-3988097506
vmware-root_922-2722632355
vmware-root_930-2722763397

//在受控主机的/tmp目录下新建一个文件test
[root@ansible ~]# ansible 192.168.118.130 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  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.
192.168.118.130 | CHANGED | rc=0 >>

[root@ansible ~]# ansible 192.168.118.130 -a 'ls /tmp'
192.168.118.130 | CHANGED | rc=0 >>
ansible_command_payload_uyqqnupx
ks-script-889z9ogn
ks-script-9gz23mj9
test
vmware-root_893-3988097506
vmware-root_922-2722632355
vmware-root_930-2722763397

//command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.118.130 -a "echo 'hello world' > /tmp/test"
192.168.118.130 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.118.130 -a 'cat /tmp/test'
192.168.118.130 | CHANGED | rc=0 >>

[root@ansible ~]# ansible 192.168.118.130 -a 'ps -ef|grep vsftpd'
192.168.118.130 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

//支持重定向
[root@ansible ~]# ansible 192.168.118.130 -m raw -a "echo 'hello world' > /tmp/test"
192.168.118.130 | CHANGED | rc=0 >>
Shared connection to 192.168.118.130 closed.

[root@ansible ~]# ansible 192.168.118.130 -a 'cat /tmp/test'
192.168.118.130 | CHANGED | rc=0 >>
hello world

//支持管道符
[root@ansible ~]# ansible 192.168.
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ansible部署LNMP 下一篇海思3516系列芯片SPI速率慢问题深..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目