Ansible Ad-Hoc (peer-to-peer mode)
Official documentation: https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html
简介
Ad-hoc命令是一种临时输入并执行的命令,通常用于测试和调试。它们不需要永久保存,简单来说,ad-hoc就是“即时命令”。
常用模块
1、command 模块(默认模块)
默认模块,没有shell强大,基本上shell模块都可以支持command模块的功能。
【1】帮助
ansible-doc command # 推荐使用下面这个 ansible-doc command -s
【2】参数解释
- free_form——必须参数,指定需要远程执行的命令。需要说明一点,free_form 参数与其他参数(如果想要使用一个参数,那么则需要为这个参数赋值,也就是name=value模式)并不相同。比如,当我们想要在远程主机上执行 ls 命令时,我们并不需要写成”free_form=ls” ,这样写反而是错误的,因为并没有任何参数的名字是 free_form,当我们想要在远程主机中执行 ls 命令时,直接写成 ls 即可。因为 command 模块的作用是执行命令,所以,任何一个可以在远程主机上执行的命令都可以被称为 free_form。
- chdir——此参数的作用就是指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。
- creates——看到 creates,你可能会从字面上理解这个参数,但是使用这个参数并不会帮助我们创建文件,它的作用是当指定的文件存在时,就不执行对应命令,比如,如果 /testdir/test文件存在,就不执行我们指定的命令。
- removes——与 creates 参数的作用正好相反,它的作用是当指定的文件不存在时,就不执行对应命令,比如,如果 /testdir/tests 文件不存在,就不执行我们指定的命令,此参数并不会帮助我们删除文件。
【3】示例演示
# 上面命令表示在 web 主机上执行 ls 命令,因为使用的是 root 用户,所以默认情况下,ls 出的结果是 web 主机中 root 用户家目录中的文件列表。 ansible web -m command -a "ls" # chdir 参数表示执行命令之前,会先进入到指定的目录中,所以上面命令表示查看 web 主机上 /testdir 目录中的文件列表,返回显示有2个文件。 ansible web -m command -a "chdir=/testdir ls" # 下面命令表示 /testdir/testfile1 文件存在于远程主机中,则不执行对应命令。/testdir/testfile3 不存在,才执行”echo test”命令。 ansible web -m command -a "creates=/testdir/testfile1 echo test" # 下面命令表示 /testdir/testfile3 文件不存在于远程主机中,则不执行对应命令。/testdir/testfile1 存在,才执行”echo test”命令。 ansible web -m command -a "removes=/testdir/testfile1 echo test"
2、shell 模块
shell模块 [执行远程主机的shell/python等脚本]。
【1】查看帮助
ansible-doc shell -s
【2】示例演示
# -o:一行显示 # 安装httpd ansible web -m shell -a 'yum -y install httpd' -o # 查看时间 ansible web -m shell -a 'uptime' -o
3、script 模块
script模块 [在远程主机执行主控端的shell/python等脚本 ]。
【1】查看帮助
ansible-doc script -s
【2】参数解释
- free_form——必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。
- chdir——此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。
- creates——使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
- removes——使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。
【3】示例演示
# 下面命令表示 ansible 主机中的 /testdir/testscript.sh 脚本将在 web 主机中执行,执行此脚本之前,会先进入到 web 主机中的 /opt 目录 ansible web -m script -a "chdir=/opt /testdir/testscript.sh" # 下面命令表示,web主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 web 主机中执行。 ansible web -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh" # 下面命令表示,web 主机中的 /testdir/testfile1 文件存在,ansible 主机中的 /testdir/testscript.sh 脚本则会在 web 主机中执行。 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 模块 从主控端复制文件到被控端。
【1】查看帮助
ansible-doc copy -s
【2】示例演示
# -a,--args:后面接参数 ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777' # backup=yes/no:文件存在且文件内容不一样是否备份,默认不备份 ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777 backup=yes'
6、fetch 模块
copy 模块从被控端复制文件到主控端,正好跟copy相反。
【1】查看帮助
ansible-doc fetch -s
【2】示例演示
# 跟copy支持的参数差不多,src:远端主机的目录,dest:主控端目录,其实真正存放的目录在:/tmp/192.168.182.129/tmp/up.sh,会按每台主机分组存放 #This `must' be a file, not a directory:只支持单个文件获取 ansible 192.168.182.129 -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"
7、unarchive 模块(解包模块)
unarchive 模块是解包模块。
【1】查看帮助
ansible-doc unarchive -s
【2】参数解释
- copy——默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件。
- src——源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no。
- dest——远程主机上的目标路径。
- mode——设置解压缩后的文件权限。
【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】示例演示
# 创建用户(present:默认,可以不写) 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】示例演示
# 创建 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】示例演示
# 创建文件 ansible web -m file -a 'path=/tmp/88.txt mode=777 state=touch' # 创建目录 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】示例演示
# 创建定时任务 ansible 192.168.182.129 -m cron -a 'minute=* weekday=1,3,5,6,7 job="/usr/bin/wall FBI warning" name=warningcron' # 关闭定时任务 ansible 192.168.182.129 -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron' # 删除定时任务 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

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics


![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

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

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.

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

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

In CTR estimation, the mainstream method uses feature embedding+MLP, in which features are very critical. However, for the same features, the representation is the same in different samples. This way of inputting to the downstream model will limit the expressive ability of the model. In order to solve this problem, a series of related work has been proposed in the field of CTR estimation, which is called feature enhancement module. The feature enhancement module corrects the output results of the embedding layer based on different samples to adapt to the feature representation of different samples and improve the expression ability of the model. Recently, Fudan University and Microsoft Research Asia jointly published a review on feature enhancement work, comparing the implementation methods and effects of different feature enhancement modules. Now, let us introduce a
