执行shell命令: ansible atlanta -a "/sbin/reboot" -f 10 这个命令中,atlanta是一个组,这个组里面有很多服务器,"/sbin/reboot"命令会在atlanta组下的所有机器上执行。这里ssh-agent会fork出10个子进程(bash)以并行的方式执行reboot命令。如前所说"每次重启10个"即是以这种方式实现。 指定用户执行shell命令: ansible atlanta -a "/usr/bin/foo" -u username 使用sudo: ansible atlanta -a "/usr/bin/foo" -u username --sudo [--ask-sudo-pass]
文件管理: 创建文件夹:ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory" 拷贝文件:ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
软件包管理: 确认一个软件包已经安装,但不去升级它:ansible webservers -m yum -a "name=acme state=present" 确认一个软件包的安装版本:ansible webservers -m yum -a "name=acme-1.5 state=present" 确认一个软件包还没有安装:ansible webservers -m yum -a "name=acme state=absent"
其他模块 git模块:git相关 service模块:系统服务相关 setup模块:系统环境相关
如何执行一个耗时的任务
1 2 3 4 5 6 7
ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff" -B 1800 表示最多运行60分钟,-P 0表示不获取状态
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" -B 1800表示最多运行30分钟,-P 60表示每隔一分钟获取一次状态
ansible web1.example.com -m async_status -a "jid=488359678239.2844"
production # inventory file for production servers(生产服务器的inventory文件) staging # inventory file for staging environment(临时环境的inventory文件)
group_vars/ group1.yml # here we assign variables to particular groups(将变量分配给特定的groups) group2.yml host_vars/ hostname1.yml # here we assign variables to particular systems(将变量分配给特定systems) hostname2.yml
library/ # if any custom modules, put them here (可选)(放置自定义modules) module_utils/ # if any custom module_utils to support modules, put them here (可选)(放置支持modules的自定义module_utils) filter_plugins/ # if any custom filter plugins, put them here (可选)(放置自定义filter plugins)
site.yml # master playbook(playbook入口) webservers.yml # playbook for webserver tier(webserver层playbook) dbservers.yml # playbook for dbserver tier(dbserver层playbook)
roles/ common/ # this hierarchy represents a "role"(这个层次代表role) tasks/ # main.yml # <-- tasks file can include smaller files if warranted(tasks文件) handlers/ # main.yml # <-- handlers file(处理程序文件) templates/ # <-- files for use with the template resource(用于模板资源的文件) ntp.conf.j2 # <------- templates end in .j2(模板以.j2结尾) files/ # bar.txt # <-- files for use with the copy resource(用于复制资源的文件) foo.sh # <-- script files for use with the script resource(用于脚本资源的脚本文件) vars/ # main.yml # <-- variables associated with this role(与此role关联的变量) defaults/ # main.yml # <-- default lower priority variables for this role(此role的默认优先级较低的变量) meta/ # main.yml # <-- role dependencies(role依赖) library/ # roles can also include custom modules(role还可以包括自定义modules) module_utils/ # roles can also include custom module_utils(role还可以包括自定义module_utils) lookup_plugins/ # or other types of plugins, like lookup in this case(或其他类型的plugins,例如lookup)
webtier/ # same kind of structure as "common" was above, done for the webtier role(与上面的"common"相同的结构,为webtier role定义) monitoring/ # "" fooapp/ # ""