Ansible Ad-Hoc命令集
Ad-Hoc
Ad-Hoc就是 “临时命令”, 从功能上讲 Ad-Hoc跟Ansible-playbook都差不多,Ansible提供了两种完成任务的方式: Ad-Hoc命令集与Ansible-playbook。
Ad-Hoc相当于linux的shell命令, Ansible-playbook相当于linux的shell scripts。
Ad-Hoc命令集
由 /usr/bin/ansible实现,其命令用法如下:
ansible <host-parttern> [options]
Ansible内置模块
ansible-doc --list 列出所有可用模块
ansible-doc module 查看模块说明
示例1: 安装软件包
root@ubuntu:~# ansible openstack -m apt -a ‘name=python-pip state=present’
示例2: 查看系统版本
root@ubuntu:~# ansible openstack -m command -a 'lsb_release -a' 192.168.20.182 | SUCCESS | rc=0 >> Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS Release: 16.04 Codename: xenialNo LSB modules are available. 192.168.20.180 | SUCCESS | rc=0 >> Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS Release: 16.04 Codename: xenialNo LSB modules are available.
示例3: 安装NTP服务
root@ubuntu:~# ansible openstack -m apt -a 'name=ntp state=present'
示例4: 启动NTP服务,并设置为开机启动
root@ubuntu:~# ansible openstack -m service -a "name=ntp state=started enabled=yes" 192.168.20.182 | SUCCESS => { "changed": false, "enabled": true, "name": "ntp", "state": "started" } 192.168.20.180 | SUCCESS => { "changed": false, "enabled": true, "name": "ntp", "state": "started" }