涛子 - 简单就是美

成单纯魁增,永继振国兴,克复宗清政,广开家必升

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

并发与shell

# 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"

文件传输

# 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模块)

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模块)

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模块)

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' #停止

有时限的后台操作

# 后台运行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
posted on 2014-12-30 17:30  北京涛子  阅读(937)  评论(0编辑  收藏  举报