最近问了个朋友关于代码发布的问题,他们测试环境是用webhook,线上用的则是ansible。html
那ansible是什么呢?在它的github主页介绍有段话node
Ansible is a radically simple IT automation system. It handles configuration-management, application deployment, cloud provisioning, ad-hoc task-execution, and multinode orchestration - including trivializing things like zero downtime rolling updates with load balancers.
翻译下来就是:python
ansible是一个很简单的IT自动化系统。它能够用来进行配置管理,应用部署,云资源分配,执行ad-hoc任务,还有多节点编排 - 包括琐碎的事情,如零停机更新与负载均衡器。git
sudo apt-get install ansible
安装后的配置文件在/etc/ansible/
目录,ansible.cfg为ansible配置文件,hosts为远程主机配置文件github
若还未安装pip(安装和管理python包的工具),可执行下列命令安装web
sudo easy_install pip
而后安装pipshell
sudo pip install ansible
ansible中的临时命令的执行是经过Ad-Hoc来完成,可以快速执行,并且不须要保存执行的命令,例如:ubuntu
ansible -i ~/hosts all -m command -a 'who' -u root ansible web -u Ponny -m script -a '/home/vagrant/my.sh' //脚本是主机上的脚本
主要参数以下:
-u username 指定ssh链接的用户名,即执行后面命令的用户
-i inventory_file 指定所使用的inventory文件的位置,默认为/etc/ansible/hosts
-m module 指定使用的模块,默认为command
-f 10 指定并发数,并发量大的时候,提升该值
--sudo [-k] 当须要root权限执行的化,-k参数用来输入root密码
-a 使用模块的参数安全
在github上有playbook的简单示例,我本身编写了个简单的my.yml以下:并发
--- - hosts: web remote_user: Ponny tasks: - name: change dir command: cd /Users/Ponny/localhost - name: test connection file: path=my.test state=touch owner=Ponny group=staff mode=0777
原本想的作法是先change目录,再建立文件,但彷佛并不成功,由于task不能更改work目录
运行剧本:
ansible-playbook -i /etc/ansible/hosts my.yml
If you want to run a command through the shell (say you are using '<', '>','|', etc), you actually want the [shell] module instead. The [command] module is much more secure as it's not affected by the user's environment. 'creates', 'removes', and 'chdir' can be specified after the command. For instance, if you only want to run a command if a certain file does not exist, use this.
就是说若是你用的命令里有 '<', '>', '|' 等这些符号,执行是不成功的,最好用shell模块来代替,