前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux云计算运维架构师(连载)-自动化运维ansible-08

Linux云计算运维架构师(连载)-自动化运维ansible-08

原创
作者头像
用户1880875
修改2021-07-05 14:27:29
1.4K0
修改2021-07-05 14:27:29
举报
文章被收录于专栏:用户1880875的专栏

1.1 Ansible模块说明-1

1.1.1 模块综述

Ansible是基于模块进行工作的,用户可以通过命令查看Ansible当前已加载的模块,具体代码如下所示。

[root@ansible ~]# ansible-doc -l

fortios_router_community_list Configure c

azure_rm_devtestlab_info Get Azure D

ecs_taskdefinition register a

avi_alertscriptconfig Module for

tower_receive Receive ass

netapp_e_iscsi_target NetApp E-Se

azure_rm_acs Manage an A

fortios_log_syslogd2_filter Filters for

junos_rpc Runs an arb

na_elementsw_vlan NetApp Elem

pn_ospf CLI command

pn_snmp_vacm CLI command

cp_mgmt_service_sctp Manages ser

onyx_ospf Manage OSPF

icx_command Run arbitra

cs_snapshot_policy Manages vol

nxos_install_os Set boot op

cnos_static_route Manage stat

win_eventlog Manage Wind

vmware_category Manage VMwa

vmware_host_feature_info Gathers inf

avi_cluster Module for

na_ontap_user NetApp ONTA

aci_l3out Manage Laye

memset_server_info Retrieve se

gcp_compute_subnetwork_info Gather info

azure_rm_virtualmachinescalesetextension Manage Azur

fortios_report_dataset Report data

avi_api_session Avi API Mod

avi_networkprofile Module for

avi_backup Module for

aci_interface_policy_cdp Manage CDP

fortios_firewall_vip Configure v

gcp_compute_backend_service Creates a G

iam_policy Manage IAM

fortios_system_fips_cc Configure F

fortios_log_null_device_setting Settings fo

win_inet_proxy Manages pro

fortios_firewall_auth_portal Configure f

:

查看Ansible相关信息之后,终端会进入一个说明界面,按Q键即可退出。Ansible的模块都可以通过命令进行调用,命令格式如下。

ansible [节点] -m [模块] -a [参数]

此处节点可以是某台客户机,也可以是某个主机组,-m用于指定需要调用的模块,-a用于指定参数。除此之外,Ansible还提供了各个模块的具体用法,通过ansible-doc命令即可查看,此处以以shell模块为例进行查看,其结果如下所示。

[root@ansible ~]# ansible-doc shell

> SHELL (/usr/lib/python2.7/site-packages/ansible/modules/commands/she

The `shell' module takes the command name followed by a list

of space-delimited arguments. Either a free form command or

`cmd' parameter is required, see the examples. It is almost

exactly like the [command] module but runs the command through

a shell (`/bin/sh') on the remote node. For Windows targets,

use the [win_shell] module instead.

* This module is maintained by The Ansible Core Team

* note: This module has a corresponding action plugin.

OPTIONS (= is mandatory):

- chdir

Change into this directory before running the command.

[Default: (null)]

type: path

version_added: 0.6

- cmd

The command to run followed by optional arguments.

[Default: (null)]

type: str

- creates

A filename, when it already exists, this step will *not* be

run.

[Default: (null)]

type: path

- executable

Change the shell used to execute the command.

This expects an absolute path to the executable.

[Default: (null)]

type: path

version_added: 0.9

- free_form

:

下面将介绍Ansible常用的七个模块。

1.1.2 shell模块

Ansible的shell模块可以帮助用户在远程主机上执行命令,完成一系列的工作。查看当前Ansible主机清单中的主机内容,代码及结果如下所示。

[root@ansible ~]# cat /etc/ansible/hosts

[apache]

host[1:2]

[nginx]

host3

[webserver:children]

apache

nginx

[webserver:vars]

ansible_ssh_user='root'

ansible_ssh_pass='f'

下面针对该主机清单,使用shell模块来完成一系列的查询或配置操作,示例如下所示。

l 获取主机名

[root@ansible ~]# ansible webserver -m shell -a 'hostname' -o

host2 | CHANGED | rc=0 | (stdout) host2

host3 | CHANGED | rc=0 | (stdout) host3

host1 | CHANGED | rc=0 | (stdout) host1

通过代码的反馈内容可以看到,hosts文件中各客户机的主机名,查询操作完成。

l 指定线程数

[root@ansible ~]# ansible webserver -m shell -a 'hostname' -o -f 2

host2 | CHANGED | rc=0 | (stdout) host2

host1 | CHANGED | rc=0 | (stdout) host1

host3 | CHANGED | rc=0 | (stdout) host3

使用“-f”可以指定Ansible一次命令执行并发的线程数,这里指定werserver组中所有主机的线程数为2。

l 部署Apache

[root@ansible ~]# ansible host2 -m shell -a 'yum -y install httpd' -o

host2 | CHANGED | rc=0 | (stdout) 已加载插件:

.............

已安装:

httpd.x86_64 0:2.4.6-97.el7.centos

作为依赖被安装:

apr.x86_64 0:1.4.8-7.el7

apr-util.x86_64 0:1.5.2-6.el7

httpd-tools.x86_64 0:2.4.6-97.el7.centos

mailcap.noarch 0:2.1.41-2.el7

完毕!

通过代码的反馈结果可以看到,host2主机已成功安装Apache。在host2中查看其版本号,结果如下所示。

[root@host2 ~]# httpd -v

Server version: Apache/2.4.6 (CentOS)

Server built: Nov 16 2020 16:18:20

l 查询系统负载

#查询host3的系统负载

[root@ansible ~]# ansible host3 -m shell -a 'uptime' -o

host3 | CHANGED | rc=0 | (stdout) 22:43:00 up 3:26, 3 users, load average: 0.00, 0.01, 0.05

通过代码的反馈结果可以看到host3客户机的系统负载信息。除了上面这些之外,Ansible可用的命令及参数还有很多,一些常用的参数如表1.3所示。

表1.1 shell模块常用参数

参数

备注

free_form

指定需要远程执行的命令

chdir

指定一个目录,在执行对应的命令之前,会先进入到chdir参数指定的目录中

creates

指定一个文件,当指定的文件存在时,就不执行对应命令

removes

指定一个文件,当指定的文件不存在时,就不执行对应命令

executable

默认情况下,shell模块会调用远程主机中的/bin/sh去执行对应的命令,远程主机中的默认shell都是bash。如果想要使用其他类型的shell执行命令,则可以使用此参数指定某种类型shell去执行对应的命令。指定shell文件时,需要使用绝对路径。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.1 Ansible模块说明-1
    • 1.1.1 模块综述
      • 1.1.2 shell模块
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档