1. 概述

    1. ansible 的 shell 模块
  2. 准别

    1. ansible 控制节点

      1. ansible

        1. 2.8.1
    2. 远程节点

      1. OS

        1. CentOS 7.5
      2. 无密码登录
        1. 已经打通

1. 模块

  1. 概述

    1. ansible 功能的具体实现
  2. 模块

    1. 本质

      1. ansible 携带的 功能模块 lib
      2. 不同的 模块, 实现了不同的功能
      3. 模块通常的作用
        1. 操作系统资源
        2. 执行系统命令
        3. 操作系统文件
      4. 如果没有理想的模块, 当然可以自己写

2. shell

  1. 概述

    1. 执行 shell 命令的模块
    2. 比较基础
      1. 有些缺点
      2. 建议用 command 模块替代
      3. 但还是值得一讲

1. 基本

  1. 概述

    1. 最基本的命令
  2. 命令1: pwd

    1. 命令

      # -m 使用的是 shell
      # -a 是要被执行的命令
      > ansible -i hosts demo -m shell -a 'pwd'
    2. 结果

      demo | CHANGED | rc=0 >>
      /root
    3. 疑问

      1. 默认位置

        1. 默认的 pwd, 是 默认用户的 home 目录

          1. 默认的 用户, 是 root
      2. 我想要换目录, 那该怎么办呢?

    4. 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
  3. 命令2: 尝试换个目录

    1. 命令

      > ansible -i hosts demo -m shell -a 'cd /etc'
    2. 结果

      # 貌似没有任何返回啊...
      demo | CHANGED | rc=0 >>
    3. 疑问

      1. 如果地址输错了, 会怎么样

        1. 结果

          demo | FAILED | rc=1 >>
          /bin/sh: line 0: cd: /etcd: No such file or directorynon-zero return code
    4. 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
  4. 命令3: 换个目录再查看

    1. 命令

      # ; 可以分割命令
      > ansible -i hosts demo -m shell -a 'cd /etc; pwd'
    2. 结果

      demo | CHANGED | rc=0 >>
      /etc
    3. 疑问

      1. 如果换目录错了, 会怎么样呢

        1. 结果

          # 前面的命令报错了, 后面的命令还是在执行
          # 这个就有点考验人了
          demo | CHANGED | rc=0 >>
          /root/bin/sh: line 0: cd: /etcd: No such file or directory
    4. 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
  5. 命令4: 另一种换目录的方式

    1. 命令

      > ansible -i hosts demo -m shell -a 'chdir=/etc pwd'
    2. 结果

      demo | CHANGED | rc=0 >>
      /etc
    3. 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
    4. 疑问

      1. 如果换目录错了, 会怎么样呢

        1. 结果

          # 会有报错, 后面代码不会执行
          # 建议使用这种方式来切换目录
          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

        }

        ```

      2. 又有一个新问题

        1. 如果可以这样

          1. ansible 发现目录不存在, 就不执行了

            1. 这个真的有
  6. 命令6: removes

    1. 命令

      # 如果没有 /etcd, 就不执行命令了
      > ansible -i hosts demo -m shell -a 'removes=/etcd pwd'
    2. 结果

      demo | SUCCESS | rc=0 >>
      skipped, since /etcd does not exist
    3. 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
    4. 疑问

      1. 如果同时使用 removes 和 chdir 两个参数会如何呢

        1. 目录存在

          > ansible -i hosts demo -m shell -a 'removes=/etcd chdir=/etc pwd'
          demo | SUCCESS | rc=0 >>
          skipped, since /etcd does not exist
        2. 目录不存在

          # 不存在还是会有问题啊...
          > 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
          }
      2. 有 目录 存在不执行 的参数吗

        1. 有的
  7. 命令7: creates

    1. 命令

      > ansible -i hosts demo -m shell -a 'creates=/etcd pwd'
    2. 结果

      demo | CHANGED | rc=0 >>
      /root
    3. 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
    4. 疑问

      1. 如果放多个参数, 会是怎么样的关系

        1. 简单试了试, 貌似是 与 的关系
        2. 想做 或 操作, 但是我没有找到
  8. 其他

    1. shell 还有其他的属性, 这里就不在多说了
    2. command 模块其实更加适合执行命令, 有更多的有点
    3. 如果你使用 shell 命令, 做 rpm 操作时
      1. ansible 会提示使用 yum, dnf 或者 zypper
      2. 其他的模块, 也了解下吧

ps

  1. ref

    1. shell – Execute shell commands on targets

Ansible - 模块 - shell的更多相关文章

  1. 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 ...

  2. ansible模块command、shell、raw、script

    简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...

  3. ansible使用shell模块在受控机上执行命令(ansible2.9.5)

    一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...

  4. ansible模块

    ansible模块: 模块(Modules),类似于 "任务插件"("task plugins")或"库插件"("library ...

  5. 第4天:Ansible模块

    Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下: 1)将模块拷贝到远程服务器 2)执行模块定义的操做,完成对服务器的修改 3)在远程服务器中删除模块 需要说明的是,Ansib ...

  6. ansible笔记(3):ansible模块的基本使用

    ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...

  7. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  8. ansible模块之command、shell、script、file、copy、fetch

    前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...

  9. ansible基本模块-shell

    ansible  XXX  -m shell -a   "XXX"

随机推荐

  1. [P1361] 小M的作物 - 最小割

    没想到今天早上的第一题网络流就血了这么多发 从经典的二选一问题上魔改 仍然考虑最小割 #include <bits/stdc++.h> using namespace std; #defi ...

  2. Alan Walker MV 合辑01 by defender

    Alan Walker MV合辑  出来啦! 百度网盘下载地址: 链接:https://pan.baidu.com/s/10WSool70XBe_8tJOae8V-w 提取码:uckq 地址查看 Mi ...

  3. 基本程序单元Activity

    Activity的4种状态,运行状态,暂停状态,停止状态,销毁状态,下面就是生命周期的顺序图 手动创建Activity 1.创建继承自Activity的Activity 2.重写需要的回调方法 3.设 ...

  4. 跨站点请求伪造(CSRF、XSRF)

    相关文章: 1. http://www.cnblogs.com/xiaoqian1993/p/5816085.html  深入解析跨站请求伪造漏洞:原理剖析 2 .http://blog.csdn.n ...

  5. pytest学习5-mark用例分类

    使用Mark标记测试用例 通过使用pytest.mark你可以轻松地在测试用例上设置元数据.例如, 一些常用的内置标记: skip - 始终跳过该测试用例 skipif - 遇到特定情况跳过该测试用例 ...

  6. PP: Data-driven classification of residential energy consumption patterns by means of functional connectivity networks

    Purpose Implement a good user aggregation and classification. or to assess the interrelation pattern ...

  7. MyBatis 动态代理开发

    MyBatis 动态代理开发 §  Mapper.xml文件中的namespace与mapper接口的类路径相同. §  Mapper接口方法名和Mapper.xml中定义的每个statement的i ...

  8. 10行代码实现简易版的Promise

    实现之前,我们先看看Promise的调用 const src = 'https://img-ph-mirror.nosdn.127.net/sLP6rNBbQhy0OXFNYD9XIA==/79910 ...

  9. Android 开发替换Launcher

    做android产品的时候,根据需求会制定各种各样的Launcher,因此,在此记录替换系统Launcher的流程. 1.修改frameworks/base/core/java/android/con ...

  10. 基于pyqt5的图片素材批量处理工具

    功能 分辨率的批量转换,文件夹递归查找 像素偏移量批量调整,文件夹单层查找 画布的大小的批量进行调整,不进行缩放,文件夹单层查找 界面 通过PyUIC生成的代码 # -*- coding: utf-8 ...