Ansible即席命令指南及示例
Ad-hoc命令在Ansible中用于即时执行任务,无需保存以供以后使用。本文章将讨论Ansible Ad-hoc命令。
在Ansible中有多个任务不需要为其编写单独的Ansible playbook;您只需运行ansible ad-hoc命令即可完成该任务。这些命令是在目标主机上执行单个任务的一行命令。这些命令位于/usr/bin/ansible
中。
什么是Ansible Ad-hoc Command?
Ansible ad-hoc命令是一种非常有用的工具,它允许您在一组服务器上执行单个任务,而无需创建完整的playbook。这些命令非常适合执行简单任务,例如检查服务状态或快速运行命令。但是,对于更复杂的任务(例如软件安装),最好使用playbook。
输入一行代码,其中包含相关信息,例如所需的模块名称和必须提供给模块的任何参数,以运行ad-hoc命令。
Ansible ad-hoc命令的语法如下:
ansible -m -a ""
这里:
- 是以逗号分隔的目标主机列表,
- 是要使用的Ansible模块的名称,和
- 是要传递给模块的任何参数。
例如,如果要检查一组服务器上的磁盘空间,可以使用带有shell模块的df -h命令,如下所示:
ansible web_servers -m shell -a "df -h"
shell模块将在web_servers组中的所有服务器上运行df -h命令。然后,您将能够在控制台上看到每个服务器的输出。这非常简单和高效!
为什么使用Ansible ad-hoc命令?
除了快速和易于使用外,ad-hoc命令非常适合简单的、一次性的任务,这些任务不需要太多的复杂性。此外,它们是在将模块或命令添加到playbook之前尝试新模块或命令的绝佳方式。这可以帮助您避免错误并节省时间。
如果您正在对服务器上的问题进行故障排除,可以使用它们来检查服务状态、查看日志文件或执行其他诊断任务。
此外,ad-hoc命令也非常灵活!您可以将它们用于各种任务,例如安装软件包、复制文件或同时在多个服务器上运行命令。
如果您想要使服务器管理更轻松高效,那么Ansible ad-hoc命令是最好的选择!
以下是您可以使用它们的一些方式:
- 重启服务器
- 管理文件
- 管理软件包
- 管理用户和组
- 管理服务
- 收集事实
- 模式和ad-hoc命令
通过Ansible Ad-hoc命令,可以轻松完成诸如ping所有主机以检查其运行状态、复制文件、重启服务器和安装软件包等任务。以下是您必须了解的基本Ansible Ad-hoc命令列表。
基本命令
下面的ad-hoc命令在清单文件中的所有主机上运行一个ping模块。这里的-m
是模块的选项。
root@yaoweibin:/home/yaoweibin# ansible all -m ping
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
下面的命令在清单文件'/etc/ansible/hosts'中的Client主机组上运行设置模块。
root@yaoweibin:/home/yaoweibin# ansible Client -m copy -a 'src=/path/to/source/file dest=/path/to/destination/file' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"checksum": "b6f7c9c4f97d5f4c81d3d6870dd9e3686bb1b3c8",
"dest": "/path/to/destination/file",
"gid": 0,
"group": "root",
"md5sum": "ff0b12b3d6e9a6ff6e6d3e2e3fbd83b0",
"mode": "0644",
"owner": "root",
"size": 1291,
"src": "/home/yaoweibin/.ansible/tmp/ansible-tmp-1626411356.83-194489083856566/source",
"state": "file",
"uid": 0
}
The command below is used to copy a directory from a source to a destination for a group of hosts (Client) defined in the inventory file.
root@yaoweibin:/home/yaoweibin# ansible Client -m synchronize -a 'src=/path/to/source/directory dest=/path/to/destination' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"dest": "/path/to/destination",
"mode": "0755",
"owner": "root",
"src": "/path/to/source/directory"
}
The command below is used to delete a file or directory for a group of hosts (Client) defined in the inventory file.
root@yaoweibin:/home/yaoweibin# ansible Client -m file -a 'path=/path/to/file_or_directory state=absent' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"path": "/path/to/file_or_directory",
"state": "absent"
}
The command below is used to create a directory for a group of hosts (Client) defined in the inventory file.
root@yaoweibin:/home/yaoweibin# ansible Client -m file -a 'path=/path/to/new_directory state=directory' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/path/to/new_directory",
"size": 4096,
"state": "directory",
"uid": 0
}
Package Management
The command below is used to install a package for a group of hosts (Client) defined in the inventory file.
root@yaoweibin:/home/yaoweibin# ansible Client -m apt -a 'name=package_name state=present' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"stderr": "",
"stderr_lines": [],
"stdout": "Reading package lists...nBuilding dependency tree...nReading state information...nThe following additional packages will be installed:n package_namenThe following NEW packages will be installed:n package_namen0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.nNeed to get 0 B/XXX MB of archives.nAfter this operation, XXX MB of additional disk space will be used.nSelecting previously unselected package package_name.n(Reading database ... n(Reading database ... 5%n(Reading database ... 10%n(Reading database ... 15%n(Reading database ... 20%n(Reading database ... 25%n(Reading database ... 30%n(Reading database ... 35%n(Reading database ... 40%n(Reading database ... 45%n(Reading database ... 50%n(Reading database ... 55%n(Reading database ... 60%n(Reading database ... 65%n(Reading database ... 70%n(Reading database ... 75%n(Reading database ... 80%n(Reading database ... 85%n(Reading database ... 90%n(Reading database ... 95%n(Reading database ... 100%n(Reading database ... 123456 files and directories currently installed.)nPreparing to unpack .../package_name.deb ...nUnpacking package_name (version) ...nSetting up package_name (version) ...n",
"stdout_lines": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"The following additional packages will be installed:",
" package_name",
"The following NEW packages will be installed:",
" package_name",
"0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.",
"Need to get 0 B/XXX MB of archives.",
"After this operation, XXX MB of additional disk space will be used.",
"Selecting previously unselected package package_name.",
"(Reading database ...",
"(Reading database ... 5%",
"(Reading database ... 10%",
"(Reading database ... 15%",
"(Reading database ... 20%",
"(Reading database ... 25%",
"(Reading database ... 30%",
"(Reading database ... 35%",
"(Reading database ... 40%",
"(Reading database ... 45%",
"(Reading database ... 50%",
"(Reading database ... 55%",
"(Reading database ... 60%",
"(Reading database ... 65%",
"(Reading database ... 70%",
"(Reading database ... 75%",
"(Reading database ... 80%",
"(Reading database ... 85%",
"(Reading database ... 90%",
"(Reading database ... 95%",
"(Reading database ... 100%",
"(Reading database ... 123456 files and directories currently installed.)",
"Preparing to unpack .../package_name.deb ...",
"Unpacking package_name (version) ...",
"Setting up package_name (version) ..."
]
}
The command below is used to uninstall a package for a group of hosts (Client) defined in the inventory file.
root@yaoweibin:/home/yaoweibin# ansible Client -m apt -a 'name=package_name state=absent' -u yaoweibin --become -K
BECOME password:
node1 | CHANGED => {
"changed": true,
"stderr": "",
"stderr_lines": [],
"stdout": "Reading package lists...nBuilding dependency tree...nReading state information...nThe following packages will be REMOVED:n package_namen0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.nAfter this operation, XXX MB disk space will be freed.n(Reading database ... n(Reading database ... 5%n(Reading database ... 10%n(Reading database ... 15%n(Reading database ... 20%n(Reading database ... 25%n(Reading database ... 30%n(Reading database ... 35%n(Reading database ... 40%n(Reading database ... 45%n(Reading database ... 50%n(Reading database ... 55%n(Reading database ... 60%n(Reading database ... 65%n(Reading database ... 70%n(Reading database ... 75%n(Reading database ... 80%n(Reading database ... 85%n(Reading database ... 90%n(Reading database ... 95%n(Reading database ... 100%n(Reading database ... 123456 files and directories currently installed.)nRemoving package_name (version) ...n",
"stdout_lines": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"The following packages will be REMOVED:",
" package_name",
"0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.",
"After this operation, XXX MB disk space will be freed.",
"(Reading database ...",
"(Reading database ... 5%",
"(Reading database ... 10%",
"(Reading database ... 15%",
"(Reading database ... 20%",
"(Reading database ... 25%",
"(Reading database ... 30%",
"(Reading database ... 35%",
"(Reading database ... 40%",
"(Reading database ... 45%",
"(Reading database ... 50%",
"(Reading database ... 55%",
"(Reading database ... 60%",
"(Reading database ... 65%",
"(Reading database ... 70%",
"(Reading database ... 75%",
"(Reading database ... 80%",
"(Reading database ... 85%",
"(Reading database ... 90%",
"(Reading database ... 95%",
"(Reading database ... 100%",
"(Reading database ... 123456 files and directories currently installed.)",
"Removing package_name (version) ..."
]
}
root@yaoweibin:/home/yaoweibin# ansible Client -m apt -a 'name=nginx state=absent purge=yes' --become
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "",
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "Reading package lists...nBuilding dependency tree...nReading state information...nThe following packages will be REMOVED:n nginx*n0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.nAfter this operation, 848 kB disk space will be freed.n(Reading database ... n(Reading database ... 5%n(Reading database ... 10%n(Reading database ... 15%n(Reading database ... 20%n(Reading database ... 25%n(Reading database ... 30%n(Reading database ... 35%n(Reading database ... 40%n(Reading database ... 45%n(Reading database ... 50%n(Reading database ... 55%n(Reading database ... 60%n(Reading database ... 65%n(Reading database ... 70%n(Reading database ... 75%n(Reading database ... 80%n(Reading database ... 85%n(Reading database ... 90%n(Reading database ... 95%n(Reading database ... 100%n(Reading database ... 132679 files and directories currently installed.)nRemoving nginx (1.14.0-0ubuntu1.5) ...nPurging configuration files for nginx (1.14.0-0ubuntu1.5) ...nProcessing triggers for man-db (2.8.3-2ubuntu0.1) ...nProcessing triggers for ufw (0.36-0ubuntu0.18.04.1) ...",
"stdout_lines": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"The following packages will be REMOVED:",
" nginx*",
"0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.",
"After this operation, 848 kB disk space will be freed.",
"(Reading database ... ",
"(Reading database ... 5%",
"(Reading database ... 10%",
"(Reading database ... 15%",
"(Reading database ... 20%",
"(Reading database ... 25%",
"(Reading database ... 30%",
"(Reading database ... 35%",
"(Reading database ... 40%",
"(Reading database ... 45%",
"(Reading database ... 50%",
"(Reading database ... 55%",
"(Reading database ... 60%",
"(Reading database ... 65%",
"(Reading database ... 70%",
"(Reading database ... 75%",
"(Reading database ... 80%",
"(Reading database ... 85%",
"(Reading database ... 90%",
"(Reading database ... 95%",
"(Reading database ... 100%",
"(Reading database ... 132679 files and directories currently installed.)",
"Removing nginx (1.14.0-0ubuntu1.5) ...",
"Purging configuration files for nginx (1.14.0-0ubuntu1.5) ...",
"Processing triggers for man-db (2.8.3-2ubuntu0.1) ...",
"Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ..."
]
}
管理服务
下面的ansible临时命令使用service模块在主机上启动nginx。状态值应该是started。
下面的代码在主机上运行service模块停止nginx服务。状态值变为stopped。
root@yaoweibin:/home/yaoweibin# ansible Client -m service -a ‘name=nginx state=stopped' –become
node1 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: true,
“name”: “nginx”,
“state”: “stopped”,
“status”: {
“ActiveEnterTimestamp”: “Sat 2019-07-06 08:28:02 EDT”,
“ActiveEnterTimestampMonotonic”: “31411371”,
“ActiveExitTimestampMonotonic”: “0”,
“ActiveState”: “active”,
“After”: “sysinit.target system.slice systemd-journald.socket basic.target network.target”,
“AllowIsolate”: “no”,
“AmbientCapabilities”: “”,
“AssertResult”: “yes”,
“AssertTimestamp”: “Sat 2019-07-06 08:27:59 EDT”,
“AssertTimestampMonotonic”: “27694868”,
“Before”: “multi-user.target shutdown.target”,
“BlockIOAccounting”: “no”,
“BlockIOWeight”: “[not set]”,
“CPUAccounting”: “no”,
“CPUQuotaPerSecUSec”: “infinity”,
“CanReload”: “yes”,
“CanStart”: “yes”,
“CanStop”: “yes”,
“CapabilityBoundingSet”: “cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend”,
“CollectMode”: “inactive”,
“ConditionResult”: “yes”,
“ConditionTimestamp”: “Sat 2019-07-06 08:27:59 EDT”,
“ConditionTimestampMonotonic”: “27694867”,
“ConfigurationDirectoryMode”: “0755”,
“Conflicts”: “shutdown.target”,
“ControlGroup”: “/system.slice/nginx.service”,
“ControlPID”: “0”,
“DefaultDependencies”: “yes”,
“Delegate”: “no”,
“Description”: “A high performance web server and a reverse proxy server”,
“DevicePolicy”: “auto”,
“Documentation”: “man:nginx(8)”,
“DynamicUser”: “no”,
}
}
检查系统
下面的ansible自适应命令运行一个shell模块来检查根分区上可用的磁盘。
root@yaoweibin:/home/yaoweibin# ansible Client -m shell -a 'df -h /dev/sda2' --become
node1 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 923M 113M 748M 14% /boot
下面的命令运行一个shell模块来检查主机上的可用内存(RAM)。
root@yaoweibin:/home/yaoweibin# ansible Client -m shell -a 'free -m' --become
node1 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 5101 854 2760 27 1487 3947
Swap: 8581 0 8581
此命令检查每个正在运行的服务器的正常运行时间。
root@yaoweibin:/home/yaoweibin# ansible Client -a "uptime"
node1 | CHANGED | rc=0 >>
11:31:17 up 1 day, 2:40, 2 users, load average: 0.23, 0.05, 0.02
收集事实
下面的ansible自适应命令将为您提供系统的所有自适应信息,包括系统中存在的所有变量。
root@yaoweibin:/home/yaoweibin# ansible all -m setup
node1 | SUCCESS => {
“ansible_facts”: {
“ansible_all_ipv4_addresses”: [
“172.17.0.1”,
“10.0.2.15”
],
“ansible_all_ipv6_addresses”: [
“fe80::763e:c0b4:14df:b273”
],
“ansible_apparmor”: {
“status”: “enabled”
},
“ansible_architecture”: “x86_64”,
“ansible_bios_date”: “12/01/2006”,
“ansible_bios_version”: “VirtualBox”,
“ansible_cmdline”: {
“BOOT_IMAGE”: “/vmlinuz-4.18.0-25-generic”,
“quiet”: true,
“ro”: true,
“root”: “UUID=5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83”,
“splash”: true
},
“ansible_date_time”: {
“date”: “2019-07-07”,
“day”: “07”,
“epoch”: “1562525628”,
“hour”: “14”,
“iso8601”: “2019-07-07T18:53:48Z”,
“iso8601_basic”: “20190707T145348850596”,
“iso8601_basic_short”: “20190707T145348”,
“iso8601_micro”: “2019-07-07T18:53:48.850697Z”,
“minute”: “53”,
“month”: “07”,
“second”: “48”,
“time”: “14:53:48”,
“tz”: “EDT”,
“tz_offset”: “-0400”,
“weekday”: “Sunday”,
“weekday_number”: “0”,
“weeknumber”: “26”,
“year”: “2019”
},
“ansible_default_ipv4”: {
“address”: “10.0.2.15”,
“alias”: “enp0s3”,
“broadcast”: “10.0.2.255”,
“gateway”: “10.0.2.2”,
“interface”: “enp0s3”,
“macaddress”: “08:00:27:68:64:9a”,
“mtu”: 1500,
“netmask”: “255.255.255.0”,
“network”: “10.0.2.0”,
“type”: “ether”
},
“ansible_default_ipv6”: {},
“ansible_device_links”: {
“ids”: {
“sda”: [
“ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5”
],
“sda1”: [
“ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part1”
],
“sda2”: [
“ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part2”
],
“sda3”: [
“ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part3”
],
“sda4”: [
“ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part4”
],
“sr0”: [
“ata-VBOX_CD-ROM_VB2-01700376”
]
},
“labels”: {
“sr0”: [
“VBox_GAs_6.0.2”
]
},
“masters”: {},
“uuids”: {
“sda1”: [
“5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83”
],
“sda2”: [
“b8b7f87b-c3bf-48ed-a44c-f9b3ce0afbe5”
],
“sda3”: [
“a6c77fa6-e292-4a0d-b21f-8804f1949bbd”
],
“sda4”: [
“8207f970-4d9a-47db-a5d5-f620e5b17b7b”
],
“sr0”: [
“2019-01-14-14-57-19-65”
]
}
},
“ansible_devices”: {
“loop0”: {
“holders”: [],
“host”: “”,
“links”: {
“ids”: [],
“labels”: [],
“masters”: [],
“uuids”: []
},
“model”: null,
“partitions”: {},
“removable”: “0”,
“rotational”: “1”,
“sas_address”: null,
“sas_device_handle”: null,
“scheduler_mode”: “none”,
“sectors”: “29704”,
“sectorsize”: “512”,
“size”: “14.50 MB”,
“support_discard”: “4096”,
“vendor”: null,
“virtual”: 1
},
“loop1”: {
“holders”: [],
“host”: “”,
“links”: {
“ids”: [],
“labels”: [],
“masters”: [],
“uuids”: []
},
“model”: null,
“partitions”: {},
“removable”: “0”,
“rotational”: “1”,
“sas_address”: null,
“sas_device_handle”: null,
“scheduler_mode”: “none”,
“sectors”: “0”,
“sectorsize”: “512”,
“size”: “0.00 Bytes”,
“support_discard”: “4096”,
“vendor”: null,
“virtual”: 1
},
}
所以,这就是关于Ansible Ad-hoc命令的全部内容。继续在您的ansible setup上尝试这些命令。使用这些命令运行Ansible模块,而无需编写任何Ansible playbook来执行这些任务。如果在运行这些命令时遇到任何问题,请留下评论。