Ansible - 模块 - shell
概述
- ansible 的 shell 模块
准别
ansible 控制节点
- ansible
- 2.8.1
- ansible
远程节点
- OS
- CentOS 7.5
- 无密码登录
- 已经打通
- OS
1. 模块
概述
- ansible 功能的具体实现
模块
- 本质
- ansible 携带的 功能模块 lib
- 不同的 模块, 实现了不同的功能
- 模块通常的作用
- 操作系统资源
- 执行系统命令
- 操作系统文件
- 如果没有理想的模块, 当然可以自己写
- 本质
2. shell
- 概述
- 执行 shell 命令的模块
- 比较基础
- 有些缺点
- 建议用 command 模块替代
- 但还是值得一讲
1. 基本
概述
- 最基本的命令
命令1: pwd
命令
# -m 使用的是 shell
# -a 是要被执行的命令
> ansible -i hosts demo -m shell -a 'pwd'
结果
demo | CHANGED | rc=0 >>
/root
疑问
默认位置
- 默认的 pwd, 是 默认用户的 home 目录
- 默认的 用户, 是 root
- 默认的 pwd, 是 默认用户的 home 目录
我想要换目录, 那该怎么办呢?
playbook
# playbook02.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'pwd'
# ansible-playbook -i hosts playbook02.yml
- hosts: servers
# 关闭 采集
gather_facts: false
tasks:
- name: shell test pwd
shell: pwd
命令2: 尝试换个目录
命令
> ansible -i hosts demo -m shell -a 'cd /etc'
结果
# 貌似没有任何返回啊...
demo | CHANGED | rc=0 >>疑问
- 如果地址输错了, 会怎么样
结果
demo | FAILED | rc=1 >>
/bin/sh: line 0: cd: /etcd: No such file or directorynon-zero return code
- 如果地址输错了, 会怎么样
playbook
playbook03.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'cd /etc'
# ansible-playbook -i hosts playbook03.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: cd /etc
命令3: 换个目录再查看
命令
# ; 可以分割命令
> ansible -i hosts demo -m shell -a 'cd /etc; pwd'
结果
demo | CHANGED | rc=0 >>
/etc
疑问
- 如果换目录错了, 会怎么样呢
结果
# 前面的命令报错了, 后面的命令还是在执行
# 这个就有点考验人了
demo | CHANGED | rc=0 >>
/root/bin/sh: line 0: cd: /etcd: No such file or directory
- 如果换目录错了, 会怎么样呢
playbook
# 这个 tasks 下面, 有两个 shell 任务
playbook04.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'cd /etc; pwd'
# ansible-playbook -i hosts playbook04.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: cd /etc
- name: pwd
shell: pwd
命令4: 另一种换目录的方式
命令
> ansible -i hosts demo -m shell -a 'chdir=/etc pwd'
结果
demo | CHANGED | rc=0 >>
/etc
playbook
playbook05.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'chdir=/etc pwd'
# ansible-playbook -i hosts playbook05.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: pwd
args:
chdir: /etc
疑问
如果换目录错了, 会怎么样呢
结果
# 会有报错, 后面代码不会执行
# 建议使用这种方式来切换目录
demo | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to 192.168.2.xxx closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible-tmp-1569235278.83-13414444200011/AnsiballZ_command.py\", line 114, in <module>\r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1569235278.83-13414444200011/AnsiballZ_command.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1569235278.83-13414444200011/AnsiballZ_command.py\", line 49, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_command_payload_dCXTDZ/__main__.py\", line 327, in <module>\r\n File \"/tmp/ansible_command_payload_dCXTDZ/__main__.py\", line 263, in main\r\nOSError: [Errno 2] No such file or directory: '/etcd'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
```又有一个新问题
- 如果可以这样
- ansible 发现目录不存在, 就不执行了
- 这个真的有
- ansible 发现目录不存在, 就不执行了
- 如果可以这样
命令6: removes
命令
# 如果没有 /etcd, 就不执行命令了
> ansible -i hosts demo -m shell -a 'removes=/etcd pwd'
结果
demo | SUCCESS | rc=0 >>
skipped, since /etcd does not exist
playbook
playbook06.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'removes=/etcd pwd'
# ansible-playbook -i hosts playbook06.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: cd /etc
args:
removes: /etcd
疑问
如果同时使用 removes 和 chdir 两个参数会如何呢
目录存在
> ansible -i hosts demo -m shell -a 'removes=/etcd chdir=/etc pwd'
demo | SUCCESS | rc=0 >>
skipped, since /etcd does not exist目录不存在
# 不存在还是会有问题啊...
> ansible -i hosts demo -m shell -a 'removes=/etcd chdir=/etcd pwd'
demo | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to 192.168.2.135 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible-tmp-1569236237.29-269825187752078/AnsiballZ_command.py\", line 114, in <module>\r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1569236237.29-269825187752078/AnsiballZ_command.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1569236237.29-269825187752078/AnsiballZ_command.py\", line 49, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_command_payload_YsMUPL/__main__.py\", line 327, in <module>\r\n File \"/tmp/ansible_command_payload_YsMUPL/__main__.py\", line 263, in main\r\nOSError: [Errno 2] No such file or directory: '/etcd'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
有 目录 存在不执行 的参数吗
- 有的
命令7: creates
命令
> ansible -i hosts demo -m shell -a 'creates=/etcd pwd'
结果
demo | CHANGED | rc=0 >>
/root
playbook
playbook07.yml
---
# 测试 shell
# ansible -i hosts demo -m shell -a 'creates=/etcd pwd'
# ansible-playbook -i hosts playbook07.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: cd /etc
args:
creates: /etcd
疑问
- 如果放多个参数, 会是怎么样的关系
- 简单试了试, 貌似是 与 的关系
- 想做 或 操作, 但是我没有找到
- 如果放多个参数, 会是怎么样的关系
其他
- shell 还有其他的属性, 这里就不在多说了
- command 模块其实更加适合执行命令, 有更多的有点
- 如果你使用 shell 命令, 做 rpm 操作时
- ansible 会提示使用 yum, dnf 或者 zypper
- 其他的模块, 也了解下吧
ps
Ansible - 模块 - shell的更多相关文章
- ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory
命令: ansible -i hosts_20 st -m shell -a 'service zabbix_agentd star' -K --become ansible -i hosts_2 ...
- ansible模块command、shell、raw、script
简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...
- ansible使用shell模块在受控机上执行命令(ansible2.9.5)
一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...
- ansible模块
ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...
- 第4天:Ansible模块
Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...
- ansible笔记(3):ansible模块的基本使用
ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...
- win10的pycharm中安装ansible模块过程
前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...
- ansible模块之command、shell、script、file、copy、fetch
前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...
- ansible基本模块-shell
ansible XXX -m shell -a "XXX"
随机推荐
- wx: wx.showModal 回调函数中调用自定义方法
一.在回调函数中调用自定义方法: 回调函数中不能直接使用this,需要在外面定义 var that = this 然后 that.自定义的方法.如下: //删除 onDelete: function ...
- HDFS写入数据
HDFS副本摆放策略 不同的版本副本摆放策略可能并不一致,HDFS主要采用一种机架感知(rack-ware)的机制来实现摆放策略. 由于不同的机架上节点间通信要通过交换机(switches),同一机架 ...
- Python之二:基础知识
1.常量: 1.1.数:5.1.23.9.25e-3 4种类型的数——整数.长整数.浮点数和复数 2是一个整数的例子. 长整数不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示1 ...
- sort函数的用法(C++排序库函数的调用)
对数组进行排序,在c++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了. (一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c+ ...
- php中普通类 接口类 抽象类 浅谈
一.普通类 1.关键词:class 类名,继承关键字extends 2.继承:只能实现单继承, 3.多态:子类继承可以实现多种功能 4.封装:类有权限机制,私有的只能自己用,受保护的可以被继承,子类 ...
- JMeter概念
1. Test Plan 测试计划 Test Plan也就是测试计划,概念有点类似eclipse里面的project(项目.工程). 一个JMeter测试计划有很多种测试元素组成.一般至少包含一个T ...
- Spring Boot Post、Get接收Map
原文地址:https://blog.csdn.net/java0311/article/details/81671754 Post: @RequestBody Map param Get: @Req ...
- ORA-00904: "I_LEVEL": invalid identifier
问题描述 ORA-00904: "I_LEVEL": invalid identifier 标示符无效
- orcal时区
查询数据库时区 select dbtimezone from dual 修改时区 alter database set time_zone='+8:00';
- memcached和redis对比
关于memcached和redis的使用场景,总结如下:两者对比: redis提供数据持久化功能,memcached无持久化. redis的数据结构比memcached要丰富,能完成场景以外的事情: ...