Ansible?Ad-hoc命令執(zhí)行模塊實(shí)戰(zhàn)教程
Ad-hoc
Ad-hoc簡(jiǎn)介
- Ad-hoc是Ansible下臨時(shí)執(zhí)行的一條命令,對(duì)于復(fù)雜的命令會(huì)使用playbook。Ad-hoc的執(zhí)行依賴于模塊,ansible官方提供了大量的模塊。如command,file,copy,shell等
- 幫助查詢
- ansible-doc -l 列出所有模塊
- ansible-doc -s module 查看某個(gè)模塊的參數(shù)
- ansible-doc module 查看某個(gè)模塊更詳細(xì)的信息
Ad-hoc命令說(shuō)明
- 命令說(shuō)明
ansible 主機(jī)或組 -m 模塊名 -a '模塊參數(shù)' ansible參數(shù)- 主機(jī)和組:就是你要在哪些主機(jī)上執(zhí)行這個(gè)命令,必須是配置文件里面定義好的
- 模塊名:可以通過(guò)ansibel-doc -l查看目前安裝的模塊,然后過(guò)濾出你想要的,默認(rèn)不指定時(shí),使用的時(shí)command模塊,可以在配置文件里面找到 "module_name = command"改掉這個(gè)
- 模塊參數(shù):可以通過(guò)ansible-doc 來(lái)查看模塊需要使用哪些參數(shù),以及具體用法
- ansible參數(shù):可以通過(guò)ansible 的幫助查到,有很多參數(shù)可以使用,比如是否提權(quán),是否輸入密碼等等
Ad-hoc示例
[devops@node1 ansible]$ ansible node1 -m shell -a 'whoami' node1 | CHANGED | rc=0 >> root # 使用ad-hoc創(chuàng)建一個(gè)目錄 [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)建一個(gè)目錄的時(shí)候他有一個(gè)警告,大概就是說(shuō)讓我們考慮使用file模塊去創(chuàng)建目錄,但是這個(gè)是警告,并不是報(bào)錯(cuò),這個(gè)目錄也是被創(chuàng)建出來(lái)了
[devops@node1 ansible]$ sudo ls /ansibel -d /ansibel
這就是一些簡(jiǎn)單的使用案例
命令執(zhí)行模塊
1. command模塊
該模塊通過(guò)-a跟上要執(zhí)行的命令可以直接執(zhí)行,不過(guò)命令里面不能帶一些符號(hào)(|,>,<,&),否則會(huì)不成功。
# 正常的 [devops@node1 ansible]$ ansible all -m command -a 'whoami' node1 | CHANGED | rc=0 >> root node2 | CHANGED | rc=0 >> root # 錯(cuò)誤的 [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)容重定向到某個(gè)文件內(nèi)他是會(huì)報(bào)錯(cuò)的,那我們?nèi)绻羞@種需求改怎么做呢?來(lái)看下一個(gè)模塊
2. shell模塊
shell模塊的用法基本和command一樣,不過(guò)他是通過(guò)/bin/sh執(zhí)行,所以shell模塊可以執(zhí)行任何命令,報(bào)錯(cuò)剛剛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
這個(gè)模塊確實(shí)是可以執(zhí)行一些command執(zhí)行不了的操作,shell模塊還有一些選項(xiàng)
- chdir: 在執(zhí)行命令前,先切換到指定的目錄,默認(rèn)工作目錄是用戶的home目錄
- creates:一個(gè)文件名,當(dāng)該文件存在,那么命令就不會(huì)執(zhí)行
- removes:一個(gè)文件名,當(dāng)該文件不存在,那么命令不會(huì)執(zhí)行
后面這兩個(gè)可能有點(diǎn)難以理解,我們通過(guò)實(shí)驗(yàn)來(lái)看看
creates實(shí)驗(yàn):
# 我們剛剛不是在node1上創(chuàng)建了一個(gè)/opt/who.txt嗎?node2上是沒(méi)有的,那么我們選擇就來(lái)指定這個(gè)文件 [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上有這個(gè)文件,那么它是沒(méi)有去執(zhí)行的,他直接跳過(guò)了,但是node2上沒(méi)有這個(gè)文件,那么它執(zhí)行了whoami這個(gè)命令
removes實(shí)驗(yàn):
[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
通過(guò)這2個(gè)實(shí)驗(yàn)應(yīng)該能理解這倆選項(xiàng)的作用了吧
3. raw模塊
這個(gè)模塊的用法和shell是一樣的,不同點(diǎn)在于它沒(méi)有chdir,creates和removes選項(xiàng),其他都是一樣一樣的
4. script模塊
這個(gè)模塊就比較有意思了,他是將你主控端的腳本直接在被控端上執(zhí)行,注意,他并不會(huì)將這個(gè)文件傳過(guò)去
[devops@node1 ansible]$ cat test.sh
#!/bin/bash
ifconfig |grep netmask | awk -F" " '{print $2}'注意看這個(gè)腳本,是在node1上的,也就是主控端,我們來(lái)使用過(guò)scripts模塊來(lái)執(zhí)行一下這個(gè)腳本,看看它是不是沒(méi)有將文件傳過(guò)去
[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上看看這個(gè)文件是否不存在,不在node1上看是因?yàn)檫@個(gè)文件本來(lái)就是node1上寫(xiě)的,因?yàn)閚ode1就是主控節(jié)點(diǎn),它同時(shí)也是被控
[devops@node1 ansible]$ ansible node2 -m shell -a 'find / -name test.sh' node2 | CHANGED | rc=0 >>
我們可以看到,使用find去查到這個(gè)文件名他沒(méi)有任何的輸出,那么就是沒(méi)有找到,也就是沒(méi)有這個(gè)文件
到此這篇關(guān)于Ansible Ad-hoc命令執(zhí)行模塊的文章就介紹到這了,更多相關(guān)Ansible Ad-hoc模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Django和Ansible實(shí)現(xiàn)自動(dòng)化部署方式
- 利用Ansible實(shí)現(xiàn)批量服務(wù)器自動(dòng)化管理詳解
- 如何使用ansible批量初始化服務(wù)器
- Ansible?Galaxy命令的使用實(shí)踐示例詳解
- ansible管理工具的環(huán)境及部署安裝
- Ansible部署K8s集群的方法
- ansible執(zhí)行shell腳本的方法
- Linux系統(tǒng)實(shí)現(xiàn)ansible自動(dòng)化安裝配置httpd的方法
- 多服務(wù)器運(yùn)維:用Ansible一鍵自動(dòng)化部署和管理Nginx
相關(guān)文章
mac使用Shell(終端)SSH連接遠(yuǎn)程服務(wù)器的方法
這篇文章主要介紹了mac使用Shell(終端)SSH連接遠(yuǎn)程服務(wù)器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
如何解決 shell 腳本重復(fù)執(zhí)行的問(wèn)題
假如執(zhí)行備份腳本消耗的時(shí)間遠(yuǎn)大于設(shè)置的備份間隔的話,系統(tǒng)會(huì)出現(xiàn)多個(gè)同時(shí)在執(zhí)行腳本的Bash實(shí)例,會(huì)占用大量的系統(tǒng)資源,進(jìn)而影響正常業(yè)務(wù)程序的運(yùn)行,那如何解決上述shell腳本重復(fù)執(zhí)行的問(wèn)題呢,本文將要介紹的 flock 命令可以解決這個(gè)問(wèn)題2021-05-05
shell腳本實(shí)現(xiàn)隨機(jī)生成10個(gè)8位密碼
這篇文章主要介紹了shell腳本實(shí)現(xiàn)隨機(jī)生成10個(gè)8位密碼的方法,這里推薦給大家,有需要的小伙伴的可以參考下。2015-03-03
linux?sed/awk命令檢索區(qū)間日志的問(wèn)題
這篇文章給大家介紹linux sed/awk命令檢索區(qū)間日志的問(wèn)題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-09-09
ssh(ssh-keygen)配置免輸入密碼登錄遠(yuǎn)程主機(jī)的方法
這篇文章主要是介紹ssh(ssh-keygen)配置免輸入密碼登錄遠(yuǎn)程主機(jī)的方法,供大家學(xué)習(xí)參考2013-02-02
shell腳本快速創(chuàng)建格式化磁盤(pán)與詳細(xì)操作步驟
這篇文章主要介紹了shell腳本快速創(chuàng)建格式化磁盤(pán)與詳細(xì)操作步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

