ansible安装配置及最佳实践roles

ansible是什么?

ansible是一款轻量级配置管理工具,用于远程批量部署、安装、配置。相似的还有puppet、saltstack,各有所长,任君自选。html

官方文档:http://docs.ansible.com/ansible/latest/index.htmlpython

中文文档:http://www.ansible.com.cn/index.htmlnginx

安装ansible

Linux系统上最简单的可使用yum安装,但因为ansible故不须要后台进程,不须要root权限,不依赖其余软件,只要有ssh和python环境便可运行,这里采用run from source安装方式。git

#python版本2.5以上,低于2.5须要额外安装模块python-simplejson
#安装pip yum install epel-release -y ; yum install python-pip -y
#开始安装
python --version
    Python 2.7.5
mkdir /app && cd /app
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible/
source ./hacking/env-setup   #一般每次登陆都要执行一次来配置环境,咱们能够将其添加到~/.bash_profile文件中,保证每次登陆都会自动执行
echo "source /app/ansible/hacking/env-setup" >> ~/.bash_profile
pip install paramiko PyYAML Jinja2 httplib2 six

//此外还能够经过pip install ansible的方式来安装,更加方便

 配置ssh密钥认证

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.10

 配置inventory文件并测试

inventory文件是ansible的客户机清单,默认的位置是/etc/ansible/hosts,固然咱们可使用 -i 参数另行指定。github

cd /app
mkdir ansible-playbook
cd ansible-playbook/
echo "172.16.1.10" > hosts
ansible all -i /app/ansible-playbook/hosts -m ping

 最佳实践:playbook+roles方式

"ansible all -i /app/ansible-playbook/hosts -m ping” 这种执行方式被称为ad-hoc模式,即命令行或交互模式,但任何配置管理工具的官方文档都会告诉你要用编排的方式进行复杂的部署,例如saltstack里的.sls文件,ansible里的playbook。除此以外,ansible提供了一种目录树结构的编排方式,顶层目录对应roles,里面包含子目录,好比defaults、files、tasks、templates等,不一样的子目录对应不一样的功能,这种方式使得管理和重复调用变得极为方便。shell

用ansible编译安装nginx

注意:json

1.roles下子目录里必需要有main.yml文件,ansible会自动查询并执行。bash

2.roles目录和nginx.yml放在同一级目录中,或者在ansible.cfg中配置roles的查询路径。app

