您的位置:首页 > 其它

ansible

2016-02-25 18:38 411 查看
Ansible特点

不需要安装客户端,通过sshd去通信
基于模块工作,模块可以由任何语言开发
不仅支持命令行使用模块,也支持编写yaml格式的playbook
支持sudo
有提供ui(浏览器图形化)www.ansible.com/tower 10台主机以内免费
开源ui https://github.com/alaxli/amsible_ui文档 http://download.csdn.net/detail/liyang23456/7741185

采集信息
ansible web2.bbs.com -m setup

安装

两台机器 192.168.1.122 192.168.1.124
只需要在122上安装ansible就好了
yum install -y epel-release
yum install -y ansible

配置秘钥
106上生成秘钥队
ssh-keygen -t rsa 直接回车即可,不用设置秘钥密码
把公钥(id_rsa.pub)内容放到对方机器124的/root/.ssh/authorized_keys里面
scp .ssh/id_rsa.pub 192.168.1.124:/root/.shh/authorized_keys
本机也要操作
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
关闭selinux
setenforce 0

yum install -y openssh*
ssh web2.bbs.com

远程执行命令

vim /etc/ansible/host
[testhosts]
127.0.0.1
192.168.1.124
说明testhost为主机组名字,自定义 下面两个ip为组内的机器ip

ansible testhost -m command -a 'w'
这样就可以批量执行命令了 这里的testhost为主机名,-m后面是模块名字,-a后面就是命令,当然也可以直接些一个ip,针对某一台机器来执行命令

ansible 127.0.0.1 -m command -a 'hostname'
错误 如果出现 “msg”Aborting,target........
解决 yum install -y libselinux-python
还有一个模块就是shell同样也可以实现
ansible testhost -m shell -a 'w'

不行command
[root@web1 ~]# ansible testhosts -m command -a 'cat /etc/passwd|grep root'
192.168.1.124 | FAILED | rc=1 >>
cat: /etc/passwd|grep: No such file or directory
cat: root: No such file or directory

127.0.0.1 | FAILED | rc=1 >>
cat: /etc/passwd|grep: No such file or directory
cat: root: No such file or directory

可以 shell
[root@web1 ~]# ansible testhosts -m shell -a 'cat /etc/passwd|grep root'
192.168.1.124 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

127.0.0.1 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

拷贝文件或目录
ansible testhosts -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
注意:源目录会放到目标目录下面去,如果目标指定的目录不存在它会自动创建,如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名,但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面

ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"
这里的/tmp/123和源机器上的/etc/passwd是一致的,但是如果目标机器上已经有/tmp/123目录,则不会再/tmp/123目录下面建立passwd文件

远程执行脚本
首先创建一个shell脚本

vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_lest.txt
然后把脚本分发到各个机器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最后是批量执行该脚本
ansible testhost -m shell -a "/bin/bash /tmp/test.sh"
shell 模块,还支持远程执行命令并且带管道符
ansible testhost -m shell -a "cat /etc/passwd|wc -l"

实现任务计划
ansible testhost -m cron -a "name=test cron' job='/bin/touch /tmp/1212.txt' weekday=6"

ansible testhost -m cron -a "name='test_cron' job='/bin/bash /usr/local/sbin/1.sh' weekday=6"
若要删除该cron只需要加一个字段 stat=absent
ansible testhost -m cron -a "name='test_cron' state=absent"
其他时间表示: 分钟minute 小时hout 日期day 月份month

安装rpm包/管理服务
ansible testhost -m yum -a "name=httpd"
在name后面还可以加上state-installed
ansible testhost -m service -a "name=httpd state=started enabled=yes"
这里的name是centos系统里面的服务名可以通过chkconfig --list查到
Ansible文档的使用
ansible-doc -l 列出所有的模块
ansible-doc cron 查看指定模块的文档

playbook的使用 相当于linux里面的shell

相当于把模块写到配置文件里面,
cat /etc/ansible/test.yml
---
- hosts: testhost
remote_user:root
tasks:
- name: test_playbook
shell:touch /tmp/bbs.txt
说明 hosts参数指定了对那些主机进行操作
user参数指定了使用什么用户登录远程主机操作
tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来
执行:ansible-playbook test.yml

vim user.yml
---
- name: create_user
hosts: web2.bbs.com
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变量的值

ansible-playbook user.yml

playbook中的循环
两台机器上都要有1.txt 2.txt文件才行
---
hosts: testhost
user: root
task:
- name: change mod for file
file: path=/tmp/` item ` mode=600 owner=root group=root
with_items:
- 1.txt
- 2.txt

playbook判断
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: facter_ipaddress == "192.168.1.122"
~

playbook中的handlers
执行task之后,服务其发生变化之后要执行一些操作,比如修改了配置文件后,需要重启一下服务
---
- hosts: testhost
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相关命令,这种比较适合配置文件发生更改后,重启服务的操作

Ansible 安装nginx
思路; 先在一台机器上编辑安装好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@web1 tasks]# pwd
/etc/ansible/nginx_install/roles/common/tasks
[root@web1 tasks]# ls
main.yml

cd /tec/ansible/nginx_install/roles
定义commond的tasks,nginx是需要一些依赖包的
vim ./common/tasks/main.yml 内容如下
- name: Install initaliztion require software
yum: name=` item ` state=installed
with_items:
- zlib-devel
- pcre-devel
- openssl-devel

定义变量

[root@web1 roles]# cd install/
[root@web1 install]# ls
vars
[root@web1 install]# mkdir tasks files templates
[root@web1 install]# ls
files tasks templates vars
[root@web1 install]# cp /usr/local/nginx.tar.gz files/
[root@web1 install]# cp /usr/local/nginx/conf/nginx.conf templates/
[root@web1 install]# cp /etc/init.d/nginx templates/
[root@web1 install]# vi vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

vim 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
vim install.yml
- name: Create Nginx User
user: name=` nginx_user ` state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
service: name=nginx state=restarted
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz

再创建main.yml并且把copy和install调用

vim 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

管理配置文件

生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。
下面我们来写个管理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 vhosts /etc/ansible/nginx_conf/roles/new/files/

[root@web1 ansible]# mkdir nginx_config
[root@web1 ansible]# cd nginx_config/
[root@web1 nginx_config]# mkdir roles
[root@web1 nginx_config]# cd roles/
[root@web1 roles]# mkdir old new
[root@web1 roles]# cd new/
[root@web1 new]# mkdir vars files tasks handlers
[root@web1 new]# cp /usr/local/nginx/conf/nginx.conf files/
[root@web1 new]# cp -r /usr/local/nginx/conf/vhosts files/
[root@web1 new]# vim vars/main.yml
[root@web1 new]# vim handlers/main.yml
- name: restart nginx
shell: /etc/init.d/nginx reload

[root@web1 new]# vim 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

[root@web1 new]# cd ..
[root@web1 roles]# cd ..
[root@web1 nginx_config]# vim update.yml
---
- hosts: testhost
user: root
roles:
- new

[root@web1 nginx_config]# ansible-playbook update.yml

回滚 再更改之前把new里面的files 复制到 old/files下
[root@web1 roles]# rsync -av new/files/ old/files/
[root@web1 nginx_config]# cp update.yml backup.yml
[root@web1 nginx_config]# vim backup.yml
[root@web1 nginx_config]# vim backup.yml
---
- hosts: testhost
user: root
roles:
- old

[root@web1 nginx_config]# ansible-playbook update.yml
如果出现异常那么

[root@web1 nginx_config]# ansible-playbook backup.yml

git clone git://github.com/dl528888/ansible-examples.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ansible