Ad-hoc命令和模块简介

Ad-hoc命令和模块简介

1.概念

  • Ansible提供两种方式去完成任务,一是ad-hoc命令,一是写Ansible playbook(剧本)
  • Ad-hoc命令:即交互式临时命令,是一种单条命令,命令不需要特别保存下来,执行后即结束,可以用于执行简单的临时命令,相当于shell命令
  • Ansible playbook:更适合解决复杂或需固化下来的任务,相当于Linux系统的Shell脚本

2.格式

[root@server ~]# ansible 主机或组 -m 模块名 -a '模块参数' -i 清单文件
  • 使用ansible命令时,必须指明受管主机的信息,如果已经设置过主机清单文件(/etc/ansible/hosts),则可以使用all参数来指代全体受管控的主机,-a是要传递给模块的参数,只有功能极其简单的模块才不需要额外参数,所以大多情况下-m-a参数都会同时出现

3.Ansible命令常用参数

参数 说明
-m 指定要使用地模块名
-a 设置传递给模块地参数
-i 指定主机清单文件
-k 手动输入ssh协议密码
-S 使用su命令
-M 指定要使用地模块路径
-T 设置ssh协议连接超时时间
-h 帮助信息
--version 查看ansible版本信息

4.模块类型

  • Ansible服务的强大之处在于只需要一条命令,便可以操控成千上万台的主机节点,由于Ansible服务实际上只是一个框架,能够完成工作的是模块化功能代码

4.1 三种模块类型

  • 核心模块:由Ansible的官方团队提供
  • 附加模块:由各个社区提供的,如:openstack社区、docker社区
  • 用户自定义模块自己设计的模块

4.2Ansible核心模块和附加模块

  • 1000+,需要使用帮助文档来查询、记忆、理解
  • 查询模块信息:
[root@server ~]# ansible-doc  模块名称
[root@server ~]# ansible-doc ping
> ANSIBLE.BUILTIN.PING    (/usr/lib/python3.9/site-packages/ansible/modules/ping.py)

        A trivial test module, this module always returns `pong' on
        successful contact. It does not make sense in playbooks, but it
        is useful from `/usr/bin/ansible' to verify the ability to
        login and that a usable Python is configured. This is NOT ICMP
        ping, this is just a trivial test module that requires Python
        on 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: historical

OPTIONS (= is mandatory):

- data
        Data to return for the `ping' return value.
        If this parameter is set to `crash', the module will cause an
        exception.
        default: pong
        type: str


ATTRIBUTES:

        check_mode:
          description: Can run in check_mode and return changed status prediction without
            modifying target
          support: full
        diff_mode:
......
......
......
  • 列出所有模块信息
[root@server ~]# ansible-doc -l | wc -l   #有多少模块
7736
  • 列出yum模块
[root@server ~]# ansible-doc -l | grep yum
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 ...
  • 常用模块

在这里插入图片描述

示例1

  • 通过ansiblenode1node2下载安装httpd
[root@node1 ~]# yum list httpd
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:43:02 前,执行于 2024年07月06日 星期六 11时32分13秒。
可安装的软件包
httpd.x86_64                               2.4.57-8.el9                               app
[root@node2 ~]# yum list httpd
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:47:15 前,执行于 2024年07月06日 星期六 11时28分19秒。
可安装的软件包
httpd.x86_64                               2.4.57-8.el9                               app
[root@server ~]# vim /etc/ansible/hosts
node1.example.com
node2.example.com


[root@server ~]# ansible all -m ping   #首先测试网络连通性
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 ~]# ansible all -m yum -a "name=httpd state=installed"   #下发yum命令
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 ~]# yum list httpd
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:48:50 前,执行于 2024年07月06日 星期六 11时32分13秒。
已安装的软件包
httpd.x86_64                              2.4.57-8.el9                               @app
[root@node2 ~]# yum list httpd
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:52:53 前,执行于 2024年07月06日 星期六 11时28分19秒。
已安装的软件包
httpd.x86_64                              2.4.57-8.el9                               @app

示例2

  • server端操控node1node2
  • 不加-m参数默认使用command模块
[root@server ~]# ansible all -a 'df -h'
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 ~]# ansible node2.example.com -a 'ls /root'
node2.example.com | CHANGED | rc=0 >>
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
[root@server ~]# ansible node1.example.com -a 'cat /etc/os-release'
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 ~]# ansible node1.example.com -a 'hostname'
node1.example.com | CHANGED | rc=0 >>
node1.example.com

相关推荐

  1. ansible——Ansible ad hoc命令

    2024-07-11 22:20:05       20 阅读
  2. docker简介运行命令

    2024-07-11 22:20:05       37 阅读
  3. adb简单使用命令

    2024-07-11 22:20:05       22 阅读
  4. Openharmony hdcadb指令对应

    2024-07-11 22:20:05       59 阅读
  5. 【Muduo】网络库框架模型模块简介

    2024-07-11 22:20:05       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 22:20:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 22:20:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 22:20:05       58 阅读
  4. Python语言-面向对象

    2024-07-11 22:20:05       69 阅读

热门阅读

  1. SQL 存储过程

    2024-07-11 22:20:05       25 阅读
  2. 大数据面试题之数据湖

    2024-07-11 22:20:05       21 阅读
  3. MySQL常用命令

    2024-07-11 22:20:05       17 阅读
  4. 多态

    多态

    2024-07-11 22:20:05      22 阅读
  5. 面向本科生的智能品牌传播策略优化

    2024-07-11 22:20:05       28 阅读
  6. 数字化转型

    2024-07-11 22:20:05       15 阅读
  7. MySQL索引之索引类型

    2024-07-11 22:20:05       18 阅读
  8. 在 Linux 上安装 Miniconda

    2024-07-11 22:20:05       23 阅读
  9. 洛谷P7537-字典树+DFS

    2024-07-11 22:20:05       19 阅读
  10. SpringBoot使用@RestController处理GET和POST请求

    2024-07-11 22:20:05       19 阅读
  11. python的内置函数和模块(网安)

    2024-07-11 22:20:05       24 阅读