华子目录  Ad-hoc命令和模块简介 1.概念 2.格式 3.Ansible命令常用参数 4.模块类型 4.1 三种模块类型 4.2`Ansible`核心模块和附加模块 示例1 示例2   
 
Ansible提供两种方式去完成任务,一是ad-hoc命令,一是写Ansible playbook(剧本)Ad-hoc命令:即交互式临时命令,是一种单条命令,命令不需要特别保存下来,执行后即结束,可以用于执行简单的临时命令,相当于shell命令Ansible playbook:更适合解决复杂或需固化下来的任务,相当于Linux系统的Shell脚本[ root@server ~] 
使用ansible命令时,必须指明受管主机的信息,如果已经设置过主机清单文件(/etc/ansible/hosts),则可以使用all参数来指代全体受管控的主机,-a是要传递给模块的参数,只有功能极其简单的模块才不需要额外参数,所以大多情况下-m与-a参数都会同时出现 参数 说明 -m指定要使用地模块名 -a设置传递给模块地参数 -i指定主机清单文件 -k手动输入ssh协议密码 -S使用su命令 -M指定要使用地模块路径 -T设置ssh协议连接超时时间 -h帮助信息 --version查看ansible版本信息 
 
Ansible服务的强大之处在于只需要一条命令,便可以操控成千上万台的主机节点,由于Ansible服务实际上只是一个框架,能够完成工作的是模块化功能代码核心模块:由Ansible的官方团队提供附加模块:由各个社区提供的,如:openstack社区、docker社区等用户自定义模块:自己设计的模块Ansible核心模块和附加模块有1000+,需要使用帮助文档来查询、记忆、理解 查询模块信息: [ root@server ~] 
[ root@server ~] 
>  ANSIBLE.BUILTIN.PING    ( /usr/lib/python3.9/site-packages/ansible/modules/ping.py) A trivial test  module, this module always returns ` pong' onsuccessful contact. It does not make  sense in  playbooks, but itis useful from ` ' to verify the ability tologin and that a usable Python is configured. This is NOT ICMPping, this is just a trivial test module that requires Pythonon the remote-node. For Windows targets, use the[ansible.windows.win_ping] module instead. For Network targets,use the [ansible.netcommon.net_ping] module instead.ADDED IN: historicalOPTIONS (= is mandatory):- dataData to return for the `ping'  return  value.If this parameter is set  to `crash', the module will cause anexception.default: pongtype: strATTRIBUTES:check_mode:description: Can run in  check_mode and return  changed status prediction withoutmodifying targetsupport: fulldiff_mode:
.. .. .. 
.. .. .. 
.. .. .. 
[ root@server ~] 
7736 
[ root@server ~] 
ansible.builtin.yum                                                                              Manages packages with the.. .
ansible.builtin.yum_repository                                                                   Add or r.. .
community.general.yum_versionlock                                                                Locks / unlocks a installed package( s)  from being updated .. .
 
通过ansible给node1和node2下载安装httpd [ root@node1 ~] 
正在更新 Subscription Management 软件仓库。
无法读取客户身份本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。上次元数据过期检查:1:43:02 前,执行于 2024 年07月06日 星期六 11 时32分13秒。
可安装的软件包
httpd.x86_64                               2.4 .57-8.el9                               app
[ root@node2 ~] 
正在更新 Subscription Management 软件仓库。
无法读取客户身份本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。上次元数据过期检查:1:47:15 前,执行于 2024 年07月06日 星期六 11 时28分19秒。
可安装的软件包
httpd.x86_64                               2.4 .57-8.el9                               app
[ root@server ~] 
node1.example.com
node2.example.com[ root@server ~] 
node1.example.com |  SUCCESS = >  { "ansible_facts" :  { "discovered_interpreter_python" :  "/usr/bin/python3" } ,"changed" :  false,"ping" :  "pong" 
} 
node2.example.com |  SUCCESS = >  { "ansible_facts" :  { "discovered_interpreter_python" :  "/usr/bin/python3" } ,"changed" :  false,"ping" :  "pong" 
} [ root@server ~] 
node2.example.com |  CHANGED = >  { "ansible_facts" :  { "discovered_interpreter_python" :  "/usr/bin/python3" } ,"changed" :  true,"msg" :  "" ,"rc" :  0 ,"results" :  [ "Installed: httpd-tools-2.4.57-8.el9.x86_64" ,"Installed: httpd-filesystem-2.4.57-8.el9.noarch" ,"Installed: apr-util-1.6.1-23.el9.x86_64" ,"Installed: httpd-2.4.57-8.el9.x86_64" ,"Installed: apr-util-bdb-1.6.1-23.el9.x86_64" ,"Installed: mod_lua-2.4.57-8.el9.x86_64" ,"Installed: httpd-core-2.4.57-8.el9.x86_64" ,"Installed: apr-util-openssl-1.6.1-23.el9.x86_64" ,"Installed: centos-logos-httpd-90.8-1.el9.noarch" ,"Installed: mod_http2-2.0.26-2.el9.x86_64" ,"Installed: apr-1.7.0-12.el9.x86_64" ] 
} 
node1.example.com |  CHANGED = >  { "ansible_facts" :  { "discovered_interpreter_python" :  "/usr/bin/python3" } ,"changed" :  true,"msg" :  "" ,"rc" :  0 ,"results" :  [ "Installed: httpd-tools-2.4.57-8.el9.x86_64" ,"Installed: apr-util-1.6.1-23.el9.x86_64" ,"Installed: httpd-2.4.57-8.el9.x86_64" ,"Installed: apr-util-bdb-1.6.1-23.el9.x86_64" ,"Installed: httpd-filesystem-2.4.57-8.el9.noarch" ,"Installed: mod_lua-2.4.57-8.el9.x86_64" ,"Installed: httpd-core-2.4.57-8.el9.x86_64" ,"Installed: apr-util-openssl-1.6.1-23.el9.x86_64" ,"Installed: centos-logos-httpd-90.8-1.el9.noarch" ,"Installed: mod_http2-2.0.26-2.el9.x86_64" ,"Installed: apr-1.7.0-12.el9.x86_64" ] 
} 
[ root@node1 ~] 
正在更新 Subscription Management 软件仓库。
无法读取客户身份本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。上次元数据过期检查:1:48:50 前,执行于 2024 年07月06日 星期六 11 时32分13秒。
已安装的软件包
httpd.x86_64                              2.4 .57-8.el9                               @app
[ root@node2 ~] 
正在更新 Subscription Management 软件仓库。
无法读取客户身份本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。上次元数据过期检查:1:52:53 前,执行于 2024 年07月06日 星期六 11 时28分19秒。
已安装的软件包
httpd.x86_64                              2.4 .57-8.el9                               @app
[ root@server ~] 
node1.example.com |  CHANGED |  rc = 0  >> 
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               4 .0M     0   4 .0M    0 % /dev
tmpfs                  968M     0   968M    0 % /dev/shm
tmpfs                  388M  9 .6M  378M    3 % /run
/dev/mapper/rhel-root   16G  7 .0G  8 .7G   45 % /
/dev/nvme0n1p1         395M  235M  161M   60 % /boot
tmpfs                  194M   52K  194M    1 % /run/user/42
tmpfs                  194M   36K  194M    1 % /run/user/0
node2.example.com |  CHANGED |  rc = 0  >> 
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               4 .0M     0   4 .0M    0 % /dev
tmpfs                  968M     0   968M    0 % /dev/shm
tmpfs                  388M  9 .5M  378M    3 % /run
/dev/mapper/rhel-root   16G  4 .1G   12G   27 % /
/dev/nvme0n1p1         395M  235M  161M   60 % /boot
tmpfs                  194M   52K  194M    1 % /run/user/42
tmpfs                  194M   36K  194M    1 % /run/user/0
[ root@server ~] 
node2.example.com |  CHANGED |  rc = 0  >> 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
[ root@server ~] 
node1.example.com |  CHANGED |  rc = 0  >> 
NAME = "Red Hat Enterprise Linux" 
VERSION = "9.1 (Plow)" 
ID = "rhel" 
ID_LIKE = "fedora" 
VERSION_ID = "9.1" 
PLATFORM_ID = "platform:el9" 
PRETTY_NAME = "Red Hat Enterprise Linux 9.1 (Plow)" 
ANSI_COLOR = "0;31" 
LOGO = "fedora-logo-icon" 
CPE_NAME = "cpe:/o:redhat:enterprise_linux:9::baseos" 
HOME_URL = "https://www.redhat.com/" 
DOCUMENTATION_URL = "https://access.redhat.com/documentation/red_hat_enterprise_linux/9/" 
BUG_REPORT_URL = "https://bugzilla.redhat.com/" REDHAT_BUGZILLA_PRODUCT = "Red Hat Enterprise Linux 9" 
REDHAT_BUGZILLA_PRODUCT_VERSION = 9.1 
REDHAT_SUPPORT_PRODUCT = "Red Hat Enterprise Linux" 
REDHAT_SUPPORT_PRODUCT_VERSION = "9.1" 
[ root@server ~] 
node1.example.com |  CHANGED |  rc = 0  >> 
node1.example.com