我阅读了Ansible的文档并记录了一些笔记
我之前阅读了一些有关Ansible的博客和官方文档,想尝试一下使用它,所以做了一些记录。
以下是我主要参考的链接。
点击链接后,对于”Ansible到底是什么?”这样的问题,我有了更清楚的了解。
-
- Ansibleの公式ドキュメント
http://docs.ansible.com/index.html
Playbookのサンプル
https://github.com/ansible/ansible-examples
Ansibleの概要を知るのにとてもよかった。
http://apatheia.info/blog/2013/04/06/about-ansible/
构成
与Chef和Puppet不同,这里是通过Ansible连接到目标服务器。
目标服务器不需要任何特定的代理或者软件。
安装
大部分都可以通过包管理等方法轻松完成,非常简单。
通过Pip
$ sudo easy_install pip
$ sudo pip install ansible
经由百胜中国
$ sudo rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
$ sudo yum install ansible
通过Apt
$ sudo apt-add-repository ppa:rquillo/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
通过 Homebrew
$ brew update
$ brew install ansible
已安装的版本
$ ansible --version
ansible 1.6.2
基本格式
在ansible命令后可以使用-m选项指定模块,也可以使用-a选项直接指定命令。
$ ansible 対象 -m モジュール
$ ansible 対象 -a 'コマンド'
举例说明执行基本步骤
ping模块可用于对目标进行检查等操作。
$ ansible all -m ping
target01.okochang.com | success >> {
"changed": false,
"ping": "pong"
}
$ ansible all -a "uname -r"
target01.okochang.com | success | rc=0 >>
3.10.35-43.137.amzn1.x86_64
指定目标主机的方式
设置文件
以下可根据您所选择的任一种(中文)表达方式。
-
- /etc/ansible/hosts
- export ANSIBLE_HOSTS=”your config path”
设定文件中的书写方式
基本上只需写主机名就可以了,但可以有各种不同的写法。
# グループ化
[webservers]
www01.okochang.com
www02.okochang.com
# 接続先のポートをデフォルトから変える
db01.okochang.com:22222
# IPアドレスを指定する
local.okochang.com ansible_ssh_port=22 ansible_ssh_host=192.168.0.10
# 範囲指定とかも出来る
app-[01:10].okochang.com
app-[a:d].okochang.com
# 対象毎にコネクションのタイプも指定出来る
localhost ansible_connection=local
other1.okochang.com ansible_connection=ssh ansible_ssh_user=ec2-user
# 変数を指定したりも出来る(ホスト毎)
host1 http_port=80 maxRequestPerChild=101
host2 http_port=8080 maxRequestPerChild=102
# グループ毎に変数を指定する
[foo-group]
host1
host2
[foo-group:vars]
ntp_server=ntp.okochang.com
proxy_servers=proxy.okochang.com
执行命令时
使用通配符
$ ansible webserver* -m ping
可以指定多个选项
$ ansible webservers:dbservers -m ping
可以从群组中排除特定的主机。
$ ansible webservers:\!webserver01 -m ping
ansible执行命令时的有用选项
我希望使用SSH密码认证的无理由。
$ ansible all -m ping --ask-pass
我想指定SSH的私钥。
$ ansible all -m ping --private-key="~/.ssh/yourkey.pem"
我想要指定SSH连接的用户(默认为当前用户)。
$ ansible all -m ping -u ec2-user
我想用sudo运行
$ ansible all -m ping -u ec2-user --sudo
我想要指定执行 sudo 命令的用户。
$ ansible all -m ping -u ec2-user --sudo --sudo-user okochang
在执行sudo命令时需要输入密码的情况下
$ ansible all -m ping -u ec2-user --sudo --ask-sudo-pass
如果处理需要花费时间,可以指定超时时间。
$ ansible webserver01 -B 180 -a "sleep 120"
设置文件
-
- /etc/ansible/ansible.cfg
- ~/.ansible.cfg
在这些中的任何一个都可以进行设置,并且多数都是指定默认行为的印象。
# 接続するときにホスト公開鍵の検証をするかしないか
$ host_key_checking = False
# リモード接続するときに平行して生成するプロセス数
forks=5
# ホストを指定する方法
hostfile=/etc/ansible/hosts
# ログファイルのパス
log_path=/var/log/ansible.log
# 色の設定
nocolor=0
# デフォルトで使用する接続先のポート
remote_port=22
对AWS的支持
使用AWS时,由于连接目标的IP地址经常改变或者实例数量经常变化,需要进行相应的处理。
引入编辑
在Github上有Python脚本和ini文件可供分发。
为了使用它们,需要安装boto库,然后授予执行权限,就可以简单好用地使用了。
$ sudo pip install boto
$ cd .ansible
$ curl -LO https://raw.github.com/ansible/ansible/devel/plugins/inventory/ec2.py
$ curl -LO https://raw.github.com/ansible/ansible/devel/plugins/inventory/ec2.ini
$ vi .bash_profile
export AWS_ACCESS_KEY_ID='YOURACCESSKEYID'
export AWS_SECRET_ACCESS_KEY='YOURSECRETACCESSKEY'
export EC2_INI_PATH=~/.ansible/ec2.ini
$ chmod +x .ansible/ec2.py
$ .ansible/ec2.py --list
使用方法
在所有地区的EC2实例上执行
$ ansible ec2 -i ~/.ansible/ec2.py -u ec2-user -m ping
只在特定区域执行
$ ansible ap-northeast-1 -i .ansible/ec2.py -u ec2-user -m ping
只在Arizona州内执行
$ ansible ap-northeast-1a -i .ansible/ec2.py -u ec2-user -m ping
仅限制安全组进行执行
$ ansible security_group_okochang-group -i .ansible/ec2.py -u ec2-user -m ping
只执行指定的标签(如果Name标签是web01的话)。
$ ansible tag_Name_web01 -i .ansible/ec2.py -u ec2-user -m ping
模组
Ansible中被处理的小执行单位,类似于Chef中的Resources。
使用方法备注。
外壳
$ ansible webservers -m shell -a 'ping -c 3 google.co.jp'
复制
$ ansible webservers -m copy -a "src=~/test.txt dest=/tmp/test.txt"
档案 ‘àn)
$ ansible webservers -m file -a "dest=/tmp/test.txt mode=600 owner=foo group=var"
好吃
$ ansible webservers -m yum -a "name=httpd state=installed" -u ec2-user --sudo
$ ansible webservers -m yum -a "name=postfix state=removed" -u ec2-user --sudo
服务
$ ansible webservers -m service -a "name=httpd state=started" -u ec2-user --sudo
$ ansible webservers -m service -a "name=httpd state=restarted" -u ec2-user --sudo
$ ansible webservers -m service -a "name=httpd state=stopped" -u ec2-user --sudo
$ ansible webservers -m git -a "repo=git://github.com/serverworks/aws-spec.git dest=/opt/aws-spec version=HEAD"
Git 模块
$ ansible webservers -m git -a "repo=git://github.com/serverworks/aws-spec.git dest=/opt/aws-spec version=HEAD"
游戏手册
使用模块将要在目标服务器上执行的处理程序进行组合编写。
类似于Chef的菜谱,书写形式为yml格式。
使用ping模块的playbook
# test.con.yml
- hosts: all
remote_user: ec2-user
tasks:
- name: test connection
ping:
执行
$ ansible-playbook .ansible/playbooks/test_con.yml
使用 shell 模块的 playbook
# shell_result.yml
- hosts: ec2
remote_user: ec2-user
tasks:
- name: run command and ignore the result
shell: /bin/echo foo
ignore_errors: True
还可以与先前针对AWS的脚本一起使用。
$ ansible-playbook -i .ansible/ec2.py -u ec2-user .ansible/playbooks/shell_result.yml
用于Web服务器的Playbook看起来会是这样的。
# webserver.yml
- hosts: tag_Name_target01
vars:
http_port: 80
max_clients: 200
remote_user: ec2-user
sudo: yes
sudo_user: root
tasks:
- name: ensure apache is installed
yum: pkg=httpd state=installed
- name: write the apache config file
template: src=~/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: create virtual host file for {{ vhost }}
template: src=~/vhost.conf.j2 dest=/etc/httpd/conf.d/{{ vhost }}.conf
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
在选项中可以指定变量。
$ ansible-playbook -i .ansible/ec2.py -u ec2-user .ansible/playbooks/webserver.yml -e vhost=foo
可以包含文件。
# foo_var.yml
- name: echo foo
command: /bin/echo foo
- name: echo var
command: /bin/echo var
# hoge.yml
- hosts: ec2
remote_user: ec2-user
tasks:
- include: foo_var.yml
执行
$ ansible-playbook -i .ansible/ec2.py -u ec2-user .ansible/playbooks/hoge.yml
角色手冊
可以方便地重复使用Playbook的机制。
当按照预定的结构进行配置时,可以自动实现重复使用。
以下是common和webservers角色的目录结构。
https://github.com/okochang/ansible-sample-project
$ tree
.
├── roles
│ ├── common
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ └── clock.j2
│ │ └── vars
│ └── webservers
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ ├── install_httpd.yml
│ │ └── main.yml
│ ├── templates
│ │ └── vhosts.conf.j2
│ └── vars
└── site.yml
如果要执行的话,大致就是这样的。
$ ansible-playbook -i ~/.ansible/ec2.py -u ec2-user ./site.yml