-
概述
- 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
- 其他的模塊, 也了解下吧