ansible 的基本模块操作介绍

本贴最后更新于 2245 天前,其中的信息可能已经物是人非

下面的示例演示如何使用/usr/bin/Ansible 用于运行临时任务。

什么是临时命令?

即命令是您可能输入的东西,可以非常快地完成一些事情,但不想保存到以后。

这是一个很好的地方,可以开始了解 Ansible 在学习剧本之前可以做些什么-即席命令也可以用来做一些你不一定想要写完整的剧本的快速的事情。

一般说来,经验之谈的真正力量在于剧本。你为什么要使用临时任务而不是剧本呢?

例如,如果您想在圣诞节假期关闭所有的实验室,您可以在 Ansible 中执行一个快速的一行程序,而无需编写剧本。

但是,对于配置管理和部署,您需要学习使用‘/usr/bin/Ansible-playbook’-您将在这里学到的概念将直接移植到剧本语言中。

(见使用剧本有关这些的更多信息)

如果你没看过处理库存已经过了,请先看一下,然后我们就出发了。

一、并行性和 Shell 命令

任意的例子。

让我们使用 Ansible 的命令行工具重新启动亚特兰大的所有 Web 服务器,每次 10 台。首先,让我们设置 SSH 代理,以便它能够记住我们的凭据:

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa

如果您不想使用 ssh-agent,并且希望使用密码而不是密钥来代替 ssh,则可以使用 --ask-pass (-k),但是使用 ssh-agent 要好得多。

现在,要在组中的所有服务器上运行该命令,在本例中,亚特兰大,在 10 个平行叉子中:

$ ansible atlanta -a "/sbin/reboot" -f 10

/usr/bin/ansible 将默认为从用户帐户运行。如果您不喜欢这种行为,请输入“-u 用户名”。如果希望以不同用户的身份运行命令,则如下所示:

$ ansible atlanta -a "/usr/bin/foo" -u username

通常,您不会只想从您的用户帐户中做一些事情。如果您想通过权限提升运行命令:

$ ansible atlanta -a "/usr/bin/foo" -u username --become [--ask-become-pass]

使用 --ask-become-pass (-K)如果您没有使用无密码特权提升方法(sudo/su/pfexec/doas/etc)。这将交互式地提示您输入要使用的密码。使用无密码设置可以使事情更容易自动化,但这不是必需的。

也有可能成为除 root 之外的其他用户。--become-user:

$ ansible atlanta -a "/usr/bin/foo" -u username --become --become-user otheruser [--ask-become-pass]

注:

很少有一些用户有安全规则,将 sudo/pbrun/doas 环境限制为只运行特定的命令路径。这不适用于 Ansible 的非自举哲学和数百个不同的模块。如果这样做,请从没有此约束的特殊帐户中使用 Ansible。在不共享对未经授权的用户的访问的情况下,一种方法是将 Ansible 与可抗塔,它可以持有 SSH 证书,并允许某些组织的成员代表他们使用 SSH 证书,而无需直接访问。

好的,这些都是基本的。如果你还没有读过关于模式和组的文章,那就回去读吧处理模式.

这,这个,那,那个 -f 10 在上面指定使用 10 个同时使用的进程。您也可以将其设置为配置可测性以避免再次设置它。缺省值实际上是 5,这是非常小和保守的。您可能会想要与更多的同时主机,所以请随意提出这一点。如果您有更多的主机设置的分叉计数,Ansible 将与他们交谈,但它将需要更长的时间。请随意将此值推送到您的系统所能处理的高度!

您还可以选择要运行的 Ansible“模块”。通常,命令也会接受 -m 对于模块名,但是默认的模块名是“命令”,所以我们不需要一直指定它。我们会用 -m 在后面的示例中运行其他一些使用模块.

注:

The command module does not support extended shell syntax like piping and redirects (although shell variables will always work). If your command requires shell-specific syntax, use the shell module instead. Read more about the differences on the Working With Modules page.

使用 shell 模块看起来是这样的:

$ ansible raleigh -m shell -a 'echo $TERM'

