Ansible介绍

Ansible介绍

• 不须要安装客户端,经过sshd去通讯html

• 基于模块工做,模块能够由任何语言开发python

• 不只支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读linux

• 安装十分简单,centos上可直接yum安装nginx

• 有提供UI(浏览器图形化)www.ansible.com/tower,收费的git

• 官方文档 http://docs.ansible.com/ansible/latest/index.htmlgithub

• ansible已经被redhat公司收购,它在github上是一个很是受欢迎的开源软件,github地址https://github.com/ansible/ansibleshell

• 一本不错的入门电子书 https://ansible-book.gitbooks.io/ansiblevim

 

Ansible安装

• 准备两台机器,前面咱们作实验的两台机器aming-01,aming-02centos

• 只须要在aming-01上安装ansible浏览器

• yum list |grep ansible 能够看到自带源里就有2.4版本的ansible

• yum install -y ansible

• aming-01上生成密钥对 ssh-keygen -t rsa

• 把公钥放到aming-02上,设置密钥认证

• vi /etc/ansible/hosts //增长

•[testhost]

•127.0.0.1

•192.168.133.132

•说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost  ~]#
[root@localhost ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#

 

Ansible远程执行命令

• ansible  testhost -m command -a 'w' 

• 这样就能够批量执行命令了。这里的testhost 为主机组名,-m后边是模块名字,-a后面是命令。固然咱们也能够直接写一个ip,针对某一台机器来执行命令。

• ansible 127.0.0.1 -m  command -a 'hostname'

• 错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

• 解决: yum install -y libselinux-python

• 还有一个模块就是shell一样也能够实现

• ansible  testhost -m shell -a 'w'

 

实例:

[root@localhost ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#

Ansible拷贝文件或者目录

• ansible aming-02 -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mode=0755"

• 注意:源目录会放到目标目录下面去,若是目标指定的目录不存在,它会自动建立。若是拷贝的是文件,dest指定的名字和源若是不一样,而且它不是已经存在的目录,至关于拷贝过去后又重命名。但相反,若是desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。

• ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"

• 这里的/tmp/123和源机器上的/etc/passwd是一致的,但若是目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面创建passwd文件

 

实例:

[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localroot ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#
[root@localhost  ~]#

Ansible远程执行脚本

• 首先建立一个shell脚本

• vim  /tmp/test.sh  //加入内容

• #!/bin/bash

• echo `date` > /tmp/ansible_test.txt

• 而后把该脚本分发到各个机器上

• ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"

• 最后是批量执行该shell脚本

• ansible testhost -m shell -a "/tmp/test.sh"

• shell模块,还支持远程执行命令而且带管道

• ansible testhost -m shell -a "cat /etc/passwd|wc -l "

 

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

Ansible管理任务计划

• ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"

• 若要删除该cron 只须要加一个字段 state=absent 

• ansible testhost -m cron -a "name='test cron' state=absent"

• 其余的时间表示:分钟 minute 小时 hour 日期 day 月份 month

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

Ansible安装rpm包/管理服务

• ansible testhost -m yum -a "name=httpd"

• 在name后面还能够加上state=installed/removed

• ansible testhost -m service -a "name=httpd state=started enabled=yes"

• 这里的name是centos系统里的服务名,能够经过chkconfig --list查到。

• Ansible文档的使用

• ansible-doc -l   列出全部的模块

• ansible-doc cron  查看指定模块的文档

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

Ansible playbook的使用

• 至关于把模块写入到配置文件里面,例:

• vi  /etc/ansible/test.yml //加入以下内容

•---

•- hosts: aming-02

•  remote_user: root

•  tasks:

•    - name: test_playbook

•      shell: touch /tmp/lishiming.txt

• 说明: 第一行须要有三个杠,hosts参数指定了对哪些主机进行参做,若是是多台机器能够用逗号做为分隔,也可使用主机组,在/etc/ansible/hosts里定义;

• user参数指定了使用什么用户登陆远程主机操做;

• tasks指定了一个任务,其下面的name参数一样是对任务的描述,在执行过程当中会打印出来,shell是ansible模块名字

• 执行:ansible-playbook test.yml

•再来一个建立用户的例子:

• vi /etc/ansible/create_user.yml //加入以下内容

•---

•- name: create_user

•  hosts: aming-02

•  user: root

•  gather_facts: false

•  vars:

•    - user: "test"

•  tasks:

•    - name: create user

•      user: name="{{ user }}"

•说明:name参数对该playbook实现的功能作一个概述,后面执行过程当中,会打印 name变量的值 ,能够省略;gather_facts参数指定了在如下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,须要注意的是,变量值必定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增长的用户名字调用了上面user变量的值。

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

Ansible playbook中的循环

 

• vi /etc/ansible/while.yml //加入以下内容

•---

•- hosts: testhost

•  user: root

•  tasks:

•    - name: change mode for files

•      file: path=/tmp/{{ item }} mode=600

•      with_items:

•        - 1.txt

•        - 2.txt

•        - 3.txt

• 说明: with_items为循环的对象

• 执行 ansible-playbook while.yml

 

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

Ansible playbook中的条件判断

• vi /etc/ansible/when.yml //加入以下内容

•---

•- hosts: testhost

•  user: root

•  gather_facts: True

•  tasks:

•    - name: use when

•      shell: touch /tmp/when.txt

•      when: ansible_ens33.ipv4.address == "172.7.15.114“

• 说明:ansible aming-02 -m setup 能够查看到全部的facter信息

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

Ansible playbook中的handlers

• 执行task以后,服务器发生变化以后要执行的一些操做,好比咱们修改了配置文件后,须要重启一下服务 vi /etc/ansible/handlers.yml//加入以下内容

•---

•- name: handlers test

•  hosts: aming-02

•  user: root

•  tasks:

•    - name: copy file

•      copy: src=/etc/passwd dest=/tmp/aaa.txt

•      notify: test handlers

•  handlers:

•    - name: test handlers

•      shell: echo "111111" >> /tmp/aaa.txt

• 说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操做。也就是说若是1.txt和2.txt内容是同样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操做。

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

playbook实战-nginx安装1

• 思路:先在一台机器上编译安装好nginx、打包,而后再用ansible去下发

• cd /etc/ansible   进入ansible配置文件目录

• mkdir  nginx_install   建立一个nginx_install的目录,方便管理

• cd nginx_install

• mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

• 说明:roles目录下有两个角色,common为一些准备操做,install为安装nginx的操做。每一个角色下面又有几个目录,handlers下面是当发生改变时要执行的操做,一般用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates一般存一些配置文件,启动脚本等模板文件,vars下为定义的变量

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

 

playbook实战-nginx安装2

• 须要事先准备好安装用到的文件,具体以下:

• 在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件

• 安装好后,咱们须要把nginx目录打包,并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为nginx.tar.gz

• 启动脚本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面

• cd  /etc/ansible/nginx_install/roles

• 定义common的tasks,nginx是须要一些依赖包的

•vim  ./common/tasks/main.yml //内容以下

•- name: Install initializtion require software

•  yum: name={{ item }} state=installed

•  with_items:

•    - zlib-devel

•    - pcre-devel

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

playbook实战-nginx安装3

•定义变量

•vim /etc/ansible/nginx_install/roles/install/vars/main.yml //内容以下

•nginx_user: www

•nginx_port: 80

•nginx_basedir: /usr/local/nginx

•首先要把全部用到的文档拷贝到目标机器

•vim   /etc/ansible/nginx_install/roles/install/tasks/copy.yml //内容以下

•- name: Copy Nginx Software

•  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root

•- name: Uncompression Nginx Software

•  shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/

•- name: Copy Nginx Start Script

•  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755

•- name: Copy Nginx Config

•  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

playbook实战-nginx安装4

•接下来会创建用户,启动服务,删除压缩包

•vim   /etc/ansible/nginx_install/roles/install/tasks/install.yml //内容以下

•- name: Create Nginx User

•  user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin

•- name: Start Nginx Service

•  shell: /etc/init.d/nginx start

•- name: Add Boot Start Nginx Service

•  shell: chkconfig --level 345 nginx on

•- name: Delete Nginx compression files

•  shell: rm -rf /tmp/nginx.tar.gz

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

playbook实战-nginx安装5

•再建立main.yml而且把copy和install调用

•vim   /etc/ansible/nginx_install/roles/install/tasks/main.yml //内容以下

•- include: copy.yml

•- include: install.yml

•到此两个roles:common和install就定义完成了,接下来要定义一个入口配置文件

•vim  /etc/ansible/nginx_install/install.yml  //内容以下

•---

•- hosts: testhost

•  remote_user: root

•  gather_facts: True

•  roles:

•    - common

•    - install

•执行: ansible-playbook /etc/ansible/nginx_install/install.yml

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

管理配置文件

• 生产环境中大多时候是须要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面咱们来写个管理nginx配置文件的playbook

• mkdir  -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

• 其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令

• 关于回滚,须要在执行playbook以前先备份一下旧的配置,因此对于老配置文件的管理必定要严格,千万不能随便去修改线上机器的配置,而且要保证new/files下面的配置和线上的配置一致

• 先把nginx.conf和vhosts目录放到files目录下面

• cd /usr/local/nginx/conf/

• cp -r nginx.conf vhost  /etc/ansible/nginx_config/roles/new/files/

• vim /etc/ansible/nginx_config/roles/new/vars/main.yml //定义变量

• nginx_basedir: /usr/local/nginx

• vim /etc/ansible/nginx_config/roles/new/handlers/main.yml  //定义从新加载nginx服务

•- name: restart nginx

•  shell: /etc/init.d/nginx reload

• vim /etc/ansible/nginx_config/roles/new/tasks/main.yml //这是核心的任务

•- name: copy conf file

•  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644

•  with_items:

•    - { src: nginx.conf, dest: conf/nginx.conf }

•    - { src: vhosts, dest: conf/ }

•  notify: restart nginx

• vim /etc/ansible/nginx_config/update.yml // 最后是定义总入口配置

•---

•- hosts: testhost

•  user: root

•  roles:

•  - new

• 执行: ansible-playbook /etc/ansible/nginx_config/update.yml

• 而回滚的backup.yml对应的roles为old

• rsync -av  /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/

• 回滚操做就是把旧的配置覆盖,而后从新加载nginx服务, 每次改动nginx配置文件以前先备份到old里,对应目录为/etc/ansible/nginx_config/roles/old/files

• vim /etc/ansible/nginx_config/rollback.yml // 最后是定义总入口配置

•---

•- hosts: testhost

•  user: root

•  roles:

•  - old

实例:

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

 

 

常见问题:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-first-book/

相关文章
相关标签/搜索