ansible

2017-08-27  本文已影响54人  Bruce_King

1、What's the Asible?

Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

1.1、Ansible's structure

ansible.png

Host Inventory : 主机库,用于存放管控的主机列表。
Core Modules : 核心模块
Custom Modules : 自定义模块
Ad Hoc Commands :
Playbooks : 剧本,按照设定的顺序执行完成任务。
  Tasks : 任务
  Variables : 变量
  Templates : 模板
  Handlers : 处理器
  Roles : 角色,用于调度不同的playbooks.

2、How to use Ansible?

安装环境 : CentOS7

所需软件包 : Ansible(epel源)

2.1、Ansible's configuration files

2.2、ansible命令

ansible <host-pattern> [-m module_name] [-a args] [options]

使用实例

    ansible webserver -m shell -a "ss -tnl"    #查看webserver主机列表中的主机的TCP端口是否处于监听状态
    ansible webserver -m yum -a "name=httpd state=present"    #安装httpd服务

2.3、ansible-doc命令

ansible-doc [-l] [-s] [module...]

2.4、ansible常用模块

2.4.1 command模块

2.4.2 shell模块

[root@localhost ~]# ansible 192.168.0.14 -m shell -a "ss -tnl | grep 80"
192.168.0.14 | SUCCESS | rc=0 >>
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128         :::80                      :::*   

2.4.3、copy模块

2.4.4、cron模块

2.4.5、file模块

2.4.6、yum模块

2.4.7、service模块

2.4.8、user模块

2.4.9、group模块

2.4.10、ping模块

2.4.11、script模块

2.4.12、setup模块

3、Playbooks剧本

  Playbooks是ansible更为强大的配置管理组件,实现基于文本文件编排执行的多个任务,而且可以多次执行。Playbooks使用YAML,类似于半结构化语言,声明式配置,可读性很高,易于与脚本语言交互。

3.1核心组件

Playbooks的基本示例

- hosts: webserver
  remote_user: root
  tasks:
  - name: install nginx
    yum: name=nginx state=present
  - name: start nginx
    service: name=nginx state=started
  - name: change nginx
    template: src=/etc/ansible/ansible.file/nginx.conf.j dest=/etc/nginx/nginx.conf
    tags: change_conf
    notify: restart nginx
 handlers:
 - name: restart nginx
   service: name=nginx state=restarted

3.2、Playbook的条件测试

 在某个tasks后面添加when子句,即可实现条件测试功能,when语句支持jinja2语法。

示例

- hosts: webserver
  remote_user: root
  tasks:
  - name: install_httpd
    yum: name=httpd state=present
    when: ansible_os_family == "CentOS7"
  - name: start_nginx
    service: name=nginx state=started

3.2、Playbook的迭代

 在task中调用内置的item变量,在某task后面使用with_items语句来定义元素列表。

示例

- hosts: webserver
  remote_user: root
  tasks:
  - name: install some packages
    yum: name={{ item }} state=present
    with_items:
    - nginx
    - memcached
    - php-fpm


- hosts: webserver
  remote_user: root
  tasks:
  - name: add some groups
    user: name={{ item.name }} group={{ item.group }} state=present
    with_items:
     - { name: 'user11',group: 'group11' }
     - { name: 'user12',group: 'group12' }
     - { name: 'user13',group: 'group13' }

3.3、Roles 角色

  roles实现了"代码复用",让playbook中的各元素组织起来,roles是以特定的层级结构组织起来的playbook元素。

注意 : 在/etc/ansible目录下,有roles的目录,在此目录下为每个不同的服务提供不同的目录,每一个服务相当于一个角色,这样就能实现管理的标准化和便捷化。

示例 :

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

- name: install_nginx
  yum: name=nginx state=present
- name: start_nginx
  service: name=nginx state=started
- name: change_conf
  template: src=nginx.conf dest=/etc/nginx/nginx.conf
  notify: restart_nginx

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

- name: restart_nginx
  service: name=nginx state=restarted

[root@localhost nginx]# vi vars/main.yml
user: daemon
group: daemon

将nginx.conf文件复制到templates目录下,作为模板,并将其中的 user nginx
改为 user {{ user }}  {{ group }} ,作为一个变量使用。


在roles同级目录下创建 nginx.yml,调用角色nginx

 - hosts: webserver
   remote_user: root
   roles:
   - nginx
上一篇 下一篇

猜你喜欢

热点阅读