Linux之Ansible

2016-03-07  本文已影响1231人  魏镇坪

Ansible简介

Ansible是2013年推出的一款IT自动化和DevOps软件,目前由Redhat已签署Ansible收购协议。其是基于Python研发,糅合了人多老运维工具的优点实现了批量操作系统配置,批量程序的部署,批量运行命令等功能。

Ansible的特性:

Ansible的组件

imgimg

ansible的配置文件

ansible安装

ansible命令

ansible-doc命令

获取模块列表,及模块使用格式

ansible常用模块

一、command模块

[root@Centos7 ~]# ansible all -a 'ntpdate 172.16.0.1'
172.16.36.61 | success | rc=0 >>
29 Feb 19:36:07 ntpdate[2360]: step time server 172.16.0.1 offset 135.642843 sec

172.16.36.60 | success | rc=0 >>
29 Feb 19:36:07 ntpdate[2402]: step time server 172.16.0.1 offset 135.687205 sec

172.16.36.71 | success | rc=0 >>
29 Feb 19:36:13 ntpdate[2345]: step time server 172.16.0.1 offset -28665.158303 sec

172.16.36.70 | success | rc=0 >>
29 Feb 19:36:13 ntpdate[32087]: step time server 172.16.0.1 offset -28664.450998 sec

二、user模块

管理用户

三、group模块

组管理

四、cron模块

定义cron任务

五、ping模块

测试网络连通性, ping模块没有参数

[root@Centos7 ~]# ansible all -m ping
172.16.36.61 | success >> {
    "changed": false,
    "ping": "pong"
}

172.16.36.60 | success >> {
    "changed": false,
    "ping": "pong"
}

172.16.36.70 | success >> {
    "changed": false,
    "ping": "pong"
}

172.16.36.71 | success >> {

六、file模块

功能

文件创建和删除

使用格式
使用示例

七、copy模块

功能

文件复制,把管理端的文件复制到远程主机各一份

使用格式
使用示例

八、yum模块

功能

实现程序包安装及管理

使用格式
使用示例

九、service模块

功能

实现服务管理

使用格式
使用示例

十、shell模块

功能

运行shell命令,其是启动一个子shell进程来运行命令,它可以支持管道传送

使用格式
使用示例

十一、script模块

功能

指定本地的脚本文件,到远程主机运行一次

使用格式
使用示例

*ansible webserver -m script -a '/root/test.sh'

十二、setup模块

功能

收集远程指定主机的facts信息,其将收集的信息保存在各变量中,变量引用方法为:直接引用名称

使用示例
[root@Centos7 ~]# ansible 172.16.36.71 -m setup
172.16.36.71 | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.16.36.71"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:fed1:dcb5"
        ],
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "05/20/2014",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-229.el7.x86_64",
            "LANG": "zh_CN.UTF-8",
            "crashkernel": "auto",
            "quiet": true,
            "rhgb": true,
            "ro": true,
            "root": "UUID=cdd3548e-caf3-46cb-b81c-03f443325edc"
        },
        "ansible_date_time": {
            "date": "2016-02-29",
            "day": "29",
            "epoch": "1456751201",
            "hour": "21",
            "iso8601": "2016-02-29T13:06:41Z",
            "iso8601_micro": "2016-02-29T13:06:41.194636Z",
            "minute": "06",
            "month": "02",
            "second": "41",
            "time": "21:06:41",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "Monday",
            "year": "2016"
        },
        "ansible_default_ipv4": {
            "address": "172.16.36.71",
            "alias": "eno16777736",
            "gateway": "172.16.0.1",
            "interface": "eno16777736",
            "macaddress": "00:0c:29:d1:dc:b5",
            "mtu": 1500,
            "netmask": "255.255.0.0",
            "network": "172.16.0.0",
            "type": "ether"
        },
        
        .........

playbooks剧本

简介

playbooks是ansible更为强大的配置管理组件,实现基于文本文件编排执行的多个任务,且多次重复执行。其是使用YAML(Yet Another Markup Language),类似于半结构化语言,声明式配置,可读性较高。易于与脚本语言交互

核心组件

YAML语法格格式

- hosts: webserver
  remote_user: root
  tasks:
    -  name: install nginx
       yum: name=nginx state=present
    -  name: start nginx
       service: name=nginx state=started enabled=true
    -  name: install php-fpm
       yum: name=php-fpm state=present
    -  name: start php-fpm
       service: name=php-fpm state=started enabled=true
   vars
   handlers
-  hosts: dbserver
   remote_user: root
   tasks:
     -  name: install mysql
        yum: name=mysql state=present

playbook的变量

变量命名方式

字母、数字和下划线组成,仅能以字母开头

