Ansible - 模塊 - shell


  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM