Ansible

Ansible简介

  • ansible是一种基于python语言开发的轻量级自动化运维工具,它可以自动化批量完成主机服务配置管理,软件部署,执行特定命令等工作

  • ansible的核心组件有ansible core(核心代码),host inventory(要管理的主机),core modules(核心模块),custom modules(用户可以自定义模块),playbook (yaml, jinjia2),connect plugin(paramiko)

Ansible的安装和基础配置

Summary     : SSH-based configuration management, deployment, and task execution system
URL         : http://ansible.com
License     : GPLv3+
Description :         需要配置好epelYUM源
            : Ansible is a radically simple model-driven configuration management,
            : multi-node deployment, and remote task execution system. Ansible works
            : over SSH and does not require any software or daemons to be installed
            : on remote nodes. Extension modules can be written in any language and
            : are transferred to managed machines automatically.
[root@centos ~]# yum install ansible -y  自动安装yaml,jinjia2,paramiko等相关库
,,,,,,,
Complete!
[root@centos ~]# ansible --version  测试是否安装成功,提示有ansible版本号
ansible 2.1.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
[root@centos ~]# ssh-keygen 基于秘钥对管理client
[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.40.138
[root@centos ~]# ssh root@192.168.40.138  "ifconfig>/dev/null"  测试是否能够免密码登录client,我这里只有一台client
[root@centos ~]#
[root@centos ansible]# vim /etc/ansible/hosts 定义client
[singleclient]  被管主机的定义方式,该配置文件有详细范例
192.168.40.138
[root@centos ansible]# ansible all  -m ping   需要关闭client的selinux
192.168.40.138 | SUCCESS => {
    "changed": false, 
    "ping": "pong"     测试能ping通
}

Ansible常用命令的使用

 ansible <host-pattern主机列表> [-m module_name模块名字] [-a args模块参数] [options]
 常用[options] 更多参数需要man ansible 或 ansible --help了解
 
 -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur 预测结果
 -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check 
 -e EXTRA_VARS, --extra-vars=EXTRA_VARS 向playbook里传递变量
                        set additional variables as key=value or YAML/JSON
 -f FORKS, --forks=FORKS 多少个线程并行执行任务
                        specify number of parallel processes to use
                        
 ansible-doc - show documentation on Ansible modules
 ansible-doc -l 列出所有可用模块 ansible-doc -s module 查看某个模块用法
 
 ansible-playbook - run an ansible playbook 运行剧本内的编排任务

Ansible的playbook编写

  • playbook的主要组成部分Tasks(由多个模块组成的多个任务),Variables(变量),Template(模版针对服务配置文件),Handler(处理器),Roles(角色)

  • playbook一定要指定Hosts(主机),Users(以哪个用户身份执行任务)

  • playbook遵循YAML语言语法格式,常用的有序列用“-”表示;key:value键值对;{k1:v1,k2:v2}字典等

  • 下面为playbook由tasks,variables,template,handler,roles逐个测试,当然可以组合使用,组合使用才能发挥playbook的任务编排能力

[root@centos ~]# ansible-playbook ./test1.yml  --syntax-check
playbook: ./test1.yml
[root@centos ~]# cat test1.yml 
- hosts: singleclient 指定要在那些主机上运行playbook
  remote_user: root  指定了以那个用户身份去运行playbook
  tasks:
  - name: install httpd 任务一
    yum: name=httpd state=present
  - name: start the httpd service 任务二
    service: name=httpd state=running
  - name: reporting 任务三
    shell: /bin/echo "httpd installed and running"
  ignore_errors: true  如果在运行任务三时报错,就忽略掉报错
[root@centos ~]# ansible-playbook ./test1.yml 
PLAY [singleclient] ************************************************************
其中- host是顶级层是个大列表,remote_user,tasks和ignore_errors是顶级层的元素,
remote_user和tasks都是键值对,tasks的值比较特殊,为三个列表;playbook中可以有多个- host
ansible的变量分为fact变量就是ansible all -m setup获得的变量
通过--extra-var 参数传进playbook的变量,playbook 中通过{{}}引用变量 例如:
yum: name={{software}} state=present 也可以在playbook中通过var关键字来定义并引用
ansible-doc --extra-var "sotfware=nginx" test1.yml 则会安装nginx
inventory的主机变量和组变量,在主机后面加上k=v ,playbook通过{{k}}来引用,模版则会针对不同的主机得到不同的值
[root@centos ~]# cat index.txt /template/index.j2 
just test port for {{ port }} 源文件
just test a port {{ port }} 模版文件
[root@centos ~]# cat test3.yml 
- hosts: singleclient
  remote_user: root
  tasks: 此处为简单测试,实际生产环境会针对服务的配置文件来操作
  - name: copy configuration file
    copy: src=/root/index.txt dest=/tmp/index.txt 复制源文档到client
  - name: modify configuration file
    template: src=/template/index.j2 dest=/tmp/index.txt 根据模版修改client的index.txt文档
[root@centos ~]# grep "port" /etc/ansible/hosts 
192.168.40.138 port=888  主机变量 port
#port=888
[root@centos ~]# ssh root@192.168.40.138 "cat /tmp/index.txt"
just test a port 888
[root@centos ~]# grep  -C2 "singleclient" /etc/ansible/hosts 
## 192.168.100.1
## 192.168.100.10
[singleclient]采用组变量方式,40.138,40.200都会相同的变量,其值也相同
192.168.40.138 
192.168.40.200
[singleclient:vars]
port=888
# Ex 2: A collection of hosts belonging to the 'webservers' group
[root@centos ~]# ansible-playbook test3.yml  test3.yml仍能正常执行
PLAY [singleclient] ************************************************************
[root@centos ~]# ssh root@192.168.40.138 "cat /tmp/index.txt"
just test a port 888   测试结果和主机变量测试结果相同
ansible的条件,迭代和处理器测试
tasks: 当client主机发行版为CentOS时,才会关机
  - name: "shut down CentOS 6 systems"   
    command: /sbin/shutdown -t now  
    when:
    - ansible_distribution == "CentOS"
[root@centos ~]# ansible-playbook test4.yml 
PLAY [singleclient] ************************************************************
TASK [setup] *******************************************************************
ok: [192.168.40.138]
TASK [copy wrong file to test when] ********************************************
fatal: [192.168.40.138]: FAILED! => {"failed": true, "msg": "the file_name 
...ignoring
TASK [ping test] ***************************************************************
ok: [192.168.40.138]
PLAY RECAP *********************************************************************
192.168.40.138             : ok=3    changed=0    unreachable=0    failed=0   
[root@centos ~]# cat test4.yml 
- hosts: singleclient
  remote_user: root
  tasks:
  - name: copy wrong file to test when
    copy: src=/root/index dest=/tmp/index.txt
    register: result
    ignore_errors: True 为了不影响下一条任务执行和when条件判断
  - name: ping test
    ping: 
    when: result|failed  上一条命令执行失败时,才执行ping
,,,,,,,,,,,,,,,,,
- name: add several users  user: name={{ item }} state=present groups=wheel 迭代测试 
 with_items: 
     - testuser1 
     - testuser2
,,,,,,,,,,,,,,,,,    
tasks:处理器测试
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:  表明触发条件
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:  执行相应动作
    - name: restart apache
      service: name=httpd state=restarted
ansible的roles使用
roles是变量,任务,文件,模版,处理器,分别放在单独的目录并都是role目录的子目录
role目录会有site.yml为ansible-playbook的执行入口,需要相应的文件是会自动导入
适用场景当有大量主机要安装配置不同的服务时,比如20台主机需要安装配置nginx,
其他20台需要安装配置mysql,则可以新建nginx和mysql的role来执行安装配置nginx和mysql服务

role内各目录中可用的文件

tasks目录:至少应该包含一个名为main.yml(只写任务列表)的文件,其定义了此角色的任务列表

files目录:存放由copy或script等模块调用的文件; 

templates目录:template模块会自动在此目录中寻找Jinja2模板文件; 

handlers目录:此目录中应当包含一个main.yml(只写handler)文件,用于定义此角色用到的各handler;

vars目录:应当包含一个main.yml(只写变量),用于定义此角色用到的变量;

meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系;

default目录:为当前角色设定默认变量时使用此目录;应当包含一个main.yml文件;

roles目录的同级目录下需要新建site.yml 来作为ansible-playbook的执行入口,定义Hosts和Users
使用roles关键字来指定执行roles目录下的哪个role中的编排任务

通过以上总结可以对ansible的简单使用加深理解,ansible的其他用法需要参考官网或其他文档

原创文章,作者:Snoo,如若转载,请注明出处:http://www.178linux.com/57466

(0)
上一篇 2016-11-14 08:27
下一篇 2016-11-14 08:28

相关推荐

  • Linux文件压缩、解压、归档总结

    一、简介   压缩对我们来说通常意味着减小文件体积,节省硬盘空间。在Windows平台上处理大文件或目录中的文件很多时我们都需要对这些文件进行压缩处理,压缩格式多种多样,如:zip、rar、7z等格式,压缩方式也比较多样话,支持按最大压缩比例或者最快时间进行压缩等多种方式,而生成的压缩包就可以让我们更方便的进行拷贝及归档整理。解压缩顾名思义就是回归…

    Linux干货 2015-09-26
  • python面向对象学习第一周

     面向对象的思想 一个具体对象的属性方法,都有各个来源,来源于类,比如消化类,有各种各样的消化模式,人类的只是其中一种 另一种思想方法,类有各种属性方法,人类有很多的属性,一个人应该包括其中的属性,只是值上可能有不同,不同人群也有他们的特征属性和方法     类是对象的抽象,但是类本身也是对象, 对象是一个类的实例. 类的属性:类变量,对象方法,类方法,静态…

    Linux干货 2017-11-13
  • shell脚本编程之数组

    bash仅支持一维数组,类型声明: declare -i    : 表示数值 declare -a  :表示普通数组(默认,可不用声明类型) declare -A :表示关联数组(必须声明类型,bash需4.0以上版本才支持,可通过bash –version查看版本) 一、普通数组(用数字为下标) 1.1数组赋值(修…

    Linux干货 2015-08-24
  • linux中进程及内存管理工具

    pstree 显示进程树 ({}是线程) 选项-p #显示进程编号 pstree username #查看指定用户的进程 centos7中可以使用选项-s来查看指定进程的父子进程   ps (默认显示当前终端运行的进程) 支持三种选项: 1、UNIX选项 -C cmdlist 指定命令(允许同时指定多个命令) -L 显示线程 -e: 显示所有进程,…

    Linux干货 2017-12-17
  • CentOS6 ELK实现

    1 简介 我们来介绍Centos6.5基于SSL密码认证部署ELK(Elasticsearch 1.4.4+Logstash 1.4.2+kibana3),同时为大家介绍如何集合如上组件来收集日志,本章的日志收集主要为大家介绍SYSTEM日志收集. 集中化日志收集主要应用场景是在同一个窗口临时性或永久性鉴定分析系统,应用等各类日志,对用户提供极大便利,同时也…

    2015-02-15
  • 中秋干货之系统启动修复

    在使用CentOS系统时,难免会有误操作而导致机器不能正常启动,这里介绍了多种启动失败的原因和修复的方法。 grub损坏类 grub 1stage 被破坏使用dd擦写MBR前446字节,即抹去stage1阶段–[root@_2_ ~]# reboot #重启–启动失败,找不到系统,这时只能借助光盘进入修复模式,重新安装grub&#82…

    Linux干货 2016-09-15