mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
翻译完成
This commit is contained in:
parent
198b723993
commit
0707ed9559
@ -1,74 +1,74 @@
|
|||||||
[#]: collector: (lujun9972)
|
[#]: collector: "lujun9972"
|
||||||
[#]: translator: (MjSeven)
|
[#]: translator: "MjSeven"
|
||||||
[#]: reviewer: ( )
|
[#]: reviewer: " "
|
||||||
[#]: publisher: ( )
|
[#]: publisher: " "
|
||||||
[#]: url: ( )
|
[#]: url: " "
|
||||||
[#]: subject: (Ansible Ad-hoc Command Quick Start Guide with Examples)
|
[#]: subject: "Ansible Ad-hoc Command Quick Start Guide with Examples"
|
||||||
[#]: via: (https://www.2daygeek.com/ansible-ad-hoc-command-quick-start-guide-with-examples/)
|
[#]: via: "https://www.2daygeek.com/ansible-ad-hoc-command-quick-start-guide-with-examples/"
|
||||||
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
[#]: author: "Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/"
|
||||||
|
|
||||||
Ansible Ad-hoc Command Quick Start Guide with Examples
|
Ansible 点对点命令快速入门指南示例
|
||||||
======
|
======
|
||||||
|
|
||||||
Recently, we have written an article about the **[Ansible installation and configuration][1]**.
|
之前,我们写了一篇有关 **[Ansible 安装和配置][1]**的文章。
|
||||||
|
|
||||||
Only a few examples of how to use it are included in that tutorial.
|
在那个教程中只包含了一些使用方法的示例。
|
||||||
|
|
||||||
If you are new to Ansible, I suggest you read the Installation and Configuration section by pressing the URL above.
|
如果你是 Ansible 新手,建议你阅读上篇文章。
|
||||||
|
|
||||||
Once you’re good in that area, go ahead and play with this article.
|
一旦你熟悉了,就可以继续阅读本文了。
|
||||||
|
|
||||||
By default, Ansible uses only 5 parallel processes. If you want to perform a task on multiple hosts, you need to manually set the value of the fork count by adding **“-f [fork count]”**.
|
默认情况下,Ansible 仅使用 5 个并行进程。如果要在多个主机上执行任务,需要通过添加 **"-f [进程数]"** 选项来手动设置进程数。
|
||||||
|
|
||||||
### What is ad-hoc Command
|
### 什么是点对点 (ad-hoc) 命令?
|
||||||
|
|
||||||
The ad-hoc command is used to automate a task on one or more managed nodes. Ad-hoc commands are very simple, but they are not re-usable. It uses the **“/usr/bin/ansible”** binary to perform all actions.
|
点对点命令用于在一个或多个受控节点上自动执行任务。它非常简单,但是不可重用。它使用 **"/usr/bin/ansible"** 二进制文件执行所有操作。
|
||||||
|
|
||||||
Ad-hoc commands are best for tasks you run once. For example, if you want to check whether a given user is available or not, you can use the Ansible Quick One liner without writing a playbook.
|
点对点命令最适合运行一次的任务。例如,如果要检查指定用户是否可用,你可以使用一行命令而无需编写剧本。
|
||||||
|
|
||||||
### Why Would You Like to Know About ad-hoc Commands?
|
### 为什么你要了解点对点命令?
|
||||||
|
|
||||||
Ad-hoc commands prove the simplicity and power of the Ansible. It currently supports 3389 modules as of version 2.9, so you need to understand and learn the list of Ansible modules you want to use regularly.
|
点对点命令证明了 Ansible 的简单性和强大功能。从 2.9 版本开始,它支持 3389 个模块,因此你需要了解和学习要定期使用的 Ansible 模块列表。
|
||||||
|
|
||||||
If you are new to Ansible, you can easily practice those modules and their arguments with the help of ad-hoc command.
|
如果你是一个 Ansible 新手,可以借助点对点命令轻松地练习这些模块及参数。
|
||||||
|
|
||||||
The concepts you learn here will port over directly to the playbook language.
|
你在这里学习到的概念将直接移植到剧本中。
|
||||||
|
|
||||||
**General Syntax of ad-hoc command:**
|
**点对点命令的一般语法:**
|
||||||
|
|
||||||
```
|
```
|
||||||
ansible | [pattern] | -m [module] | -a "[module options]"
|
ansible | [pattern] | -m [module] | -a "[module options]"
|
||||||
A | B | C | D
|
A | B | C | D
|
||||||
```
|
```
|
||||||
|
|
||||||
The ad-hoc command comes with four parts and the details are below.
|
点对点命令包含四个部分,详细信息如下:
|
||||||
|
|
||||||
```
|
```markdown
|
||||||
+-----------------+--------------------------------------------------+
|
+-----------------+--------------------------------------------------+
|
||||||
| Details | Description |
|
| Details | Description |
|
||||||
+-----------------+--------------------------------------------------+
|
+-----------------+--------------------------------------------------+
|
||||||
|ansible | A command |
|
|ansible | 命令 |
|
||||||
|pattern | Input the entire inventory or a specific group |
|
|pattern | I输入清单或指定组 |
|
||||||
|module | Run the given module name |
|
|module | 运行指定的模块名称 |
|
||||||
|module options | Specify the module arguments |
|
|module options | 指定模块参数 |
|
||||||
+-----------------+--------------------------------------------------+
|
+-----------------+--------------------------------------------------+
|
||||||
```
|
```
|
||||||
|
|
||||||
### How To Use Ansible Inventory File
|
### 如何使用 Ansible 清单文件
|
||||||
|
|
||||||
If you use the default inventory file of Ansible **“/etc/ansible/hosts”**, you can call it directly.
|
如果使用 Ansible 的默认清单文件 **"/etc/ansible/hosts"**,你可以直接调用它。
|
||||||
|
|
||||||
If not, the entire path of the Ansible Inventory file should be called with the **“-i”** option.
|
如果没有默认文件,你可以使用 **"-i"** 选项调用 Ansible 清单文件的路径。
|
||||||
|
|
||||||
### What’s Pattern and How to Use it?
|
### 什么是模式以及如何使用它?
|
||||||
|
|
||||||
An Ansible pattern can refer to a single host, IP address, an inventory group, a set of groups, or all hosts in your inventory.
|
Ansible 模式可以代指某个主机、IP 地址、清单组、一组主机或者清单中的所有主机。
|
||||||
|
|
||||||
It allows you to run commands and playbooks against them. Patterns are very flexible and you can use them according to your needs.
|
它允许你对它们运行命令和剧本。模式非常灵活,你可以根据需要使用它们。
|
||||||
|
|
||||||
For example, you can exclude hosts, use wildcards or regular expressions, and more.
|
例如,你可以排除主机、使用通配符或正则表达式等等。
|
||||||
|
|
||||||
The table below describes common patterns and their use. But if it doesn’t meet your needs, you can use variables in patterns with the **“-e”** argument in the ansible-playbook.
|
下表描述了常见的模式以及用法。但是,如果它不能满足你的需求,你可以在 ansible-playbook 中使用带有 **"-e"** 参数的模式中的变量。
|
||||||
|
|
||||||
```
|
```
|
||||||
+-----------------------+------------------------------+-----------------------------------------------------+
|
+-----------------------+------------------------------+-----------------------------------------------------+
|
||||||
@ -84,35 +84,35 @@ The table below describes common patterns and their use. But if it doesn’t mee
|
|||||||
+-----------------------+------------------------------+-----------------------------------------------------+
|
+-----------------------+------------------------------+-----------------------------------------------------+
|
||||||
```
|
```
|
||||||
|
|
||||||
### What is Ansible Modules and What it Does?
|
### 什么是 Ansible 模块,它干了什么?
|
||||||
|
|
||||||
Modules (also referred to as “task plugins” or “library plugins”) are units of code that can be used to perform a specific task directly on remote hosts or through Playbooks.
|
模块,也称为“任务插件”或“库插件“,它是一组代码单元,可以直接或通过剧本在远程主机上执行指定任务。
|
||||||
|
|
||||||
Ansible executes the given module on the remote target node and collects the return values.
|
Ansible 在远程目标节点上执行指定模块并收集其返回值。
|
||||||
|
|
||||||
Each module supports multiple arguments, allowing it to meet the user’s needs. Almost all modules take **“key=value”** arguments except few.
|
每个模块都支持多个参数,可以满足用户的需求。除少数模块外,几乎所有模块都采用 **"key=value"** 参数。
|
||||||
|
|
||||||
You can add multiple arguments with the space at once, and the command/shell modules directly take the string of the command you want to run.
|
你可以一次添加带有空格的多个参数,command 或 shell 模块会直接运行你输入的字符串。
|
||||||
|
|
||||||
We will add a table with the most frequently used **“module options”** arguments.
|
我们将添加一个包含最常用的”模块选项“参数的表。
|
||||||
|
|
||||||
To list all available modules, run the command below.
|
列出所有可用的模块,运行以下命令:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible-doc -l
|
$ ansible-doc -l
|
||||||
```
|
```
|
||||||
|
|
||||||
Run the command below to read the documentation for the given module
|
运行以下命令来阅读指定模块的文档。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible-doc [Module]
|
$ ansible-doc [Module]
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1) How to List the Contents of a Directory Using Ansible on Linux
|
### 1)如何在 Linux 上使用 Ansible 列出目录的内容
|
||||||
|
|
||||||
This can be done using the Ansible command module as follows. We have listed the contents of the **“daygeek”** user’s home directory on the **“node1.2g.lab”** and **“node2.2g.lab”** remote server.
|
可以使用 Ansible command 模块来完成这项操作,如下所示。我们列出了 **"node1.2g.lab"** 和 **"nod2.2g.lab"** 远程服务器上 **"daygeek"** 用户主目录的内容。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m command -a "ls -lh /home/daygeek"
|
$ ansible web -m command -a "ls -lh /home/daygeek"
|
||||||
|
|
||||||
node1.2g.lab | CHANGED | rc=0 >>
|
node1.2g.lab | CHANGED | rc=0 >>
|
||||||
@ -141,11 +141,11 @@ drwxr-xr-x. 2 daygeek daygeek 6 Nov 9 09:55 Templates
|
|||||||
drwxr-xr-x. 2 daygeek daygeek 6 Nov 9 09:55 Videos
|
drwxr-xr-x. 2 daygeek daygeek 6 Nov 9 09:55 Videos
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2) How to Manage Files Using Ansible on Linux
|
### 2)如何在 Linux 使用 Ansible 管理文件
|
||||||
|
|
||||||
Ansible “copy module” copies a file from a local system to a remote system. Use the Ansible command module to move or copy files to a remote machine.
|
Ansible 的 "copy 模块" 将文件从本地系统复制到远程系统。使用 Ansible command 模块将文件移动或复制到远程计算机。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m copy -a "src=/home/daygeek/backup/CentOS7.2daygeek.com-20191025.tar dest=/home/u1" --become
|
$ ansible web -m copy -a "src=/home/daygeek/backup/CentOS7.2daygeek.com-20191025.tar dest=/home/u1" --become
|
||||||
|
|
||||||
node1.2g.lab | CHANGED => {
|
node1.2g.lab | CHANGED => {
|
||||||
@ -187,9 +187,9 @@ node2.2g.lab | CHANGED => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We can verify it by running the command below.
|
我们可以运行以下命令进行验证:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m command -a "ls -lh /home/u1" --become
|
$ ansible web -m command -a "ls -lh /home/u1" --become
|
||||||
|
|
||||||
node1.2g.lab | CHANGED | rc=0 >>
|
node1.2g.lab | CHANGED | rc=0 >>
|
||||||
@ -203,97 +203,97 @@ total 36K
|
|||||||
-rw-rw-r--. 1 u1 u1 18 Jan 23 02:21 magi.txt
|
-rw-rw-r--. 1 u1 u1 18 Jan 23 02:21 magi.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
To copy a file from one location to another on the remote machine, use the following command.
|
要将文件从一个位置复制到远程计算机上的另一个位置,使用以下命令。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m command -a "cp /home/u2/magi/ansible-1.txt /home/u2/magi/2g" --become
|
$ ansible web -m command -a "cp /home/u2/magi/ansible-1.txt /home/u2/magi/2g" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
To move a file, use the following command.
|
移动文件,使用以下命令:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m command -a "mv /home/u2/magi/ansible.txt /home/u2/magi/2g" --become
|
$ ansible web -m command -a "mv /home/u2/magi/ansible.txt /home/u2/magi/2g" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
To create a new file named **“ansible.txt”** under **“u1”** user, run the following command.
|
在 **"u1"** 用户目录下创建一个名为 **"ansible.txt"** 的新文件,运行以下命令:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m file -a "dest=/home/u1/ansible.txt owner=u1 group=u1 state=touch" --become
|
$ ansible web -m file -a "dest=/home/u1/ansible.txt owner=u1 group=u1 state=touch" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
To create a new directory named **“magi”** under the **“u1”** user, run the following command. **_“**The file module can also create directories as follows**_“**.
|
在 **"u1"** 用户目录下创建一个名为 **"magi"** 的新目录,运行以下命令。**”file 模块还可以创建目录“**。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m file -a "dest=/home/u1/magi mode=755 owner=u2 group=u2 state=directory" --become
|
$ ansible web -m file -a "dest=/home/u1/magi mode=755 owner=u2 group=u2 state=directory" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
To change the permission of the **“ansible.txt”** file to **“777”** under **“u1”** user, run the following command.
|
将 **"u1"** 用户目录下的 **"ansible.txt"** 文件权限更改为 **"777"**,运行以下命令:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m file -a "dest=/home/u1/ansible.txt mode=777" --become
|
$ ansible web -m file -a "dest=/home/u1/ansible.txt mode=777" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
To delete the “ansible.txt” file under “u1” user, run the following command.
|
删除 **"u1"** 用户目录下的 **"ansible.txt"** 文件,运行以下命令:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m file -a "dest=/home/u2/magi/ansible-1.txt state=absent" --become
|
$ ansible web -m file -a "dest=/home/u2/magi/ansible-1.txt state=absent" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
Use the following command to delete a directory and it will delete the given directory recursively.
|
使用以下命令删除目录,它将递归删除指定目录。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m file -a "dest=/home/u2/magi/2g state=absent" --become
|
$ ansible web -m file -a "dest=/home/u2/magi/2g state=absent" --become
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3) User Management
|
### 3) 用户管理
|
||||||
|
|
||||||
You can easily perform the user management activity through Ansible, such as user creation, deleting a user, and adding a user to the group.
|
你可以使用 Ansible 轻松执行用户管理活动。例如创建、删除用户以及向一个组添加用户。
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible all -m user -a "name=foo password=[crypted password here]"
|
$ ansible all -m user -a "name=foo password=[crypted password here]"
|
||||||
```
|
```
|
||||||
|
|
||||||
To remove a user, run the following command.
|
运行以下命令删除用户:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible all -m user -a "name=foo state=absent"
|
$ ansible all -m user -a "name=foo state=absent"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4) Managing Package
|
### 4) 管理包
|
||||||
|
|
||||||
Package installation can be easily managed using the appropriate Ansible Package Manager module. For example, we are going to use the yum module to manage packages on the CentOS system.
|
使用合适的 Ansible 包管理器模块可以轻松地管理安装包。例如,我们将使用 yum 模块来管理 CentOS 系统上的软件包。
|
||||||
|
|
||||||
To install the latest Apache (httpd) package.
|
安装最新的 Apache(httpd):
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m yum -a "name=httpd state=latest"
|
$ ansible web -m yum -a "name=httpd state=latest"
|
||||||
```
|
```
|
||||||
|
|
||||||
To uninstall the Apache (httpd) package.
|
卸载 Apache(httpd) 包:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m yum -a "name=httpd state=absent"
|
$ ansible web -m yum -a "name=httpd state=absent"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 5) Managing Service
|
### 5) 管理服务
|
||||||
|
|
||||||
Use the following Ansible module command to manage any service on Linux using Ansible
|
使用以下 Ansible 模块命令可以在 Linux 上管理任何服务。
|
||||||
|
|
||||||
To stop the httpd service
|
停止 httpd 服务:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m service -a "name=httpd state=stopped"
|
$ ansible web -m service -a "name=httpd state=stopped"
|
||||||
```
|
```
|
||||||
|
|
||||||
To start the httpd service
|
启动 httpd 服务:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m service -a "name=httpd state=started"
|
$ ansible web -m service -a "name=httpd state=started"
|
||||||
```
|
```
|
||||||
|
|
||||||
To restart the httpd service
|
重启 httpd 服务:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ ansible web -m service -a "name=httpd state=restarted"
|
$ ansible web -m service -a "name=httpd state=restarted"
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -303,7 +303,7 @@ via: https://www.2daygeek.com/ansible-ad-hoc-command-quick-start-guide-with-exam
|
|||||||
|
|
||||||
作者:[Magesh Maruthamuthu][a]
|
作者:[Magesh Maruthamuthu][a]
|
||||||
选题:[lujun9972][b]
|
选题:[lujun9972][b]
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
译者:[MjSeven](https://github.com/MjSeven)
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user