Official documentation: https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html
簡介
Ad-hoc命令是一種臨時(shí)輸入并執(zhí)行的命令,通常用于測試和調(diào)試。它們不需要永久保存,簡單來說,ad-hoc就是“即時(shí)命令”。
常用模塊
1、command 模塊(默認(rèn)模塊)
默認(rèn)模塊,沒有shell強(qiáng)大,基本上shell模塊都可以支持command模塊的功能。
【1】幫助
ansible-doc command # 推薦使用下面這個(gè) ansible-doc command -s
【2】參數(shù)解釋
- free_form——必須參數(shù),指定需要遠(yuǎn)程執(zhí)行的命令。需要說明一點(diǎn),free_form 參數(shù)與其他參數(shù)(如果想要使用一個(gè)參數(shù),那么則需要為這個(gè)參數(shù)賦值,也就是name=value模式)并不相同。比如,當(dāng)我們想要在遠(yuǎn)程主機(jī)上執(zhí)行 ls 命令時(shí),我們并不需要寫成”free_form=ls” ,這樣寫反而是錯(cuò)誤的,因?yàn)椴]有任何參數(shù)的名字是 free_form,當(dāng)我們想要在遠(yuǎn)程主機(jī)中執(zhí)行 ls 命令時(shí),直接寫成 ls 即可。因?yàn)?command 模塊的作用是執(zhí)行命令,所以,任何一個(gè)可以在遠(yuǎn)程主機(jī)上執(zhí)行的命令都可以被稱為 free_form。
- chdir——此參數(shù)的作用就是指定一個(gè)目錄,在執(zhí)行對應(yīng)的命令之前,會先進(jìn)入到 chdir 參數(shù)指定的目錄中。
- creates——看到 creates,你可能會從字面上理解這個(gè)參數(shù),但是使用這個(gè)參數(shù)并不會幫助我們創(chuàng)建文件,它的作用是當(dāng)指定的文件存在時(shí),就不執(zhí)行對應(yīng)命令,比如,如果 /testdir/test文件存在,就不執(zhí)行我們指定的命令。
- removes——與 creates 參數(shù)的作用正好相反,它的作用是當(dāng)指定的文件不存在時(shí),就不執(zhí)行對應(yīng)命令,比如,如果 /testdir/tests 文件不存在,就不執(zhí)行我們指定的命令,此參數(shù)并不會幫助我們刪除文件。
【3】示例演示
# 上面命令表示在 web 主機(jī)上執(zhí)行 ls 命令,因?yàn)槭褂玫氖?root 用戶,所以默認(rèn)情況下,ls 出的結(jié)果是 web 主機(jī)中 root 用戶家目錄中的文件列表。 ansible web -m command -a "ls" # chdir 參數(shù)表示執(zhí)行命令之前,會先進(jìn)入到指定的目錄中,所以上面命令表示查看 web 主機(jī)上 /testdir 目錄中的文件列表,返回顯示有2個(gè)文件。 ansible web -m command -a "chdir=/testdir ls" # 下面命令表示 /testdir/testfile1 文件存在于遠(yuǎn)程主機(jī)中,則不執(zhí)行對應(yīng)命令。/testdir/testfile3 不存在,才執(zhí)行”echo test”命令。 ansible web -m command -a "creates=/testdir/testfile1 echo test" # 下面命令表示 /testdir/testfile3 文件不存在于遠(yuǎn)程主機(jī)中,則不執(zhí)行對應(yīng)命令。/testdir/testfile1 存在,才執(zhí)行”echo test”命令。 ansible web -m command -a "removes=/testdir/testfile1 echo test"
2、shell 模塊
shell模塊 [執(zhí)行遠(yuǎn)程主機(jī)的shell/python等腳本]。
【1】查看幫助
ansible-doc shell -s
【2】示例演示
# -o:一行顯示 # 安裝httpd ansible web -m shell -a 'yum -y install httpd' -o # 查看時(shí)間 ansible web -m shell -a 'uptime' -o
3、script 模塊
script模塊 [在遠(yuǎn)程主機(jī)執(zhí)行主控端的shell/python等腳本 ]。
【1】查看幫助
ansible-doc script -s
【2】參數(shù)解釋
- free_form——必須參數(shù),指定需要執(zhí)行的腳本,腳本位于 ansible 管理主機(jī)本地,并沒有具體的一個(gè)參數(shù)名叫 free_form,具體解釋請參考 command 模塊。
- chdir——此參數(shù)的作用就是指定一個(gè)遠(yuǎn)程主機(jī)中的目錄,在執(zhí)行對應(yīng)的腳本之前,會先進(jìn)入到 chdir 參數(shù)指定的目錄中。
- creates——使用此參數(shù)指定一個(gè)遠(yuǎn)程主機(jī)中的文件,當(dāng)指定的文件存在時(shí),就不執(zhí)行對應(yīng)腳本,可參考 command 模塊中的解釋。
- removes——使用此參數(shù)指定一個(gè)遠(yuǎn)程主機(jī)中的文件,當(dāng)指定的文件不存在時(shí),就不執(zhí)行對應(yīng)腳本,可參考 command 模塊中的解釋。
【3】示例演示
# 下面命令表示 ansible 主機(jī)中的 /testdir/testscript.sh 腳本將在 web 主機(jī)中執(zhí)行,執(zhí)行此腳本之前,會先進(jìn)入到 web 主機(jī)中的 /opt 目錄 ansible web -m script -a "chdir=/opt /testdir/testscript.sh" # 下面命令表示,web主機(jī)中的 /testdir/testfile1文件已經(jīng)存在,ansible 主機(jī)中的 /testdir/testscript.sh 腳本將不會在 web 主機(jī)中執(zhí)行。 ansible web -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh" # 下面命令表示,web 主機(jī)中的 /testdir/testfile1 文件存在,ansible 主機(jī)中的 /testdir/testscript.sh 腳本則會在 web 主機(jī)中執(zhí)行。 ansible ansible-demo3 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"
4、raw 模塊
raw模塊 [類似于command模塊、支持管道傳遞]。
【1】查看幫助
ansible-doc raw -s
【2】示例演示
ansible web -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print $2}' |awk -F: '{print $2}'"
5、copy 模塊
copy 模塊 從主控端復(fù)制文件到被控端。
【1】查看幫助
ansible-doc copy -s
【2】示例演示
# -a,--args:后面接參數(shù) ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777' # backup=yes/no:文件存在且文件內(nèi)容不一樣是否備份,默認(rèn)不備份 ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777 backup=yes'
6、fetch 模塊
copy 模塊從被控端復(fù)制文件到主控端,正好跟copy相反。
【1】查看幫助
ansible-doc fetch -s
【2】示例演示
# 跟copy支持的參數(shù)差不多,src:遠(yuǎn)端主機(jī)的目錄,dest:主控端目錄,其實(shí)真正存放的目錄在:/tmp/192.168.182.129/tmp/up.sh,會按每臺主機(jī)分組存放 #This `must' be a file, not a directory:只支持單個(gè)文件獲取 ansible 192.168.182.129 -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"
7、unarchive 模塊(解包模塊)
unarchive 模塊是解包模塊。
【1】查看幫助
ansible-doc unarchive -s
【2】參數(shù)解釋
- copy——默認(rèn)為yes,當(dāng)copy=yes,那么拷貝的文件是從ansible主機(jī)復(fù)制到遠(yuǎn)程主機(jī)上的,如果設(shè)置為copy=no,那么會在遠(yuǎn)程主機(jī)上尋找src源文件。
- src——源路徑,可以是ansible主機(jī)上的路徑,也可以是遠(yuǎn)程主機(jī)上的路徑,如果是遠(yuǎn)程主機(jī)上的路徑,則需要設(shè)置copy=no。
- dest——遠(yuǎn)程主機(jī)上的目標(biāo)路徑。
- mode——設(shè)置解壓縮后的文件權(quán)限。
【3】示例演示
ansible 192.168.182.129 -m unarchive -a 'src=/testdir/ansible/data.tar.gz dest=/tmp/tmp/'
8、archive模塊(打包模塊)
unarchive 模塊是打包模塊。
【1】查看幫助
ansible-doc archive -s
【2】示例演示
# path:主控端目錄,format:壓縮格式,dest:被控端目錄文件' ansible 192.168.182.129 -m archive -a 'path=/tmp/ format=gz dest=/tmp/tmp/t.tar.gz'
9、user 模塊
【1】查看幫助
ansible-doc user -s
【2】示例演示
# 創(chuàng)建用戶(present:默認(rèn),可以不寫) ansible web -m user -a 'name=test state=present' # 刪除用戶(absent) ansible web -m user -a 'name=test state=absent' # 修改密碼 # 步驟一、生成加密密碼 echo '777777'|openssl passwd -1 -stdin # 步驟二、修改秘密 ansible web -m user -a 'name=test password="$1$Jo5FD9Jr$2QB.BuybbtR35ga4O5o8N."' # 修改shell ansible web -m user -a 'name=test shell=/sbin/noglogin append=yes'
10、group 模塊
【1】查看幫助
ansible-doc group -s
【2】示例演示
# 創(chuàng)建 ansible 192.168.182.129 -m group -a 'name=testgroup system=yes' # 刪除 ansible 192.168.182.129 -m group -a 'name=testgroup state=absent'
11、yum 模塊
【1】查看幫助
ansible-doc yum -s
【2】示例演示
# 升級所有包 ansible web -m yum -a 'name="*" state=latest' # 安裝apache ansible web -m yum -a 'name="httpd" state=latest'
12、service 模塊
【1】查看幫助
ansible-doc service -s
【2】示例演示
ansible web -m service -a 'name=httpd state=started' ansible web -m service -a 'name=httpd state=started enabled=yes' ansible web -m service -a 'name=httpd state=stopped' ansible web -m service -a 'name=httpd state=restarted' ansible web -m service -a 'name=httpd state=started enabled=no'
13、file 模塊
【1】查看幫助
ansible-doc file -s
【2】示例演示
# 創(chuàng)建文件 ansible web -m file -a 'path=/tmp/88.txt mode=777 state=touch' # 創(chuàng)建目錄 ansible web -m file -a 'path=/tmp/99 mode=777 state=directory' # 刪除 ansible web -m file -a 'path=/tmp/99 state=absent'
14、setup 模塊
【1】查看幫助
ansible-doc setup -s
【2】示例演示
ansible web -m setup ansible web -m setup -a 'filter=ansible_all_ipv4_addresses'
15、cron 模塊
【1】查看幫助
ansible-doc cron -s
【2】示例演示
# 創(chuàng)建定時(shí)任務(wù) ansible 192.168.182.129 -m cron -a 'minute=* weekday=1,3,5,6,7 job="/usr/bin/wall FBI warning" name=warningcron' # 關(guān)閉定時(shí)任務(wù) ansible 192.168.182.129 -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron' # 刪除定時(shí)任務(wù) ansible 192.168.182.129 -m cron -a ' job="/usr/bin/wall FBI warning" name=warningcron state=absent'
16、hostname 模塊
【1】查看幫助
ansible-doc hostname -s
【2】示例演示
ansible 192.168.182.129 -m hostname -a 'name=192.168.182.129'
The above is the detailed content of Ansible Ad-Hoc (peer-to-peer mode). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
![WLAN expansion module has stopped [fix]](https://img.php.cn/upload/article/000/465/014/170832352052603.gif?x-oss-process=image/resize,m_fill,h_207,w_330)
If there is a problem with the WLAN expansion module on your Windows computer, it may cause you to be disconnected from the Internet. This situation is often frustrating, but fortunately, this article provides some simple suggestions that can help you solve this problem and get your wireless connection working properly again. Fix WLAN Extensibility Module Has Stopped If the WLAN Extensibility Module has stopped working on your Windows computer, follow these suggestions to fix it: Run the Network and Internet Troubleshooter to disable and re-enable wireless network connections Restart the WLAN Autoconfiguration Service Modify Power Options Modify Advanced Power Settings Reinstall Network Adapter Driver Run Some Network Commands Now, let’s look at it in detail

This article details methods to resolve event ID10000, which indicates that the Wireless LAN expansion module cannot start. This error may appear in the event log of Windows 11/10 PC. The WLAN extensibility module is a component of Windows that allows independent hardware vendors (IHVs) and independent software vendors (ISVs) to provide users with customized wireless network features and functionality. It extends the capabilities of native Windows network components by adding Windows default functionality. The WLAN extensibility module is started as part of initialization when the operating system loads network components. If the Wireless LAN Expansion Module encounters a problem and cannot start, you may see an error message in the event viewer log.

1. Introduction to the sys module The os module introduced earlier is mainly for the operating system, while the sys module in this article is mainly for the Python interpreter. The sys module is a module that comes with Python. It is an interface for interacting with the Python interpreter. The sys module provides many functions and variables to deal with different parts of the Python runtime environment. 2. Commonly used methods of the sys module. You can check which methods are included in the sys module through the dir() method: import sys print(dir(sys))1.sys.argv-Get the command line parameters sys.argv is used to implement the command from outside the program. The program is passed parameters and it is able to obtain the command line parameter column

Hello, my name is somenzz, you can call me Brother Zheng. Python's import is very intuitive, but even so, sometimes you will find that even though the package is there, we will still encounter ModuleNotFoundError. Obviously the relative path is very correct, but the error ImportError:attemptedrelativeimportwithnoknownparentpackage imports a module in the same directory and a different one. The modules of the directory are completely different. This article helps you easily handle the import by analyzing some problems often encountered when using import. Based on this, you can easily create attributes.

Preface This article continues to introduce the Python collection module. This time it mainly introduces the named tuples in it, that is, the use of namedtuple. Without further ado, let’s get started – remember to like, follow and forward~ ^_^Creating named tuples The named tuple class namedTuples in the Python collection gives meaning to each position in the tuple and enhances the readability of the code Sexual and descriptive. They can be used anywhere regular tuples are used, and add the ability to access fields by name rather than positional index. It comes from the Python built-in module collections. The general syntax used is: import collections XxNamedT

The working principle of Ansible can be understood from the above figure: the management end supports three methods of local, ssh, and zeromq to connect to the managed end. The default is to use the ssh-based connection. This part corresponds to the connection module in the above architecture diagram; you can press the application type HostInventory (host list) classification is carried out in other ways. The management node implements corresponding operations through various modules. A single module and batch execution of a single command can be called ad-hoc; the management node can implement a collection of multiple tasks through playbooks. Implement a type of functions, such as installation and deployment of web services, batch backup of database servers, etc. We can simply understand playbooks as, the system passes

All data are automatically assigned a "DOB" (Date of Birth) at the beginning. Therefore, it is inevitable to encounter date and time data when processing data at some point. This tutorial will take you through the datetime module in Python and using some peripheral libraries such as pandas and pytz. In Python, anything related to date and time is handled by the datetime module, which further divides the module into 5 different classes. Classes are simply data types that correspond to objects. The following figure summarizes the 5 datetime classes in Python along with commonly used attributes and examples. 3 useful snippets 1. Convert string to datetime format, maybe using datet

After receiving feedback from the project, the customer encountered difficulties when deploying the product using the tools we provided, and encountered problems in the host addition step, which prevented the implementation team from continuing to advance the work, so they asked us for help. Environment information: kylin10 architecture: arm has begun to take shape. During the system deployment process, for batch operations of hosts, we used ansible scripts during development. Recently, I encountered a problem with the execution process being stuck. It was initially suspected that ansible was blocked during execution. To verify this, I have sent a command to the field for testing. localhost$date2024-02-19 Sunday 17:30:41CSTlocalhost$ansibleall-i
