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 08:27
下一篇 2016-11-14 08:28

相关推荐

  • 关于大型网站技术演进的思考(六):存储的瓶颈(6)

    原文出处: 夏天的森林  在讲数据库水平拆分时候,我列出了水平拆分数据库需要解决的两个难题,它们分别是主键的设计问题和单表查询的问题,主键问题前文已经做了比较详细的讲述了,但是第二个问题我没有讲述,今天我将会讲讲如何解决数据表被垂直拆分后的单表查询问题。 要解决数据表被水平拆分后的单表查询问题,我们首先要回到问题的源头,我们为什么需要将数…

    2015-03-11
  • 数组和变量之间的关系及如何灵活运用数组

    什么是数组?数组:存储多个元素的连续的内存空间,相当于多个变量的集合什么是变量? 变量即是存储单个元素的内存空间 如果整个数组只有一个名字;数组名是数组的标识;也是数组所指向的存储多个元素的连续的内存空间开始地址的标识;数组索引定义从 0 开始(第一个数组元素);到 N 结束引用某个数组元素时,数组名+【索引】格式:${ARRAY_NAM…

    Linux干货 2017-05-22
  • 从Linux小白到大牛——与狼共舞的日子10

    马哥教育网络班21期+第10周课程练习 1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) POST –> Boot Sequence(BIOS) –> Boot Loader(MBR)   –> Kernel+ramdi…

    Linux干货 2016-12-05
  • hello 小伙伴们

    一起跟着马哥,努力向前!不管多么累、不管有多忙,不要间断、不要放弃,为了实现我们心中所想的,努力吧,骚年们!

    Linux干货 2016-08-08
  • shell脚本编程基础

    一.编程基础              程序:指令+数据              程序编程风格:  &n…

    Linux干货 2016-08-12
  • 8.1_Linux习题和作业

    7.28 作业 1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 1 # cat /etc/issue | tr 'a-z' 'A-Z'whoi > /tmp/issue.out 2、将当前系…

    Linux干货 2016-08-04