Ansible文档,自动化运维工具Ansible

一、基础介绍

1、简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

2、总体架构

3、特性

(1)、no agents:不需要在被管控主机上安装任何客户端;

(2)、no server:无服务器端,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

(4)、yaml,not code:使用yaml语言定制剧本playbook;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。

4、优点

(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;

(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;

(3)、使用python编写,维护更简单,ruby语法过于复杂;

(4)、支持sudo。

5、任务执行流程

二、Ansible基础安装

1、Ansible安装

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

# vim epel.repo #替换https为http

# yum install ansible

节点[如果没有使用mysql_db模块,则无需安装]

# yum install MySQL-python

2、节点配置

ssh-keygen #192.168.0.220生成证书,不用设置密码,一直回车

ls /root/.ssh/

id_rsa id_rsa.pub known_hosts

#将IP添加到操作列表

vim /etc/ansible/hosts

[bgo]

192.168.0.211 ansible_ssh_port=22

IP或者域名 SSH服务端口,默认22

#测试

ansible bgo -m ping

三、Ansible操作说明

1、ansible 远程执行命令

ansible bgo -m command -a 'hostname' #command命令模块,不支持管道过滤

ansible bgo -m shell -a 'cat /etc/passwd |grep root' #shell命令模块,支持管道过滤

注意:建议使用shell命令模块

2、ansible拷贝文件或目录

[root@master-ansible-220 ~]# ansible bgo -m copy -a "src=/etc/passwd dest=/tmp/passwd owner=root group=root mode=0644"

ansible bgo -m copy -a "src=/tmp/123/ dest=/tmp/456 owner=root group=root mode=0755"

 

3、ansible 远程执行脚本

首先创建一个shell脚本

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

#!/bin.bash

d=`date`

echo $d > /tmp/an_test.log

 

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

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

最后是批量执行该shell脚本

ansible bgo -m shell -a "/bin/bash /tmp/test.sh"

4、ansible实现任务计划

ansible bgo -m cron -a "name='test cron' job='/bin/bash /tmp/test.sh' weekday6"

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

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

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

5、ansible安装rpm包管理服务

ansible bgo -m yum -a "name=httpd"

在name后面还可以加上state=install

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

这里的name是centos系统里的服务名,可以通过chkconfig --list查到

Ansible文档的使用

ansible-doc -l #列出所有的模块

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

6、ansible playbook的使用

相当于把模块写入到配置文件里面,例:

cat /etc/ansible/test.yml

---

- hosts:bgo

remote_user:root

tasks:

- name:test_playbook

shell:touch /tmp/test.log

 

说明:hosts参数指定了对哪些机器进行操作;

user参数指定了使用什么用户登录远程主机操作;

tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。

执行:ansible-playbook test.yml

再来一个创建用户的例子:

cat /etc/ansible/create_user.yml

---

- name: create_user

hosts: 192.168.0.211

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变量的值。

7、ansible playbook 中的循环

---

- hosts: bgo

user: root

tasks:

- name: change mod for file

file: path=/tmp/{{ item }} mode=600 owner=root group=root

with_items:

- 1.txt

- 2.txt

8、ansible playbook 判断

---

- hosts: bgo

user: root

gather_facts: True

tasks:

- name: user when

shell: touch /tmp/when.txt

when: facter_ipaddress == "192.168.0.211"

9、ansible playbook 中的handlers

执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务

---

- hosts: bgo

remote_user: root

tasks:

- name: test copy

copy: src=/tmp/1.txt dest=/tmp/2.txt

notify: test handlers

handlers:

- name: test handlers

shell: echo "121212" >> /tmp/2.txt

 

说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样

的,并不会去执行handlers里面的shell相关命令。这种比较适合配置文件发生更改后,重启服务器的操作。

四、远程执行脚本

1、weblogic服务重启

---

- hosts: 192.168.0.234

remote_user: cudo

tasks:

- name: test_playbook

shell: /bin/bash /home/cudo/scripts/yeah100_web_local.sh

执行命令:ansible-playbook sed.yml 234.yml

2、替换文件内容(向yml文件传递参数)

---

- hosts: 192.168.0.236

remote_user: cudo

tasks:

- name: test_playbook

shell: /bin/bash /home/cudo/sed.sh '{{ a }}' '{{ b }}' '{{ c }}'

注意:shell: '{{ a }}' '{{ b }}' '{{ c }}' 为参数,因为远程执行shell脚本时,脚本里定义了$1 $2 $3,所以要传递参数

执行命令:ansible-playbook sed.yml --extra-vars "a=aaa b=111 c=/tmp/123"

五、以下是自动化批量部署安装服务

1、ansible安装(nginx、tomcat、mysql)

在一个IP上,安装一个NGINX,一个TOMCAT,一个MYSQL

Nginx 的 80 口反向代理到 tomcat 的 8080口

Mysql里面有db : students , table : profile, 两个字段: name , age

Insert 一行 到mysql ,”Teddy”, “12”

2、构建目录结构

mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}

● defaults 默认寻找路径

● tasks 存放playbooks路径

● files 存放文件和脚本,copy模块文件搜索路径

● templates 模版存放路径

● handlers notify调用部分playbook存放路径

● vars roles内变量存放路径

3、文件结构

# tree /ansible/

/ansible/

├── roles

│ ├── db

│ │ ├── defaults

│ │ ├── files

│ │ │ └── stu.sql #要导入的sql

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ │ └── main.yml #创建数据库和导入sql

│ │ ├── templates

│ │ └── vars

│ ├── mysql

│ │ ├── defaults

│ │ ├── files

│ │ │ ├── mysql-5.6.27.tar.gz

│ │ │ └── mysql_install.sh #mysql源码安装脚本

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ │ └── main.yml #安装mysql

│ │ ├── templates

│ │ └── vars

│ ├── nginx

│ │ ├── defaults

│ │ ├── files

│ │ │ ├── install_nginx.sh #nginx安装脚本

│ │ │ └── nginx-1.8.0.tar.gz

│ │ ├── handlers

│ │ ├── meta

│ │ ├── tasks

│ │ │ └── main.yml #安装nginx

│ │ ├── templates

│ │ └── vars

│ └── tomcat

│ ├── defaults

│ ├── files

│ │ ├── apache-tomcat-7.0.65.tar.gz

│ │ ├── tomcat-initscript.sh #tomcat的init管理脚本

│ │ └── tomcat-users.xml #tomcat配置文件

│ ├── handlers

│ │ └── main.yml #安装后处理

│ ├── meta

│ ├── tasks

│ │ └── main.yml #安装tomcat

│ ├── templates

│ │ └── tomcat-users.xml #tomcat配置文件模版

│ └── vars

└── web.yml #总调用文件

4、playbooks & shell

/ansible/web.yml

- hosts: bgo

remote_user: root

roles:

- nginx

- mysql

- tomcat

- db

注意:在roles: # - nginx (#)为注释,不安装nginx

/ansible/roles/db/tasks/main.yml

---

- name: create db

mysql_db: name=student state=present login_password=bingoclo123 login_user=root login_unix_socket=/data/mysql/data/mysql.sock

- name: copy sql file

copy: src=stu.sql dest=/tmp

- name: import sql

mysql_db: name=student state=import target=/tmp/stu.sql login_password=bingoclo123 login_user=root login_unix_socket=/data/mysql/data/mysql.sock

/ansible/roles/db/files/stu.sql

create table profile(name varchar(20),age tinyint);

insert into profile(name,age) values('teddy',12);

/ansible/roles/nginx/tasks/main.yml

- name: copy nginx_tar_gz to client

copy: src=nginx-1.8.0.tar.gz dest=/tmp/nginx-1.8.0.tar.gz

- name: copy install_shell to client

copy: src=install_nginx.sh dest=/tmp/install_nginx.sh

- name: install nginx

shell: /bin/bash /tmp/install_nginx.sh

/ansible/roles/mysql/tasks/main.yml

- name: copy mysql_tar_gz to client

copy: src=mysql-5.6.27.tar.gz dest=/tmp/mysql-5.6.27.tar.gz

- name: copy install_script to client

copy: src=mysql_install.sh dest=/tmp/mysql_install.sh owner=root group=root mode=755

- name: install mysql

shell: /bin/bash /tmp/mysql_install.sh

/ansible/roles/tomcat/tasks/main.yml

- name: install java

yum: name=java-1.7.0-openjdk state=present

- name: group

group: name=tomcat

- name: user

user: name=tomcat group=tomcat home=/usr/tomcat

sudo: True

- name: copy tomcat_tar_gz

copy: src=apache-tomcat-7.0.65.tar.gz dest=/tmp/apache-tomcat-7.0.65.tar.gz

- name: Extract archive

command: /bin/tar xf /tmp/apache-tomcat-7.0.65.tar.gz -C /opt/

- name: Symlink install directory

file: src=/opt/apache-tomcat-7.0.65/ dest=/usr/share/tomcat state=link

- name: Change ownership of Tomcat installation

file: path=/usr/share/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes

- name: Configure Tomcat users

template: src=tomcat-users.xml dest=/usr/share/tomcat/conf/

notify: restart tomcat

- name: Install Tomcat init script

copy: src=catalina.sh dest=/etc/init.d/tomcat mode=0755

- name: Start Tomcat

service: name=tomcat state=started enabled=yes

/ansible/roles/tomcat/handlers/main.yml

- name: restart tomcat

service: name=tomcat state=restarted

/ansible/roles/nginx/files/install_nginx.sh

#!/bin/bash

yum -y install zlib zlib-devel openssl openssl-devel pcre-devel gcc*

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

cd /tmp

tar xf nginx-1.8.0.tar.gz;cd nginx-1.8.0

mkdir /var/run/nginx/;chown nginx.nginx /var/run/nginx/

./configure \

--prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--pid-path=/var/run/nginx/nginx.pid \

--user=nginx \

--group=nginx \

--with-http_ssl_module

make && make install

sed "/^\s*index / i proxy_pass http://localhost:8080;" /etc/nginx/nginx.conf

/usr/sbin/nginx &

/ansible/roles/mysql/files/mysql_install.sh

#!/bin/bash

DBDIR='/data/mysql/data'

PASSWD='bingoclo123'

[ -d $DBDIR ] || mkdir $DBDIR -p

yum install cmake make gcc-c++ bison-devel ncurses-devel -y

id mysql &> /dev/null

if [ $? -ne 0 ];then

useradd mysql -s /sbin/nologin -M

fi

chown -R mysql.mysql $DBDIR

cd /tmp/

tar xf mysql-5.6.27.tar.gz

cd mysql-5.6.27

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=$DBDIR \

-DMYSQL_UNIX_ADDR=$DBDIR/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DEXTRA_CHARSETS=all \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_READLINE=1 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EMBEDDED_SERVER=1

if [ $? != 0 ];then

echo "cmake error!"

exit 1

fi

make && make install

if [ $? -ne 0 ];then

echo "install mysql is failed!" && /bin/false

fi

sleep 2

ln -s /usr/local/mysql/bin/* /usr/bin/

cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chmod 700 /etc/init.d/mysqld

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$DBDIR --user=mysql

if [ $? -ne 0 ];then

echo "install mysql is failed!" && /bin/false

fi

/etc/init.d/mysqld start

if [ $? -ne 0 ];then

echo "install mysql is failed!" && /bin/false

fi

chkconfig --add mysqld

chkconfig mysqld on

/usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"

/usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"

/usr/local/mysql/bin/mysql -e "delete from mysql.user where password='';"

/usr/local/mysql/bin/mysql -e "flush privileges;"

if [ $? -eq 0 ];then

echo "ins_done"

fi

5、执行安装 & 检查

cd /ansible

ansible-playbook web.yml --syntax-check #检查语法

ansible-playbook web.yml #执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值