在使用 Ansible 运行任何命令时_临时_CLI(相对于 playbook),特别注意 shell 引用规则,这样本地 shell 在传递给 Ansible 之前不会吃变量。例如,在上面的示例中使用双引号而不是单引号将计算您所在框上的变量。

到目前为止,我们一直在演示简单的命令执行,但大多数 Ansible 模块并不是简单的命令式脚本。相反,它们使用声明式模型,计算和执行达到指定的最终状态所需的操作。此外,它们通过在开始之前检查当前状态来实现一种幂等效应,如果当前状态与指定的最终状态匹配,则什么也不做。然而,我们也认识到运行任意命令是有价值的,因此 Ansible 很容易支持这两种命令。

二、File Transfer

Here’s another use case for the /usr/bin/ansible command line. Ansible can SCP lots of files to multiple machines in parallel.

To transfer a file directly to many servers:

$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

If you use playbooks, you can also take advantage of the template module, which takes this another step further. (See module and playbook documentation).

The file module allows changing ownership and permissions on files. These same options can be passed directly to the copy module as well:

$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

The file module can also create directories, similar to mkdir -p:

$ ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

As well as delete directories (recursively) and delete files:

$ ansible webservers -m file -a "dest=/path/to/c state=absent"

Managing Packages

There are modules available for yum and apt. Here are some examples with yum.

Ensure a package is installed, but don’t update it:

$ ansible webservers -m yum -a "name=acme state=present"

Ensure a package is installed to a specific version:

$ ansible webservers -m yum -a "name=acme-1.5 state=present"

Ensure a package is at the latest version:

$ ansible webservers -m yum -a "name=acme state=latest"

Ensure a package is not installed:

$ ansible webservers -m yum -a "name=acme state=absent"

Ansible has modules for managing packages under many platforms. If there isn’t a module for your package manager, you can install packages using the command module or (better!) contribute a module for your package manager. Stop by the mailing list for info/details.

Users and Groups

The ‘user’ module allows easy creation and manipulation of existing user accounts, as well as removal of user accounts that may exist:

$ ansible all -m user -a "name=foo password="

$ ansible all -m user -a "name=foo state=absent"

See the Working With Modules section for details on all of the available options, including how to manipulate groups and group membership.

Deploying From Source Control

Deploy your webapp straight from git:

$ ansible webservers -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

Since Ansible modules can notify change handlers it is possible to tell Ansible to run specific tasks when the code is updated, such as deploying Perl/Python/PHP/Ruby directly from git and then restarting apache.

Managing Services

Ensure a service is started on all webservers:

$ ansible webservers -m service -a "name=httpd state=started"

Alternatively, restart a service on all webservers:

$ ansible webservers -m service -a "name=httpd state=restarted"

Ensure a service is stopped:

$ ansible webservers -m service -a "name=httpd state=stopped"

Time Limited Background Operations

Long running operations can be run in the background, and it is possible to check their status later. For example, to execute long_running_operation asynchronously in the background, with a timeout of 3600 seconds (-B), and without polling (-P):

$ ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"

If you do decide you want to check on the job status later, you can use the async_status module, passing it the job id that was returned when you ran the original job in the background:

$ ansible web1.example.com -m async_status -a "jid=488359678239.2844"

Polling is built-in and looks like this:

$ ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

The above example says “run for 30 minutes max (-B 30*60=1800), poll for status (-P) every 60 seconds”.

Poll mode is smart so all jobs will be started before polling will begin on any machine. Be sure to use a high enough --forks value if you want to get all of your jobs started very quickly. After the time limit (in seconds) runs out (-B), the process on the remote nodes will be terminated.

Typically you’ll only be backgrounding long-running shell commands or software upgrades. Backgrounding the copy module does not do a background file transfer. Playbooks also support polling, and have a simplified syntax for this.

Gathering Facts

Facts are described in the playbooks section and represent discovered variables about a system. These can be used to implement conditional execution of tasks but also just to get ad-hoc information about your system. You can see all facts via:

$ ansible all -m setup # 查看每台机可用再playbook上的变量或者其他信息

It’s also possible to filter this output to just export certain facts, see the “setup” module documentation for details.

Read more about facts at Variables once you’re ready to read up on Playbooks.

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...