并发与shellgit
# bruce用户身份,-m指定模块名称,默认模块名command,all全部目标主机,也能够指定组名或者主机名 ansible all -m ping -u bruce # bruce用户身份,sudoing到root用户 ansible all -m ping -u bruce --sudo # bruce用户身份,sudoing到batman用户,-U(--sudo-user)指定sudo用户,-K(--ask-sudo-pass)指定sudo用户密码 ansible all -m ping -u bruce --sudo -U batman # -f指定并发数, command模块不支持变量 ansible atlanta -m command -a '/sbin/reboot' -f 10 # -k指定用户密码,-a模块参数,shell模块支持变量,命令参数必须使用双引号 ansible all -u root -k -m shell -a "/bin/echo $i"
文件传输web
# copy模块能够拷贝文件 ansible atlanta -m copy -a 'src=/etc/hosts dest=/tmp/hosts' # file模块能够改变文件属性,创建删除目录文件 ansible webservers -m file -a 'dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan' ansible webservers -m file -a 'dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory' ansible webservers -m file -a 'dest=/path/to/c state=absent'
包管理(yum模块)shell
ansible webservers -m yum -a 'name=acme state=present' # 确保acme包被安装,可是不升级 ansible webservers -m yum -a 'name=acme-1.5 state=present' # 指定acme包版本号 ansible webservers -m yum -a 'name=acme state=latest' # acme包升级到最新版本 ansible webservers -m yum -a 'name=acme state=absent' # 确保acme包不被安装
用户&组(user&group模块)bash
ansible all -m user -a 'name=foo password=<crypted password here>' # 创建用户 ansible all -m user -a 'name=foo state=absent' # 删除用户或者组
版本控制工具(git模块)并发
ansible webservers -m git -a 'repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD'
服务管理(service模块)app
ansible webservers -m service -a 'name=httpd state=started' #启动 ansible webservers -m service -a 'name=httpd state=restarted' #重启 ansible webservers -m service -a 'name=httpd state=stopped' #中止
有时限的后台操做async
# 后台运行long_running_operation命令,时间为1小时(3600s),系统会给执行同一任务主机一个jid ansible all -B 3600 -a '/usr/bin/long_running_operation --do-stuff' # 轮询任务的执行状态 ansible all -m async_status -a 'jid=123456789' # 后台运行时间1800秒,每60分钟轮询检查一次 ansible all -B 1800 -P 60 -a '/usr/bin/long_running_operation --do-stuff'
系统facts(setup模块)工具
# 获取目标主机信息 ansible all -m setup