Ansible?Ad-hoc命令執(zhí)行模塊實戰(zhàn)教程
Ad-hoc
Ad-hoc簡介
- Ad-hoc是Ansible下臨時執(zhí)行的一條命令,對于復(fù)雜的命令會使用playbook。Ad-hoc的執(zhí)行依賴于模塊,ansible官方提供了大量的模塊。如command,file,copy,shell等
- 幫助查詢
- ansible-doc -l 列出所有模塊
- ansible-doc -s module 查看某個模塊的參數(shù)
- ansible-doc module 查看某個模塊更詳細(xì)的信息
Ad-hoc命令說明
- 命令說明
ansible 主機(jī)或組 -m 模塊名 -a '模塊參數(shù)' ansible參數(shù)- 主機(jī)和組:就是你要在哪些主機(jī)上執(zhí)行這個命令,必須是配置文件里面定義好的
- 模塊名:可以通過ansibel-doc -l查看目前安裝的模塊,然后過濾出你想要的,默認(rèn)不指定時,使用的時command模塊,可以在配置文件里面找到 "module_name = command"改掉這個
- 模塊參數(shù):可以通過ansible-doc 來查看模塊需要使用哪些參數(shù),以及具體用法
- ansible參數(shù):可以通過ansible 的幫助查到,有很多參數(shù)可以使用,比如是否提權(quán),是否輸入密碼等等
Ad-hoc示例
[devops@node1 ansible]$ ansible node1 -m shell -a 'whoami' node1 | CHANGED | rc=0 >> root # 使用ad-hoc創(chuàng)建一個目錄 [devops@node1 ansible]$ ansible node1 -m shell -a 'mkdir /ansibel' [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. node1 | CHANGED | rc=0 >>
這里我們可以看到,使用shell模塊創(chuàng)建一個目錄的時候他有一個警告,大概就是說讓我們考慮使用file模塊去創(chuàng)建目錄,但是這個是警告,并不是報錯,這個目錄也是被創(chuàng)建出來了
[devops@node1 ansible]$ sudo ls /ansibel -d /ansibel
這就是一些簡單的使用案例
命令執(zhí)行模塊
1. command模塊
該模塊通過-a跟上要執(zhí)行的命令可以直接執(zhí)行,不過命令里面不能帶一些符號(|,>,<,&),否則會不成功。
# 正常的 [devops@node1 ansible]$ ansible all -m command -a 'whoami' node1 | CHANGED | rc=0 >> root node2 | CHANGED | rc=0 >> root # 錯誤的 [devops@node1 ansible]$ ansible all -m command -a 'whoami > /opt/who.txt' node2 | FAILED | rc=1 >> whoami: extra operand ‘>' Try 'whoami --help' for more information.non-zero return code node1 | FAILED | rc=1 >> whoami: extra operand ‘>' Try 'whoami --help' for more information.non-zero return code
看到了吧,我想把輸出內(nèi)容重定向到某個文件內(nèi)他是會報錯的,那我們?nèi)绻羞@種需求改怎么做呢?來看下一個模塊
2. shell模塊
shell模塊的用法基本和command一樣,不過他是通過/bin/sh執(zhí)行,所以shell模塊可以執(zhí)行任何命令,報錯剛剛command不能執(zhí)行的重定向操作
# 不帶重定向 [devops@node1 ansible]$ ansible node1 -m shell -a 'whoami' node1 | CHANGED | rc=0 >> root # 帶重定向 [devops@node1 ansible]$ ansible node1 -m shell -a 'whoami > /opt/who.txt' node1 | CHANGED | rc=0 >> # 查看文件內(nèi)容 [devops@node1 ansible]$ cat /opt/who.txt root
這個模塊確實是可以執(zhí)行一些command執(zhí)行不了的操作,shell模塊還有一些選項
- chdir: 在執(zhí)行命令前,先切換到指定的目錄,默認(rèn)工作目錄是用戶的home目錄
- creates:一個文件名,當(dāng)該文件存在,那么命令就不會執(zhí)行
- removes:一個文件名,當(dāng)該文件不存在,那么命令不會執(zhí)行
后面這兩個可能有點難以理解,我們通過實驗來看看
creates實驗:
# 我們剛剛不是在node1上創(chuàng)建了一個/opt/who.txt嗎?node2上是沒有的,那么我們選擇就來指定這個文件 [devops@node1 ansible]$ ansible all -m shell -a 'creates=/opt/who.txt whoami' node1 | SUCCESS | rc=0 >> skipped, since /opt/who.txt exists node2 | CHANGED | rc=0 >> root
看到了吧,node1上有這個文件,那么它是沒有去執(zhí)行的,他直接跳過了,但是node2上沒有這個文件,那么它執(zhí)行了whoami這個命令
removes實驗:
[devops@node1 ansible]$ ansible all -m shell -a 'removes=/opt/who.txt whoami' node1 | CHANGED | rc=0 >> root node2 | SUCCESS | rc=0 >> skipped, since /opt/who.txt does not exist
通過這2個實驗應(yīng)該能理解這倆選項的作用了吧
3. raw模塊
這個模塊的用法和shell是一樣的,不同點在于它沒有chdir,creates和removes選項,其他都是一樣一樣的
4. script模塊
這個模塊就比較有意思了,他是將你主控端的腳本直接在被控端上執(zhí)行,注意,他并不會將這個文件傳過去
[devops@node1 ansible]$ cat test.sh #!/bin/bash ifconfig |grep netmask | awk -F" " '{print $2}'
注意看這個腳本,是在node1上的,也就是主控端,我們來使用過scripts模塊來執(zhí)行一下這個腳本,看看它是不是沒有將文件傳過去
[devops@node1 ansible]$ ansible all -m script -a 'test.sh' node2 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to node2 closed.\r\n", "stderr_lines": [ "Shared connection to node2 closed." ], "stdout": "/home/devops/.ansible/tmp/ansible-tmp-1708832697.440758-6677-176602102106477/test.sh: line 2: ifconfig: command not found\r\n", "stdout_lines": [ "/home/devops/.ansible/tmp/ansible-tmp-1708832697.440758-6677-176602102106477/test.sh: line 2: ifconfig: command not found" ] } node1 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to node1 closed.\r\n", "stderr_lines": [ "Shared connection to node1 closed." ], "stdout": "192.168.100.210\r\n192.168.200.131\r\n127.0.0.1\r\n192.168.122.1\r\n", "stdout_lines": [ "192.168.100.210", "192.168.200.131", "127.0.0.1", "192.168.122.1" ] }
這里可以看到,他執(zhí)行成功了,返回的是主機(jī)上所有的IP地址,然后我們?nèi)ode2上看看這個文件是否不存在,不在node1上看是因為這個文件本來就是node1上寫的,因為node1就是主控節(jié)點,它同時也是被控
[devops@node1 ansible]$ ansible node2 -m shell -a 'find / -name test.sh' node2 | CHANGED | rc=0 >>
我們可以看到,使用find去查到這個文件名他沒有任何的輸出,那么就是沒有找到,也就是沒有這個文件
到此這篇關(guān)于Ansible Ad-hoc命令執(zhí)行模塊的文章就介紹到這了,更多相關(guān)Ansible Ad-hoc模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux命令學(xué)習(xí)之用戶切換su,sudo命令詳解
在操作過程中需要使用特定的用戶進(jìn)行特定的操作,多數(shù)情況下是因為權(quán)限,比如要修改一個文件,只有root用戶有權(quán)限修改,那么就要切換到root用戶下進(jìn)行操作,本文給大家講解Linux命令學(xué)習(xí)之用戶切換su,sudo命令,感興趣的朋友跟隨小編一起看看吧2023-02-02linux shell循環(huán):for、while、until用法詳解
這篇文章主要介紹了linux shell下常用的循環(huán)for、while、until的用法,這也是腳本之家小編看到的比較詳細(xì)的文章了,感興趣的朋友可以參考一下,最好是在環(huán)境下自己手工打一份,不要復(fù)制2019-04-04Shell腳本實現(xiàn)的memcached進(jìn)程監(jiān)控
這篇文章主要介紹了Shell腳本實現(xiàn)的memcached進(jìn)程監(jiān)控,實現(xiàn)功能為監(jiān)控memcached進(jìn)程是否存在,不存在則啟動memcached并重載nginx,需要的朋友可以參考下2014-07-07