- 概述
- ansible 的 shell 模块
- 准别
- ansible 控制节点
- ansible
- 2.8.1
- 远程节点
- OS
- CentOS 7.5
- 无密码登录
- 已经打通
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
- 我想要换目录, 那该怎么办呢?
- 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'
# ansible-playbook -i hosts playbook04.yml
- hosts: servers
gather_facts: false
tasks:
- name: shell test cd
shell: cd /etc
- 命令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: cd /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 发现目录不存在, 就不执行了
- 这个真的有
- 命令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
- 目录不存在
1. # 不存在还是会有问题啊... > 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
- ref