ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

三、05 ansible基础命令ansible 常用命令

2026/9/13 1:08:54 拓冰建站 浏览量
三、05 ansible基础命令ansible 常用命令

这里写目录标题

    • 命令介绍
    • ansible 的基础语法
      • 实力 使用一个copy 模块
    • Ansible 模块功能查看工具
      • ansible-doc的全部用法:
      • mysql 实例

命令介绍

/usr/bin/ansible  (常用) Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行

/usr/bin/ansible-playbook  (常用) Ansible 定制自动化的任务集编排工具

/usr/bin/ansible-doc   Ansible 模块功能查看工具

/usr/bin/ansible-galaxy  下载/上传优秀代码或Roles模块 的官网平台,基于网络的

/usr/bin/ansible-pull  Ansible远程执行命令的工具,拉取配置而非推送配置
(使用较少,海量机器时使用,对运维的架构能力要求较高)

/usr/bin/ansible-vault   Ansible 文件加密工具

/usr/bin/ansible-console  Ansible基于Linux Console界面可与用户交互的命令执行工具

ansible 的基础语法

ansible <host-pattern> [-f forks] [-m module_name] [-a args]
ansible 主机清单中的主机组 -f 开启线程数   -m 模块    -a 模块需要的参数

实力 使用一个copy 模块

[root@server ~]$ ansible web -m copy -a 'src=~/hello dest=/data/hello' 
# web 是主机组
# copy是复制模块
# src 是需要拷贝的文件
# dest 是需要要拷贝到什么位置192.168.37.122 | SUCCESS => {"changed": true, "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", "dest": "/data/hello", "gid": 0, "group": "root", "md5sum": "6f5902ac237024bdd0c176cb93063dc4", "mode": "0644", "owner": "root", "size": 12, "src": "/root/.ansible/tmp/ansible-tmp-1512437093.55-228281064292921/source", "state": "file", "uid": 0
}
192.168.37.133 | SUCCESS => {"changed": true, "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", "dest": "/data/hello", "gid": 0, "group": "root", "md5sum": "6f5902ac237024bdd0c176cb93063dc4", "mode": "0644", "owner": "root", "size": 12, "src": "/root/.ansible/tmp/ansible-tmp-1512437093.74-44694985235189/source", "state": "file", "uid": 0
}

Ansible 模块功能查看工具

ansible-doc -l              #获取全部模块的信息
ansible-doc -s MOD_NAME     #获取指定模块的使用帮助

ansible-doc的全部用法:

[root@server ~]# ansible-doc

返回内容

Usage: ansible-doc [options] [module...]Options:-h, --help            show this help message and exit  # 显示命令参数API文档-l, --list            List available modules  #列出可用的模块-M MODULE_PATH, --module-path=MODULE_PATH  #指定模块的路径specify path(s) to module library (default=None)-s, --snippet         Show playbook snippet for specified module(s)  #显示playbook制定模块的用法-v, --verbose         verbose mode (-vvv for more, -vvvv to enable  # 显示ansible-doc的版本号查看模块列表:connection debugging)--version             show program's version number and exit	

mysql 实例

[root@server ~]# ansible-doc -l |grep mysql

mysql_db Add or remove MySQL databases from a remote…
mysql_replication Manage MySQL replication
mysql_user Adds or removes a user from a MySQL databas…
mysql_variables Manage MySQL global variables
[root@server ~]# ansible-doc -s mysql_user