Ansible 学习笔记(二)使用命令管理主机
前言
Ansible 提供了一个命令行工具,在官方文档中给命令行起了一个名字叫 Ad-Hoc Commands
Ansible 服务的强大之处在于只需要一条命令,便可以操控成千上万台的主机节点,而 ansible 命令便是最得力的工具之一。 Ansible 服务实际上只是一个框架,能够完成工作的是模块化功能代码,Ansible 的常用模块大致有 20 多个。
命令格式
命令+主机+模块和选项
ansible [host-pattern] [options]
Ansible 常用模块
什么是 Ansible 模块
模块就是 Ansible 的命令,调用模块可以跟不同的参数,每个模块的参数由模块自定义。
使用模块
ansible [host-pattern] -m [module] -a "[parameter]"
-
-m 后面接调用模块的名字
-
-a 后面接调用的模块参数
示例:检查所有受控主机能否 ping 通百度
ansible all -m ping -a "data=baidu.com"
其他常用参数
- -k 手动输入 ssh 密码
- -i 指定主机清单
- -M 指定要使用的模块路径
- -S 使用 su 命令
- -T 设置 SSH 协议连接超时时间
- --version 查看版本信息
- -h 查看帮助信息
常用模块
调试和测试类的模块
- ping:ping 一下远程主机,如果连接成功,那么返回 pong
- setup:收集受管节点主机上的系统及变量信息
- debug:用于调试的模块,只是简单打印一些消息,类似 Linux 系统的
echo
命令
文件类的模块
- copy:从本地复制文件到远程节点
- template:从本地复制文件到远程节点,并进行变量替换
- file:设置文件属性
- get_url:从网络中下载文件
Linux 上的常用操作
- user:管理用户账户
- yum:Redhat 系列的包管理器
- yum_repository:管理主机的软件仓库配置文件
- service:服务管理
- firewalld:管理防火墙中的服务和端口
- cron:添加、修改及删除计划任务
执行 shell 命令
- shell:在远程节点上执行 shell 命令,支持特殊字符
- command:在远程节点上执行 shell 命令
磁盘管理相关
- lvg: 管理主机的物理卷及卷组设备
- lvol:管理主机的逻辑卷设备
- mount: 挂载硬盘设备文件
- filesystem:格式化硬盘设备文件
查看其他模块
使用如下命令即可列出所有支持的模块
ansible-doc -l
如果需要查看某个模块怎么使用,可以使用
ansible-doc [模块名]
命令的基础使用
检查安装环境
检查是否可以访问所有的受控主机
[root@control ~]# ansible all -m ping
node3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
执行简单的命令
在所有的远程主机上输出 "Hello World"
[root@control ~]# ansible all -a '/bin/echo "Hello World"'
node3 | CHANGED | rc=0 >>
Hello World
node2 | CHANGED | rc=0 >>
Hello World
node1 | CHANGED | rc=0 >>
Hello World
复制文件
将控制节点的 /etc/hosts 文件复制到所有的远程主机中
[root@control ~]# ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts"
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "89b7155f4052ffcce53d47281b0bbc5e9226f673",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "19bc507951e4e6450363d39d6340f67e",
"mode": "0644",
"owner": "root",
"size": 212,
"src": "/root/.ansible/tmp/ansible-tmp-1661371441.3717391-22984-257257430804650/source",
"state": "file",
"uid": 0
}
···剩余输出省略···
安装软件
为 [web]
组内的主机安装 nginx 软件
[root@control ~]# ansible web -m yum -a "name=nginx state=present"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: nginx-1:1.18.0-5.oe1.x86_64",
"Installed: nginx-all-modules-1:1.18.0-5.oe1.noarch",
"Installed: libwebp-1.1.0-3.oe1.x86_64",
"Installed: nginx-filesystem-1:1.18.0-5.oe1.noarch",
"Installed: nginx-mod-http-image-filter-1:1.18.0-5.oe1.x86_64",
"Installed: nginx-mod-http-perl-1:1.18.0-5.oe1.x86_64",
"Installed: nginx-mod-http-xslt-filter-1:1.18.0-5.oe1.x86_64",
"Installed: nginx-mod-mail-1:1.18.0-5.oe1.x86_64",
"Installed: nginx-mod-stream-1:1.18.0-5.oe1.x86_64",
"Installed: gperftools-libs-2.8.1-2.oe1.x86_64",
"Installed: gd-2.3.0-3.oe1.x86_64"
]
}
···剩余输出省略···
启动软件
将 [web]
组内的主机上的 nginx 服务启动并设置为开机自启
[root@control ~]# ansible web -m service -a "name=nginx state=started enabled=yes"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "nginx",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
···剩余输出省略···
查看受控节点的信息
查看受控节点的详细信息
[root@control ~]# ansible all -m setup
node1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.8.11",
"192.168.122.1"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe26:28ac"
],
"ansible_apparmor": {
"status": "disabled"
},
···剩余输出省略···
查看受控节点的主机名
[root@control ~]# ansible all -m setup -a 'filter="*hostname*"'
node1 | SUCCESS => {
"ansible_facts": {
"ansible_hostname": "node1",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
···剩余输出省略···
查看受控节点的默认 IP 地址
[root@control ~]# ansible all -m setup -a 'filter="*ansible_all_ipv4_addresses*"'
node2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.8.12",
"192.168.122.1"
],
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
···剩余输出省略···
最后
Ansible 命令的基础操作就记录到这里,后续有一些更好玩的操作再补充。
© 版权声明
文章:Ansible 学习笔记(二)使用命令管理主机
作者:Guangran
链接:https://www.rsecc.cn/767.html
声明:如无特别说明本文即为原创文章仅代表个人观点,版权归《广然笔记》所有,未经授权、请勿转载。
文章:Ansible 学习笔记(二)使用命令管理主机
作者:Guangran
链接:https://www.rsecc.cn/767.html
声明:如无特别说明本文即为原创文章仅代表个人观点,版权归《广然笔记》所有,未经授权、请勿转载。
THE END