介紹ansible的Ad-hoc與commands模組
Sep 02, 2024 pm 02:16 PMAd-Hoc 是指ansible下臨時執(zhí)行的一條命令,并且不需要保存的命令,對于復雜的命令后面會說playbook。講到Ad-hoc 就要提到模塊,所有的命令執(zhí)行都要依賴于事先寫好的模塊,默認安裝好的ansible 里面已經(jīng)自帶了很多模塊,如:command、raw、shell、file、cron等,具體可以通過ansible-doc -l 進行查看 。
這里還是先來一個上幾篇幅經(jīng)常用到的一個例子:
[root@361way ~]# ansible 10.212.52.252 -a 'uptime' -k SSH password: 10.212.52.252 | success | rc=0 >> 10:10am up 27 days 19:33, 2 users, load average: 0.39, 0.34, 0.33
一個ad-hoc命令的執(zhí)行,需要按以下格式進行執(zhí)行:
- ansible 主機或組 -m 模塊名 -a '模塊參數(shù)' ansible參數(shù)
- 主機和組,是在/etc/ansible/hosts 里進行指定的部分,當然動態(tài)Inventory 使用的是腳本從外部應用里獲取的主機,這部分具體可以參考ansible小結(jié)(五)Dynamic Inventory ;
- 模塊名,可以通過ansible-doc -l 查看目前安裝的模塊,默認不指定時,使用的是command模塊,具體可以查看/etc/ansible/ansible.cfg 的“#module_name = command ” 部分,默認模塊可以在該配置文件中進行修改;
- 模塊參數(shù),可以通過 “ansible-doc 模塊名” 查看具體的用法及后面的參數(shù);
- ansible參數(shù),可以通過ansible命令的幫忙信息里查看到,這里有很多參數(shù)可以供選擇,如是否需要輸入密碼、是否sudo等。
當命令執(zhí)行時間比較長時,也可以放到后臺執(zhí)行,這里會用到-B、-P參數(shù),如下:
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff" \\后臺執(zhí)行命令 3600s,-B 表示后臺執(zhí)行的時間 ansible all -m async_status -a "jid=123456789" \\檢查任務的狀態(tài) ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" \\后臺執(zhí)行命令最大時間是 1800s 即 30 分鐘,-P 每 60s 檢查下狀態(tài)默認 15s
示例如下:
[root@361way ~]# ansible 10.212.52.252 -B 3600 -P 0 -a 'watch ls' background launch... 10.212.52.252 | success >> { "ansible_job_id": "411650646689.13501", "results_file": "/root/.ansible_async/411650646689.13501", "started": 1 } [root@361way ~]# ansible 10.212.52.252 -m async_status -a 'jid=411650646689.13501' 10.212.52.252 | success >> { "ansible_job_id": "411650646689.13501", "changed": false, "finished": 0, "results_file": "/root/.ansible_async/411650646689.13501", "started": 1 }
不指定-P或-P參數(shù)為非0時,該任務就會按-P直接的參數(shù)一直刷新下去,直到超出-B參數(shù)指定的時間或命令執(zhí)行完成:
[root@361way ~]# ansible 10.212.52.252 -B 3600 -a 'watch ls' background launch... 10.212.52.252 | success >> { "ansible_job_id": "397200656414.15008", "results_file": "/root/.ansible_async/397200656414.15008", "started": 1 } 10.212.52.252 | success >> { "ansible_job_id": "397200656414.15008", "changed": false, "finished": 0, "results_file": "/root/.ansible_async/397200656414.15008", "started": 1 } polling on 10.212.52.252, 3585s remaining …………………………………………略
上面已經(jīng)提到,ansbile自身已經(jīng)自帶了很多模塊,可以通過ansible-doc -l 進行查看。這里就結(jié)合command、shell、raw、script模塊了解下其用法。
上面四個模塊都屬于commands 類。
- command模塊,該模塊通過-a跟上要執(zhí)行的命令可以直接執(zhí)行,不過命令里如果有帶有如下字符部分則執(zhí)行不成功 “ so variables like $HOME and operations like "", "|", and "&" will not work (use the shell module if you need these features).”;
- shell 模塊,用法其本和command一樣,不過的是其是通過/bin/sh進行執(zhí)行,所以shell 模塊可以執(zhí)行任何命令,就像在本機執(zhí)行一樣,“ It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.”;
- raw模塊,用法和shell 模塊一樣 ,其也可以執(zhí)行任意命令,就像在本機執(zhí)行一樣,“Executes a low-down and dirty SSH command, not going through the module subsystem. There is no change handler support for this module. This module does not require python on the remote system”
- script模塊,其是將管理端的shell 在被管理主機上執(zhí)行,其原理是先將shell 復制到遠程主機,再在遠程主機上執(zhí)行,原理類似于raw模塊,“This module does not require python on the remote system, much like the raw module.” 。
注:raw模塊和comand、shell 模塊不同的是其沒有chdir、creates、removes參數(shù),chdir參數(shù)的作用就是先切到chdir指定的目錄后,再執(zhí)行后面的命令,這在后面很多模塊里都會有該參數(shù)?。
command模塊包含如下選項:
- creates:一個文件名,當該文件存在,則該命令不執(zhí)行
- free_form:要執(zhí)行的linux指令
- chdir:在執(zhí)行指令之前,先切換到該指定的目錄
- removes:一個文件名,當該文件不存在,則該選項不執(zhí)行
- executable:切換shell來執(zhí)行指令,該執(zhí)行路徑必須是一個絕對路徑
command模塊、raw模塊、shell模塊示例:
[root@361way ~]# ansible 10.212.52.252 -m command -a 'ps auxf|grep snmp' 10.212.52.252 | FAILED | rc=1 >> ERROR: Unsupported option (BSD syntax) ********* simple selection ********* ********* selection by list ********* -A all processes -C by command name -N negate selection -G by real group ID (supports names) -a all w/ tty except session leaders -U by real user ID (supports names) -d all except session leaders -g by session OR by effective group name -e all processes -p by process ID T all processes on this terminal -s processes in the sessions given a all w/ tty, including other users -t by tty g OBSOLETE -- DO NOT USE -u by effective user ID (supports names) r only running processes U processes for specified users x processes w/o controlling ttys t by tty *********** output format ********** *********** long options *********** -o,o user-defined -f full --Group --User --pid --cols --ppid -j,j job control s signal --group --user --sid --rows --info -O,O preloaded -o v virtual memory --cumulative --format --deselect -l,l long u user-oriented --sort --tty --forest --version -F extra full X registers --heading --no-heading --context ********* misc options ********* -V,V show version L list format codes f ASCII art forest -m,m,-L,-T,H threads S children in sum -y change -l format -M,Z security data c true command name -c scheduling class -w,w wide output n numeric WCHAN,UID -H process hierarchy [root@361way ~]# ansible 10.212.52.252 -m raw -a 'ps auxf|grep snmp' 10.212.52.252 | success | rc=0 >> root 5580 25.0 0.0 12876 1792 pts/2 Ss+ 12:36 0:00 \_ bash -c ps auxf|grep snmp root 5607 0.0 0.0 5720 832 pts/2 S+ 12:36 0:00 \_ grep snmp root 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/snmpd.pid [root@361way ~]# ansible 10.212.52.252 -m shell -a 'ps auxf|grep snmp' 10.212.52.252 | success | rc=0 >> root 5803 0.0 0.0 11308 1308 pts/2 S+ 12:36 0:00 \_ /bin/sh -c ps auxf|grep snmp root 5805 0.0 0.0 4260 572 pts/2 S+ 12:36 0:00 \_ grep snmp root 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/snmpd.pid
上面的執(zhí)行結(jié)果可以看到,我這里加了管道,command模塊執(zhí)行時出錯,而使用raw模塊和shell 模塊都正常。
使用chdir的示例:
[root@361way ~]# ansible 10.212.52.252 -m command -a 'chdir=/tmp/361way touch test.file' 10.212.52.252 | success | rc=0 >> [root@361way ~]# ansible 10.212.52.252 -m shell -a 'chdir=/tmp/361way touch test2.file' 10.212.52.252 | success | rc=0 >> [root@361way ~]# ansible 10.212.52.252 -m raw -a 'chdir=/tmp/361way touch test3.file' 10.212.52.252 | success | rc=0 >>
從上面執(zhí)行結(jié)果來看,三個命令都執(zhí)行成功了。不過通過在遠程主機上查看,前兩個文件被成功創(chuàng)建:
linux-wdh1:/tmp/361way # ls /tmp/361way test.file test2.file
使用raw模塊的執(zhí)行的結(jié)果文件也被正常創(chuàng)建了,不過不是在chdir 指定的目錄,而是在當前執(zhí)行用戶的家目錄。
linux-wdh1:~ # ls ~/test3.file /root/test3.file
creates與removes示例:
這里我在測試主機上創(chuàng)建/tmp/361way/server.txt文件,執(zhí)行結(jié)果如下:
[root@361way ~]# ansible 10.212.52.252 -a 'creates=/tmp/361way/server.txt uptime' 10.212.52.252 | success | rc=0 >> skipped, since /tmp/361way/server.txt exists [root@361way ~]# ansible 10.212.52.252 -a 'removes=/tmp/361way/server.txt uptime' 10.212.52.252 | success | rc=0 >> 15:11pm up 28 days 0:34, 2 users, load average: 0.75, 0.46, 0.39
script模塊示例:
[root@361way ~]# cat script.sh<br>
#!/bin/bash<br>
df -hl<br>
ifconfig<br>
ps auxf|grep snmp<br>
[root@361way ~]# ansible 10.212.52.252 -m script -a 'scrip.sh'<br>
10.212.52.252 | FAILED => file or module does not exist: /root/scrip.sh<br>
[root@361way ~]# ansible 10.212.52.252 -m script -a 'script.sh'<br>
10.212.52.252 | success >> {<br>
"changed": true,<br>
"rc": 0,<br>
"stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\nControl socket connect(/root/.ansible/cp/ansible-ssh-10.212.52.252-22-root): Connection refused\r\ndebug1: Connecting to 10.212.52.252 [10.212.52.252] port 22.\r\ndebug1: fd 3 clearing O_NONBLOCK\r\ndebug1: Connection established.\r\ndebug1: permanently_set_uid: 0/0\r\ndebug1: identity file /root/.ssh/identity type -1\r\ndebug1: identity file /root/.ssh/identity-cert type -1\r\ndebug1: identity file /root/.ssh/id_rsa type -1\r\ndebug1: identity file /root/.ssh/id_rsa-cert type -1\r\ndebug1: identity file /root/.ssh/id_dsa type -1\r\ndebug1: identity file /root/.ssh/id_dsa-cert type -1\r\ndebug1: identity file /root/.ssh/id_ecdsa type -1\r\ndebug1: identity file /root/.ssh/id_ecdsa-cert type -1\r\ndebug1: Remote protocol version 2.0, remote software version OpenSSH_6.2\r\ndebug1: match: OpenSSH_6.2 pat OpenSSH*\r\ndebug1: Enabling compatibility mode for protocol 2.0\r\ndebug1: Local version string SSH-2.0-OpenSSH_5.3\r\ndebug1: SSH2_MSG_KEXINIT sent\r\ndebug1: SSH2_MSG_KEXINIT received\r\ndebug1: kex: server->client aes128-ctr hmac-md5 zlib@openssh.com\r\ndebug1: kex: client->server aes128-ctr hmac-md5 zlib@openssh.com\r\ndebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024
"stdout": "Filesystem Size Used Avail Use% Mounted on\r\n/dev/sda2 9.9G 872M 8.5G 10% /\r\nudev 3.9G 128K 3.9G 1% /dev\r\ntmpfs 3.9G 76K 3.9G 1% /dev/shm\r\n/dev/sda3 5.0G 219M 4.5G 5% /boot\r\n/dev/sda8 40G 15G 23G 40% /home\r\n/dev/sda9 9.9G 5.2G 4.3G 55% /opt\r\n/dev/sda6 5.0G 2.7G 2.1G 57% /tmp\r\n/dev/sda5 9.9G 3.4G 6.0G 36% /usr\r\n/dev/sda7 9.9G 823M 8.6G 9% /var\r\neth0 Link encap:Ethernet HWaddr 00:50:56:A8:65:7E \r\n inet addr:10.212.52.252 Bcast:10.212.52.255 Mask:255.255.255.0\r\n inet6 addr: fe80::250:56ff:fea8:657e/64 Scope:Link\r\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\r\n RX packets:24112135 errors:0 dropped:792372 overruns:0 frame:0\r\n TX packets:10697339 errors:0 dropped:0 overruns:0 carrier:0\r\n collisions:0 txqueuelen:1000 \r\n RX bytes:17137233328 (16343.3 Mb) TX bytes:13390377826 (12770.0 Mb)\r\n\r\nlo Link encap:Local Loopback \r\n inet addr:127.0.0.1 Mask:255.0.0.0\r\n inet6 addr: ::1/128 Scope:Host\r\n UP LOOPBACK RUNNING MTU:16436 Metric:1\r\n RX packets:3407332 errors:0 dropped:0 overruns:0 frame:0\r\n TX packets:3407332 errors:0 dropped:0 overruns:0 carrier:0\r\n collisions:0 txqueuelen:0 \r\n RX bytes:262675450 (250.5 Mb) TX bytes:262675450 (250.5 Mb)\r\n\r\nroot 25332 0.0 0.0 4260 568 pts/2 S+ 12:54 0:00 \\_ grep snmp\r\nroot 24364 0.0 0.0 70416 6696 ? SNl May15 0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/snmpd.pid\r\n"<br>
}<br>
輸出結(jié)果很多,看起來也很亂,不過查下stdout部分,這個部分是實際上執(zhí)行后的結(jié)果。這里可以配合管道一起使用,可以如下使用:
[root@361way ~]# ansible 10.212.52.252 -m script -a 'script.sh' |egrep '>>|stdout'
篇幅所限,本來想把常用模塊都放在該篇來寫,感覺太冗長,后面再單獨分開相應的篇幅做模塊的介紹。
以上是介紹ansible的Ad-hoc與commands模組的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在CentOS上集成Postman應用可以通過多種方法來實現(xiàn),以下是詳細的步驟和建議:通過下載安裝包安裝Postman下載Postman的Linux版本安裝包:訪問Postman官方網(wǎng)站,選擇適用於Linux的版本進行下載。解壓安裝包:使用以下命令將安裝包解壓到指定目錄,例如/opt:sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt請注意將“postman-linux-x64-xx.xx.xx.tar.gz”替換為您實際下載的文件名。創(chuàng)建符號