3.若是yml中冒号后引用jinja模板以{{开头,则整行语句须要加上" ",防止yml认为这是个字典。less

[root@localhost app]# tree ansible-playbook
ansible-playbook
├── nginx.yml
└── roles
    └── nginx                   #这就是在nginx.yml主文件中指定的role
        ├── defaults
        │   └── main.yml
        ├── files
        │   ├── compile.sh.j2
        │   └── nginx-1.6.3.tar.gz
        ├── handlers
        │   └── main.yml
        ├── tasks
        │   ├── install.yml
        │   └── main.yml
        └── templates
            └── nginx.conf.j2

1.defaults中存放默认的变量,能够经过jinja模板调用
2.files中存放文件、软件包、脚本等内容,能够被copy、unarchive、script等模块调用
3.handlers中存放依赖任务,能够被notify关键字调用
4.tasks中存放主任务,ansible会首先进行调用
5.templates中存放模板文件,模板中可使用jinja模板调用defaults中定义的变量,被templates模块调用

tasks 

nginx的安装过程包括建立用户、建立目录、下载安装包、下载依赖包、编译安装、建立软连接、修改配置文件、测试、启动这些环节。

[root@localhost nginx]# tree tasks/
tasks/
├── install.yml
└── main.yml

[root@localhost nginx]# less tasks/main.yml 

---
- import_tasks: install.yml

#ansible的playbook以---开头,而后使用yml语法编写
#tasks/main.yml中加载install.yml,include方式不久会被淘汰,这里采用import_tasks关键字

[root@localhost nginx]# less tasks/install.yml 

---
- name: groupadd nginx   #建立组,存在则忽略,group模块   - name:说明
  group:                          
    name: "{{ group }}"
    gid: 888

- name: useradd nginx   #建立用户,存在则忽略,user模块
  user:
    name: "{{ user }}"
    group: "{{ group }}"
    uid: 888
    createhome: no
    shell: /sbin/nologin

- name: install pcre-devel  #安装依赖,package模块
  package:
    name: pcre-devel
    state: latest

- name: install openssl-devel  #安装依赖,package模块
  package:
    name: openssl-devel
    state: latest

- name: create /tools    #建立目录,file模块
  file: 
    path: /tools
    state: directory  

- name: copy and extract nginx tarball  #解压压缩包,unarchive模块
  unarchive: 
    src: "{{ tarball_name }}"
    dest: /tools

- name: ./configure         #检查环境,command模块
  command: ./configure --user={{ user }} --group={{ group }} --prefix=/app/{{ nginx_dir }} --with-http_stub_s
tatus_module --with-http_ssl_module
  args:
    chdir: /tools/{{ nginx_dir }}

- name: make    #编译,command模块
  command: make
  args:
    chdir: /tools/{{ nginx_dir }}

- name: make install    #安装,command模块
  command: make install
  args:
    chdir: /tools/{{ nginx_dir }}

- name: modify nginx configuration   #修改配置文件,template模块
  template:
    src: "{{ nginx_configuration }}"
    dest: /app/{{ nginx_dir }}/conf/nginx.conf

- name: make link     #建立软链接,file模块
  file:
    src: /app/{{ nginx_dir }}
    dest: /app/nginx
    state: link

- name: test nginx   #测试nginx配置,command模块
  command: /app/nginx/sbin/nginx -t
  notify:              #调用handlers目录下的main.yml
    - start nginx

 handlers

[root@localhost nginx]# tree handlers/
handlers/
└── main.yml

[root@localhost nginx]# less handlers/main.yml 

---
- name: start nginx     #notify下面指定的内容在name这里定义
  command: /app/nginx/sbin/nginx

#这里只是演示,实际批量部署不须要nginx -t 这一步

 files

[root@localhost nginx]# tree files/
files/
└── nginx-1.6.3.tar.gz

#ansible中unarchive、copy等模块会自动来这里找文件,从而咱们没必要写绝对路径,只需写文件名

 templates

[root@localhost nginx]# tree templates/
templates/
└── nginx.conf.j2

#通常来讲,模板文件中都会使用jinja2模板,因此一般咱们在模板文件后加上.j2后缀,但不是必须的

[root@localhost nginx]# less templates/nginx.conf.j2 

    server {
        listen       {{ nginx_port }};    #这里使用jinja模板引用变量
        server_name  localhost;


#template模块会将模板文件中的变量替换为实际值,而后覆盖到客户机指定路径上

 defaults

[root@localhost nginx]# tree defaults
defaults
└── main.yml

[root@localhost nginx]# less defaults/main.yml 

user: nginx
group: nginx
tarball_name: nginx-1.6.3.tar.gz
nginx_configuration: nginx.conf.j2
nginx_dir: nginx-1.6.3
nginx_port: 2223             #这是咱们刚才在模板文件中使用的变量

#defaults中的变量优先级最低,一般咱们能够临时指定变量来进行覆盖

 执行playbook

到了激动人心的时刻,ansible的好处在于什么都不用配,直接就能用,因此这里咱们将inventory、nginx.yml、roles目录放在同一级目录ansible-playbook下,便于管理

#首先看看nginx.yml主文件

[root@localhost ansible-playbook]# less nginx.yml 

---
- name: deploy nginx
  hosts: all
  remote_user: root
  roles:
    - nginx


#hosts表示选择哪些主机进行部署
#remote_user表示选择哪一个用户进行部署
#roles表示选择部署什么内容

#固然,这里还能够经过字典的方式指定不一样的变量
---
- name: deploy nginx
  hosts: all
  remote_user: root
  roles:
    - { role: nginx, nginx_port: 8080 }

咱们进入ansible-playbook目录下,执行 ansible-playbook -i hosts nginx.yml 便可开始部署

[root@localhost ansible-playbook]# ansible-playbook -i hosts nginx.yml 

TASK [Gathering Facts]   **************************************************************************************
ok: [172.16.1.10]

TASK [nginx : groupadd nginx]  *******************************************************************************
ok: [172.16.1.10]

TASK [nginx : useradd nginx] ********************************************************************************
ok: [172.16.1.10]

。。。。。

# TASK[]里的内容就是定义在首行name中的提示内容
# -i 表示自行指定inventory文件

 总结

到这里,ansible的基本用法就展现完毕了,能够看出ansible自己很简单,重点在于对模块的掌握状况,建议要常常练习,常常去官方文档的Module Index部分查看各模块的用法。

相关文章
相关标签/搜索