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)
SnooSnoo
上一篇 2016-11-14
下一篇 2016-11-14

相关推荐

  • NoSQL—mongodb常见使用和入门

    NoSQL介绍: NoSQL数据管理系统是目前非常流行的一种非关系性、分布式、不支持ACID设计规范式的数据库;NoSQL简单的数据模型、元数据和数据分离、弱一致性、高吞吐量、高水平扩展能力和低端硬件集群使其流行的主要原因,而mongodb就是NoSQL数据库一种非常流行的实现方式。   常见的NoSQL数据存储模型 列式模型 文档类型 应用场景:…

    2015-09-01
  • Linux运维实战之2-2:bash的工作特性

    这次博文我们主要来谈谈bash的工作特性哈。 主要内容: bash是一种shell解释程序     bash工作特性之命令状态返回值 bash工作特性之命令行展开 bash工作特性之命令补全 bash工作特性之路径补齐 bash工作特性之命令引用 bash工作特性之命令别名 bash工作特性之文件名通配 bash工作特性之命令hash 问…

    Linux干货 2016-11-06
  • 路由器解析路由表

    1.定义——>选择最佳路径;他只完成发送到下一个路径上就结束,任务到达就撤了(就像快递员一样,当他将物件发往到下一个目的地,那他的任务就结束了)2.分类:主机路由、网络路由、默认路由3.每个路由记录由四项主要组成部分目标地址:主机IP、网络ID号、未知地址(0.0.0.0)子网掩码接口interface:从哪个口发往目标地址 网…

    2017-09-05
  • 管中窥豹—linux命令

    命令行选项风格: 1、原始unix风格     a、命令行选项以连字符'-'开头,后跟单个字符表示选项,选项后面跟着取值,如:mysql -hlocalhost      b、选项不带取值的,可以组合在一起,如:sed -n -r 可以写成 sed -nr  …

    Linux干货 2016-10-30
  • Mysql备份II

    Mysql备份II V.II.I单台或共用机器,数据量和访问量小50G< 1 Mysqldump(全导出,导库,导表) 锁表 如果这时有些入会锁住或者超时 2 至少停止写入 防止innodb配置还没刷到磁盘里 先flash tables /usr/local/mysql/bin/mysqladmin -S /tmp/mysql.so…

    Linux干货 2016-06-09
  • Linux sed命令详则

    sed命令 sed是一种流编辑器,它是文本处理中非常好的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前的行储存在临时缓存区中,称为“模式空间”(pattern space),接着用sed命令处理缓存区中的内容,处理完成后,把缓存区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed…

    2017-08-11