暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

[译] Ansible:临时命令

原创 黎青峰 2022-05-12
758

原文地址:https://oracle-base.com/articles/misc/ansible-ad-hoc-commands
原文作者:Tim Hall
本文演示了如何使用 Ansible 运行临时命令。它基于临时命令简介文档,但针对Ansible : First Steps文章中描述的基础设施进行了修改。

目录导读

套餐

我们使用 "dnf "模块在 "appservers "组的所有主机上安装 "nginx "包。在以前的版本中,我们会使用 "yum "模块。在参数中,我们包括软件包的名称和我们想要的状态。对于安装来说,状态是 "现在 "或 “最新”。注意,我们使用了–become标志,它告诉Ansible它需要以根用户的身份运行该命令。在这些虚拟机上,我们的用户不需要密码就可以sudo,但如果需要密码,我们可以加入–ask-become-pass标志,这样就会提示我们输入密码,以实现权限升级。

$ ansible appservers -m dnf -a "name=nginx state=present" --become

我们使用“最新”状态更新现有包。如前所述,如果尚未安装,这将安装该软件包。

$ ansible appservers -m dnf -a "name=nginx state=latest" --become

我们使用“缺席”状态删除一个包。

$ ansible appservers -m dnf -a "name=nginx state=absent" --become

我们使用通配符名称和“最新”状态更新服务器上的所有包。

$ ansible appservers -m dnf -a "name=* state=latest" --become

文件
我们在本地服务器上创建一个文件。

$ touch /tmp/my-file.txt

我们将文件复制到“appservers”组中的所有主机。这就像使用 SCP 复制文件。

$ ansible appservers -m copy -a "src=/tmp/my-file.txt dest=/tmp/my-file.txt"

我们更改远程文件的权限和所有权。注意我们使用–become, 来提升权限。

$ ansible appservers -m file -a "dest=/tmp/my-file.txt mode=777 owner=root group=root" --become

我们从远程服务器中删除该文件。由于该文件现在由 root 拥有,我们需要使用提升的权限。

$ ansible appservers -m file -a "dest=/tmp/my-file.txt state=absent" --become

用户和组

所有操作都需要提升权限,因此命令包含–become标志。

使用“组”模块在所有数据库主机上创建一些组。

$ ansible 数据库 -m 组 -a “name=oinstall gid=54321” --become
$ ansible 数据库 -m 组 -a “name=dba gid=54322” --become
$ ansible 数据库 -m 组 -a “name=oper gid=54323” --become
使用“用户”模块创建一个用户,该用户是所有数据库主机上这些组的一部分。使用“*”的密码意味着它是一个禁用的帐户。

$ ansible databases -m user -a "name=oracle password='*' uid=54321 groups='oinstall,dba,oper'" --become

删除用户。

$ ansible 数据库 -m 用户 -a "name=oracle state=absent" --become

删除组。

$ ansible 数据库 -m 组 -a "name=oinstall state=absent" --become $ ansible 数据库 -m 组 -a "name=dba state=absent" --become $ ansible 数据库 -m 组 -a "name=oper state=absent" --become

服务

我们使用“dnf”模块在“appservers”组中的所有主机上安装 NGINX。

$ ansible appservers -m dnf -a "name=nginx state=present" --become

我们使用“服务”模块在“appservers”组中的所有主机上启动、重启和停止 NGINX。

$ ansible appservers -m service -a "name=nginx state=started" --become $ ansible appservers -m service -a "name=nginx state=restarted" --become $ ansible appservers -m service -a "name=nginx state=stopped" --become

我们从“appservers”组中的主机中删除 NGINX。

$ ansible appservers -m dnf -a "name=nginx state=absent" --become

重启服务器

可以使用带有默认命令模块的“rebo​​ot”命令重新启动服务器。这通常会返回错误,因为主机不再可用于 SSH,但服务器会按请求重新启动。

$ ansible 数据库 -a "/sbin/reboot" --become database1.localdomain | 失败 | rc=-1 >> ssh 连接主机失败:ssh: connect to host database1.localdomain port 22: Connection refused $

当这些命令作为剧本的一部分运行时,它们可以容忍中断。

最后修改时间:2022-05-12 15:51:14
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论