【常見目錄說明】目錄/bin存放二進制可執(zhí)行文件(ls,cat,mkdir等),常用命令一般都在這裡。 /etc存放系統(tǒng)管理和配置文件/home存放所有用戶文件的根目錄,是用戶主目錄的基點,比如用戶user的主目錄就是/home/user,可以用~user表示/usr用於存放系統(tǒng)應用程序,比較重要的目錄/usr/local?本地系統(tǒng)管理員軟件安裝目錄(安裝系統(tǒng)級的應用)。這是最龐大的目錄,要用到的應用程序和文件幾乎都在這個目錄。 /usr/x11r6?存放x?window的目錄/usr/bin?眾多

在PyCharm中設(shè)置解釋器的位置可以通過以下步驟實現(xiàn):1.打開PyCharm,點擊“File”菜單,選擇“Settings”或“Preferences”。 2.找到並點擊“Project:[你的項目名]”,然後選擇“PythonInterpreter”。 3.點擊“AddInterpreter”,選擇“SystemInterpreter”,瀏覽到Python安裝目錄,選中Python可執(zhí)行文件,點擊“OK”。設(shè)置解釋器時需注意路徑正確性、版本兼容性和虛擬環(huán)境的使用,以確保項目順利運行。

Java與其他編程語言的主要區(qū)別在於其“一次編寫,到處運行”的跨平臺特性。 1.Java的語法接近C ,但去掉了容易出錯的指針操作,適合大型企業(yè)應用。 2.與Python相比,Java在性能和大規(guī)模數(shù)據(jù)處理上更具優(yōu)勢。 Java的跨平臺優(yōu)勢源於Java虛擬機(JVM),它能在不同平臺上運行相同的字節(jié)碼,簡化開發(fā)和部署,但需注意避免使用平臺特定API以保持跨平臺性。

