Ansible 提供了一个命令行工具,在官方文档中给命令行起了一个名字叫 Ad-Hoc Commands
命令的格式:
ansible <host-pattern> [options]
常用命令
1、查看远程主机的全部系统信息
[root@jump ~]# ansible all -m setup
2、在远程主机上,默认以当前bash的同名用户,在远程主机上执行“echo hello”
[root@jump ~]# ansible all -a "/bin/echo hello"
3、复制文件/usr/local/test.txt到远程主机的/usr/local/test
[root@jump ~]# ansible all -m copy -a "src=/usr/local/test.txt dest=/usr/local/test.txt"
4、在远程主机上安装yum包
[root@jump ~]# ansible all -m yum -a "name=acme state=present"
5、新建用户foo
[root@jump ~]# ansible all -m user -a 'name=foo_test shell=/bin/bash home=/home/foo_test/ state=present'
生成加密密码
[root@jump ~]# python -c 'import crypt; print (crypt.crypt("123456","foo_test"))'
更新密码
[root@jump ~]# ansible all -m user -a 'name=foo shell=/bin/bash password=foN3DNEv6eBlE update_password=always'
6、下载Git仓库
[root@jump ~]# ansible all -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
7、启动服务
[root@jump ~]# ansible all -m service -a "name=httpd state=started"
8、并行执行
启动10个并行执行重启
[root@jump ~]# ansible all -a ” / shin/reboot”-f 10