@nalan90
2017-10-20T14:50:47.000000Z
字数 8989
阅读 712
自动化运维
摘自:http://getansible.com
示例代码: https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced
配置文件
看一个完整的anbile配置文件例子,就能基本了解到ansible都能配置什么了:
https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
对ansible配置文件里面的关键字不能完整理解,还可以参考关键词解释列表:
http://docs.ansible.com/ansible/intro_configuration.html#explanation-of-values-by-section
ansible的默认配置文件是/etc/ansible/ansible.cfg。
其实ansible会按照下面的顺序查找配置文件,并使用第一个发现的配置文件。
配置示例
[ssh_connection]
pipelining=True
#ssh_args = -F ./ssh-bastion.conf -o ControlMaster=auto -o ControlPersist=30m
#control_path = ~/.ssh/ansible-%%r@%%h:%%p
[defaults]
host_key_checking=False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp
stdout_callback = skippy
library = ./library
callback_whitelist = profile_tasks
实验使用的配置
##ansible.cfg
work:ansible ys$ cat /etc/ansible/ansible.cfg
[defaults]
host_key_checking = False ##关闭ssh指纹识别
[ssh_connection]
ssh_args=-o ForwardAgent=yes ##开启ssh-agent forward
远程主机的分组
简单的分组[]内是组名
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com
分组usa的子组还可以是其它的组,
例如[usa:children]中还可以包含southeast子组,
[southeast:children]中还可以包含atlanta和releigh
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[usa:children]
southeast
northeast
southwest
northwest
实验环境分组
##HostInventory 配置文件
默认的文件是:/etc/ansible/hosts,可通过-i指定文件
work:ansible ys$ cat hosts
[hadoop]
172.16.1.163
172.16.1.162
[web]
172.16.1.121
172.16.1.122
172.16.1.123
远程主机的连接参数和变量
##指定Server的连接参数,其中包括连接方法,用户等。
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
所有可以指定的参数在文档中 http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters
##为一个组指定变量
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
示例命令
## 打印hello
work:ansible ys$ ansible web -a "/bin/echo hello" -u zhangshuang -i hosts
172.16.1.123 | SUCCESS | rc=0 >>
hello
172.16.1.122 | SUCCESS | rc=0 >>
hello
172.16.1.121 | SUCCESS | rc=0 >>
hello
--------------------------------------------------
## ping 所有主机
work:ansible ys$ ansible all -m ping -u zhangshuang -i hosts
172.16.1.162 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.123 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.122 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.163 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.121 | SUCCESS => {
"changed": false,
"ping": "pong"
}
--------------------------------------------------
## 文件拷贝
work:ansible ys$ ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts" -u zhangshuang -i hosts
172.16.1.123 | SUCCESS => {
"changed": true,
"checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
"dest": "/tmp/hosts",
"gid": 2006,
"group": "zhangshuang",
"md5sum": "080286ed718f4c63119b91820f434dd2",
"mode": "0664",
"owner": "zhangshuang",
"size": 861,
"src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404372.35-206978874040737/source",
"state": "file",
"uid": 2006
}
172.16.1.122 | SUCCESS => {
"changed": true,
"checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
"dest": "/tmp/hosts",
"gid": 2006,
"group": "zhangshuang",
"md5sum": "080286ed718f4c63119b91820f434dd2",
"mode": "0664",
"owner": "zhangshuang",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 861,
"src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404373.17-143062131252567/source",
"state": "file",
"uid": 2006
}
172.16.1.121 | SUCCESS => {
"changed": true,
"checksum": "f687508cb64fbe053d7c9e9ec72c4426dbb64903",
"dest": "/tmp/hosts",
"gid": 2003,
"group": "zhangshuang",
"md5sum": "080286ed718f4c63119b91820f434dd2",
"mode": "0664",
"owner": "zhangshuang",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 861,
"src": "/home/zhangshuang/.ansible/tmp/ansible-tmp-1508404373.24-123352246327910/source",
"state": "file",
"uid": 2003
}
--------------------------------------------------
##添加指定用户 password为加密后的密码
work:ansible ys$ ansible hadoop -m user -a "name=test1 password=fsfsdfxvcxv" -u zhangshuang -b -i hosts
172.16.1.163 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 2011,
"home": "/home/test1",
"name": "test1",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2011
}
172.16.1.162 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 2011,
"home": "/home/test1",
"name": "test1",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2011
}
--------------------------------------------------
## 更新代码
work:ansible ys$ ansible hadoop -m git -a "repo=git@git.sunfund.com:9douyu/9douyu.git dest=/home/zhangshuang/git/9douyu version=HEAD" -u zhangshuang -i hosts
172.16.1.162 | SUCCESS => {
"after": "d9e3c9f9ce24b61776cfd54401e090db8b5c9451",
"before": null,
"changed": true,
"warnings": []
}
172.16.1.163 | SUCCESS => {
"after": "d9e3c9f9ce24b61776cfd54401e090db8b5c9451",
"before": null,
"changed": true,
"warnings": []
}
--------------------------------------------------
## 安装nginx
work:ansible ys$ ansible hadoop -m yum -a "name=nginx state=present" -u zhangshuang -b -i hosts
172.16.1.163 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"nginx-1:1.10.2-2.el7.x86_64 providing nginx is already installed"
]
}
172.16.1.162 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"nginx-1:1.10.2-2.el7.x86_64 providing nginx is already installed"
]
}
--------------------------------------------------
##启动nginx服务
work:ansible ys$ ansible hadoop -m service -a "name=nginx state=started" -u zhangshuang -b -i hosts
[zhangshuang@master 9douyu]$ ps -ef | grep nginx
root 15880 15864 0 Sep13 pts/1 00:00:00 nginx: master process nginx -g daemon off;
101 15897 15880 0 Sep13 pts/1 00:00:00 nginx: worker process
root 16107 16102 0 Sep13 pts/2 00:00:00 tail -f /var/log/nginx/access.log /var/log/nginx/error.log
root 28035 1 0 17:17 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 28036 28035 0 17:17 ? 00:00:00 nginx: worker process
nginx 28037 28035 0 17:17 ? 00:00:00 nginx: worker process
zhangsh+ 28063 26475 0 17:18 pts/0 00:00:00 grep --color=auto nginx
--------------------------------------------------
##启动10个并行进行执行重起
$ansible all -a "/sbin/reboot" -f 10 -u zhangshuang -b -i hosts
##查看远程主机的全部系统信息!!!
work:ansible ys$ ansible hadoop -m setup -u zhangshuang -b -i hosts
ansible-playbook(安装apache)
deploy.yml的功能为hadoop组的主机部署apache, 其中包含以下部署步骤:
playbook deploy.yml包含下面几个关键字,每个关键字的含义:
具体示例如下:
##目录结构
work:ansible ys$ tree
.
├── deploy.retry
├── deploy.yml
├── hosts
└── templates
├── httpd.conf.j2
└── index.html.j2
--------------------------------------------------
##ansible-playbook文件
work:ansible ys$ cat deploy.yml
---
- hosts: hadoop
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the configuration file
template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: write the default index.html file
template: src=templates/index.html.j2 dest=/var/www/html/index.html
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
--------------------------------------------------
##模板文件
httpd.conf
https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced/blob/master/templates/httpd.conf.j2
index.html
https://github.com/ansible-book/playbook-examples-chapter3-ansible-advanced/blob/master/templates/index.html.j2
--------------------------------------------------
##运行命令
work:ansible ys$ ansible-playbook deploy.yml -i hosts -u zhangshuang -b
PLAY [hadoop] ******************************************************************
TASK [setup] *******************************************************************
ok: [172.16.1.163]
ok: [172.16.1.162]
TASK [ensure apache is at the latest version] **********************************
ok: [172.16.1.162]
ok: [172.16.1.163]
TASK [write the configuration file] ********************************************
ok: [172.16.1.162]
ok: [172.16.1.163]
TASK [write the default index.html file] ***************************************
ok: [172.16.1.162]
ok: [172.16.1.163]
TASK [ensure apache is running] ************************************************
ok: [172.16.1.163]
changed: [172.16.1.162]
PLAY RECAP *********************************************************************
172.16.1.162 : ok=5 changed=1 unreachable=0 failed=0
172.16.1.163 : ok=5 changed=0 unreachable=0 failed=0
--------------------------------------------------
##测试服务
work:ansible ys$ curl http://172.16.1.162
<html>
<body>
<div class="block" style="height: 99%;">
<div class="centered">
<h1>#46 Demo</h1>
<p>Served by master (172.16.1.162).</p>
</div>
</div>
</body>
</html>
work:ansible ys$ curl http://172.16.1.163
<html>
<body>
<div class="block" style="height: 99%;">
<div class="centered">
<h1>#46 Demo</h1>
<p>Served by slave1 (172.16.1.163).</p>
</div>
</div>
</body>