了解Nginx的配置文件路徑和初始設(shè)置非常重要,因為它是優(yōu)化和管理Web服務器的第一步。 1)配置文件路徑通常是/etc/nginx/nginx.conf,使用nginx-t命令可以查找並測試語法。 2)初始設(shè)置包括全局設(shè)置(如user、worker_processes)和HTTP設(shè)置(如include、log_format),這些設(shè)置允許根據(jù)需求進行定制和擴展,錯誤配置可能導致性能問題和安全漏洞。

MySQL的安裝和配置可以通過以下步驟完成:1.從官方網(wǎng)站下載適合操作系統(tǒng)的安裝包。 2.運行安裝程序,選擇“開發(fā)者默認”選項並設(shè)置root用戶密碼。 3.安裝後配置環(huán)境變量,確保MySQL的bin目錄在PATH中。 4.創(chuàng)建用戶時遵循最小權(quán)限原則並設(shè)置強密碼。 5.優(yōu)化性能時調(diào)整innodb_buffer_pool_size和max_connections參數(shù)。 6.定期備份數(shù)據(jù)庫並優(yōu)化查詢語句以提高性能。

Informix和MySQL均為廣受青睞的關(guān)係型數(shù)據(jù)庫管理系統(tǒng),它們在Linux環(huán)境下均表現(xiàn)優(yōu)異並得到廣泛應用。以下是對兩者在Linux平臺上的對比分析:安裝與配置Informix:在Linux上部署Informix需要下載對應的安裝文件,隨後依據(jù)官方文檔指引完成安裝及配置流程。 MySQL:MySQL的安裝過程較為簡便,可通過系統(tǒng)的包管理工具(例如apt或yum)輕鬆實現(xiàn)安裝,並且網(wǎng)絡(luò)上有大量的教程和社區(qū)支持可供參考。性能表現(xiàn)Informix:Informix以卓越的性能和

參加VSCode線下技術(shù)交流活動的經(jīng)驗非常豐富,主要收穫包括插件開發(fā)的分享、實戰(zhàn)演示和與其他開發(fā)者的交流。 1.插件開發(fā)的分享:了解瞭如何利用VSCode的插件API提升開發(fā)效率,如自動格式化和靜態(tài)分析插件。 2.實戰(zhàn)演示:學習瞭如何使用VSCode進行遠程開發(fā),認識到其靈活性和擴展性。 3.與開發(fā)者交流:獲取了優(yōu)化VSCode啟動速度的技巧,如減少啟動時加載的插件數(shù)量和管理插件加載順序??傊?,這次活動讓我受益匪淺,強烈推薦對VSCode感興趣的人參加。