变量的各类
playbook调用变量的示例
-  hosts: webserver
   remote_user: root
   vars:
     user: user2
     pass: 123321
   tasks:
       - name: add user
         user: name={{ user }} state=present
       - name: set password
         shell: /bin/echo {{ pass }} | passwd --stdin {{ user }}
手动运行playbook脚本时,传递变量(手动传递时,优先级要高于脚本中的变量)

playbook的条件测试

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

####当主机的操作系统为redhat系列操作系统时,才执行yum安装
- hosts : webserver
  remote_user : root
  tasks :
     - name: yum install keepalived
       yum: name=keepalived state=present
       when: ansible_os_family == "ReaHat"
     - name: say hello
       shell: /bin/echo "hello world"

playbook的迭代

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

- hosts: webserver
  remote_user: root
  tasks:
    - name: user add
      user: name={{ item }} state=present
      with_items:
         - testuser1
         - testuser2
         - testuser3
         - testuser4
- hosts: webserver
  remote_user: root
  tasks:
  - name: user add
    user: name={{ item.name }} state=present group={{ item.groups }}
    with_items:
    - { name: 'tom2', groups: 'tom' }
    - { name: 'tom2', groups: 'tom2' }

playbook--handlers: 处理器、触发器

只有在其关注的条件满足时,才会被触发执行的任务

示例:只有原配置文件被修改了,才会重新加载服务
- hosts: webserver
  remote_user: root
  tasks:
  - name: yum install nginx
    yum: name=nginx state=present
  - name: start nginx
    service: name=nginx state=started enabled=true
  - name: copy configuration file
    copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
    notify: restart nginx
  handlers:
  - name: restart nginx
    service: name=nginx state=reloaded

Roles: 角色

roles用于实现“代码复用”,让playboo中的各元素组织起来,roles以特定的层次型格式组织起来的playbook元素(vars,tasks,template,handlers),可被playbook以roles的名字直接调用

roles目录结构
示例:

~]# cd /etc/ansible/soles   
~]# mkdir -pv webserver/{files,vars,tasks,handlers,meta,templates}
~]# cd webserver/
~]# vim vars/main.yml
user: daemon
group: daemon
~]# vim tasks/main.yml
- name: remove nginx
  yum: name=nginx state=absent
- name: install apache
  yum: name=httpd state=present
- name: start & enable httpd
  service: name=httpd state=started enabled=true
- name: install configuration file
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd servic
~]# vim handlers/main.yml
- name: restart httpd service
  service: name=httpd state=restarted
~]# cp /etc/httpd/conf/httpd.conf /root/webserver/template/httpd.conf.j2  #准备示例文档
~]# vim templates/http.conf.j2
....
User {{ user }}
Group {{ group }} 
....
~]# cd ../.. #与roles同级的目录
~]# vim webserver.yml
- hosts: webserver
  remote_user: root
  roles:
  - webserver
  
~]# ansible-playbook webserver.yml
PLAY [webserver] **************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.36.70]
ok: [172.16.36.71]

TASK: [webserver | remove nginx] **********************************************
ok: [172.16.36.70]
ok: [172.16.36.71]

TASK: [webserver | install apache] ********************************************
ok: [172.16.36.70]
ok: [172.16.36.71]

TASK: [webserver | start & enable httpd] **************************************
ok: [172.16.36.71]
ok: [172.16.36.70]

TASK: [webserver | install configuration file] ********************************
ok: [172.16.36.70]
ok: [172.16.36.71]

PLAY RECAP ********************************************************************
172.16.36.70               : ok=5    changed=0    unreachable=0    failed=0
172.16.36.71               : ok=5    changed=0    unreachable=0    failed=0


#####可以为roles传递变量
~]# vim dbserver.yml
- hosts: 172.16.36.60
  remote_user: root
  roles:
  - { role: webserver, user=nobody }
  - { role: dbserver, user=mysql }  #可以调用多个role
  
 
######使用tags参数,只调用某个tasks(以下示例只触发了install config的任务)
~]# vim roles/webserver/tasks/main.yml
- name: remove nginx
  yum: name=nginx state=absent
- name: install apache
  yum: name=httpd state=present
- name: start & enable httpd
  service: name=httpd state=started enabled=true
- name: install configuration file
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd service
  tags: conf

[root@Centos7 ~]# ansible-playbook -t conf webserver.yml

PLAY [webserver] **************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.36.71]
ok: [172.16.36.70]

TASK: [webserver | install configuration file] ********************************
ok: [172.16.36.70]
ok: [172.16.36.71]

PLAY RECAP ********************************************************************
172.16.36.70               : ok=2    changed=0    unreachable=0    failed=0
172.16.36.71               : ok=2    changed=0    unreachable=0    failed=0

上一篇下一篇

猜你喜欢

热点阅读