Ansible

2017-02-22  本文已影响0人  Uchiha_Ponny

最近问了个朋友关于代码发布的问题,他们测试环境是用webhook,线上用的则是ansible。

介绍

那ansible是什么呢?在它的github主页介绍有段话

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.

翻译下来就是:

ansible是一个很简单的IT自动化系统。它可以用来进行配置管理,应用部署,云资源分配,执行ad-hoc任务,还有多节点编排 - 包括琐碎的事情,如零停机更新与负载均衡器。

安装

sudo apt-get install ansible

安装后的配置文件在/etc/ansible/目录,ansible.cfg为ansible配置文件,hosts为远程主机配置文件

sudo easy_install pip

然后安装pip

sudo pip install ansible

特点

命令介绍

ansible中的临时命令的执行是通过Ad-Hoc来完成,能够快速执行,而且不需要保存执行的命令,例如:

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 使用模块的参数

playbook

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模块来代替,

上一篇 下一篇

猜你喜欢

热点阅读