安装自动化工具ansible

最近问了个朋友关于代码发布的问题,他们测试环境是用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

安装

  • ubuntu
sudo apt-get install ansible

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

  • pip安装

若还未安装pip(安装和管理python包的工具),可执行下列命令安装web

sudo easy_install pip

而后安装pipshell

sudo pip install ansible

特色

  • 使用python编写
  • ansible无需客户端,直接经过ssh来进行链接,这也意味着它比较慢,这也让ansible不须要在远程主机上启动守护进程,并且ssh数据传输是通过加密的,主机不容易被攻破,更安全

命令介绍

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

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

注意点

  • command模块有个须要注意的地方
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模块来代替,

相关文章
相关标签/搜索