ansible安装和配置
一、安装ansible准备
//安装准备 .两台机器 172.7.15.106 172.7.15.111 .设置hostname以及hosts 172.7.15.106 web9.lulu.com 172.7.15.111 web10.lulu.com . 安装ansible (只需要在106--server端安装)
[root@web9 ~]# yum install -y epel-release
[root@web9 ~]# yum install -y ansible
二、安装ansible
// [root@web9 ~]# ssh-keygen -t rsa //直接回车生成密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //此处输入密码
Enter passphrase (empty for no passphrase): [root@web9 ~]# scp .ssh/id_rsa.pub 172.7.15.111:/root/ssh/authorized_keys
[root@web9 ~]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized.keys
[root@web9 ~]# chmod /root/.ssh/authorized.keys
[root@web9 ~]# setenforce
[root@web9 ~]# iptables -F
[root@web9 ~]# ssh web10.lulu.com
-bash: ssh: command not found
[root@web9 ~]# yum install -y openssh-clients
//连接并配对密钥 [root@web9 ~]# ssh web10.lulu.com
The authenticity of host 'web10.lulu.com(172.7.15.111)' can't be established.
RSA key fingerprint is .....
Are you sure you want to continue connecting (yes/no)? yes
三、远程执行命令
//先更改配置文件 [root@web9 ~]# vi /etc/ansible/hosts
//ADD
[testhost]
127.0.0.1
172.7.15.111 /* testhost --主机组名字 ,自定义 以下ip --组内的机器的ip
*/ [root@web9 ~]# ansible testhost -m command -a 'hostname'
127.0.0.1 | success | rc= >>
web9.lulu.com web10.lulu.com | success | rc= >>
web10.lulu.com /* testhost --主机组名字。自定义 -m + 模块名 -a +命令
*/
此处会遇到的错误 :
[root@web9 ~]# 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模块 [root@web9 ~]# ansible 'web10.lulu.com' -m shell -a 'hostname'
web10.lulu.com | success | rc= >>
web10.lulu.com [root@web9 ~]# ansible 'web10.lulu.com' -m shell -a 'cat /etc/passwd|grep root'
web10.lulu.com | success | rc= >>
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
四、拷贝目录或者文件
//--拷贝文件 [root@web9 ~]# ansible web10.lulu.com -m copy -a "src=/etc/passwd dest=/tmp/1.txt"
web10.lulu.com | success >>{
...
...
...
} /* 解释:
-m -- 模块选择 copy src 源文件 dest 目标文件
*/ [root@web9 ~]# ansible web10.lulu.com -m copy -a "src=/etc/passwd dest=/tmp/1.txt owner=root group=root mode=0755"
web10.lulu.com | success >> {
...
...
...
}
//--拷贝目录 [root@web9 ~]# ansible web10.lulu.com -m copy -a "src=/etc/ansible dest=/tmp/ansible"
web10.lulu.com | success >>{
...
...
...
} //client端检查是否拷贝成功并且与server端相同
[root@web9 ~]# ls /etc/ansible [root@web10 ~]# ls /tmp/ansible
五、远程执行shell脚本
[root@web9 ~]# vim /tmp/test.sh
//ADD
#!/bin/bash
echo `date` > /tmp/ansible_test.txt [root@web9 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755" //将脚本分发到各服务器上
[root@web9 ~]# ansible testhost -m shell -a "/bin/bash /tmp/test.sh"
//批量执行shell脚本 //shell模块还支持远程执行命令加管道符
[root@web9 ~]# ansible testhost -m shell -a "cat /etc/passwd|wc -l"
六、配置cron
//添加cron任务
[root@web9 ~]# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/123.txt' weekday=6"
/* name 任务名称
job 执行的命令
最后加时间
*/
//client端使任务生效
[root@web9 ~]# crontab -l
//删除cron任务
[root@web9 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
七、安装rpm包/管理服务
.
[root@web9 ~]# ansible web10.lulu.com -m yum -a "name=httpd"
/* 解释:
name = rpm包名
*/ //client端检查是否安装完成
[root@web10 ~]# yum list|grep httpd .
[root@web9 ~]# ansible testhost -m service -a "name=httpd state=started enabled=yes" ==
[root@web9 ~]# ansible testhost -m service -a "name=httpd state=stopped enabled=no" [root@web9 ~]# ansible testhost -m service -a "name=httpd state=started enabled=no" ==
[root@web9 ~]# ansible testhost -m service -a "name=httpd state=stopped enabled=yes"
//ansible 文档的使用 [root@web9 ~]# ansible-doc -l //列出所有的模块 [root@web9 ~]# ansible-doc cron //查看指定模块的文档
八、ansible--playbook
[root@web9 ~]# cd /etc/ansible
[root@web9 ansible]# ls
ansible.cfg hosts roles
[root@web9 ansible]# vi test.yml
//ADD
---
- hosts: testhost
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/fran.txt /*
hosts --指定哪些主机进行参作 user --指定 使用什么用户登录 远程主机操作 tasks -- 指定任务 */ //生效
[root@web9 ansible]# ansible-playbook test.yml
//创建用户 [root@web9 ansible]# vi create_user.yml
//ADD
---
- name: create_user
hosts: testhost
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="{{ user }}" /*
gather_facts --指定了以下任务部分执行前,是否先执行setup模块获取主机相关信息 变量值 -- 一定要 " " 引住 user -- 调用了user模块 */
playbook循环
[root@web9 ansible]# vi loop.yml
//ADD
---
- hosts: testhost
user: root
tasks:
- name: change mod for file
file: path=/tmp/{{ item }} mode=600 owner=root group=root
with_items:
- 1.txt
- 2.txt
- 3.txt
[root@web9 ansible]# ansible-playbook loop.yml
[root@web9 ansible]# touch /tmp/{1.txt,2.txt,3.txt}
//同时也在client端新创建文件
[root@web10 ~]# touch /tmp/{1.txt,2.txt,3.txt} //回到server
[root@web9 ansible]# ls -l /tmp/
-rw------- 1 root root 0 12月 24 20:50 1.txt
-rw------- 1 root root 0 12月 24 20:50 2.txt
-rw------- 1 root root 0 12月 24 20:50 3.txt
//同时也在client端查看
[root@web10 ~]# ls -l /tmp/
-rw------- 1 root root 0 12月 24 20:50 1.txt
-rw------- 1 root root 0 12月 24 20:50 2.txt
-rw------- 1 root root 0 12月 24 20:50 3.txt
playbook判断
[root@web9 ansible]# vi when.yml
//ADD
---
- hosts: testhost
remote_user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: facter_ipaddress == "172.7.15.106" [root@web9 ansible]# ansible web10.lulu.com -m setup
//check是否有
...
...
“facter_ipaddress": "172.7.15.111",
...
[root@web9 ansible]# ansible-playbook when.yml
[root@web9 ansible]# ls -lt /tmp/when.txt
-rw-r--r-- root root 12月 : /tmp/when.txt
playbook--handlers
/* 执行task任务之后,服务器发生变化之后-- 需执行一些操作
比如 修改配置文件后,---需要重启服务 */
[root@web9 ansible]# vi handlers.yml
//ADD
---
- name: handlers test
hosts: web10.lulu.com
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaa.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo "" >> /tmp/aaa.txt
[root@web9 ansible]# ansible-playbook handlers.yml
//client端检查
[root@web10 ~]# cat /tmp/aaa.txt
...
//最后一行
九、ansible实例 -- 安装nginx
[root@web9 ansible]# cd /etc/ansible
[root@web9 ansible]# mkdir nginx_install //创建一个装nginx各种需要文件的目录
[root@web9 ansible]# cd nginx_install
[root@web9 ansible]# mkdir -p roles/{common,install}/{handlers.files,meta,tasks,templates,vars} /* explain:
roles -- common(准备) -- handlers(当发生改变时),files(安装时用到) install(安装nginx) -- meta(说明信息,角色依赖等),tasks(核心配置) -- templates(存配置文件,启动脚本等模版) -- vars(定义的变量)
*/ /* 准备: 在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件 安装好---将nginx目录打包---放到/etc/ansible/nginx_install/roles/install/files ,名字取为nginx.tar.gz --启动脚本、配置文件需要放到/etc/ansible/nginx_install/roles/install/templates */
步骤://将需要的文件拷贝到新创建的目录中,方便管理
[root@web9 ansible]# cp /usr/local/nginx.tar.gz files/ [root@web9 ansible]# cp /usr/local/nginx/conf/nginx.conf templates/
[root@web9 ansible]# cp /etc/init.d/nginx templates
[root@web9 ansible]# vim nginx_install/roles/install/vars/main.yml
//ADD
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
[root@web9 ansible]# cd nginx_install/roles
[root@web9 roles]# vim ./common/tasks/main.yml
//ADD
- name: Install initializtion require software
yum: name={{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
- opensshl-devel //把要用到的文档拷贝到目标机器
[root@web9 ansible]# vim nginx_install/roles/install/tasks/copy.yml
//ADD
- 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 - name: Copy Nginx Config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode= //建立用户,启动服务,删除压缩包
[root@web9 ansible]# vim nginx_install/roles/install/tasks/install.yml
//ADD
- name: Create Nginx User
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin - name: Start Nginx Service
service: name= nginx state=restarted #这里是started的区别 - name: Add Boot Start Nginx Service
shell:chkconfig --level nginx on - name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz //再创建main.yml并且把copy和install调用
[root@web9 ansible]# cd nginx_install/roles/install/tasks [root@web9 tasks]# ls
copy.yml install.yml [root@web9 tasks]# vi main.yml
//ADD
- include: copy.yml
- include: install.yml //定义入口文件
[root@web9 tasks]# cd /etc/ansible/nginx_install/
[root@web9 nginx_install]# vi install/yml
//ADD
---
- hosts: testhost
remote_user: root
gather_facts: True
roles:
- common
- install [root@web9 nginx_install]# ansible-playbook install.yml //client check
[root@web10 ~]# rpm -qa|egrep 'pcre|openssl|zlib'
...
...
...
[root@web10 ~]# ls /usr/local/nginx
.. . ... ... .. ...
[root@web10 ~]# ps aux|grep nginx
.......
.....
.....
[root@web10 ~]# chkconfig --list nginx
nginx :关闭 :关闭 :关闭 :启用 :启用 :启用 :关闭
//管理配置文件
/* 生产环境中 -- 大多需要管理配置文件
安装环境包只是初始化环境需要使用
*/
[root@web9 ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
/* new --更新 old --回滚 files --存着nginx.conf and vhosts
handlers -- 重启nginx服务的命令
关于回滚 , 执行playbook前需要 备份一下 旧的配置,
老配置管理要严格--不能随便修改线上机器的配置
且保证new/files里的配置和线上的一致
*/
[root@web9 ~]# ccd /usr/local/nginx/conf
[root@web9 conf]# cp -r nginx.conf vhosts /etc/ansible/nginx_conf/roles/new/files/
[root@web9 conf]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml //定义变量
//ADD
nginx_basedir: /usr/local/nginx
[root@web9 conf]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml //定义重加载nginx服务
//ADD
- name: restart nginx
shell: /etc/init.d/nginx reload
[root@web9 conf]# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml //核心任务
//ADD
- name: copy conf file
copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=
with_items:
- { src: nginx.conf, dest: conf/nginx.conf }
- { src: vhosts, dest: conf/ }
notify: restart nginx
[root@web9 tasks]# vim /etc/ansible/nginx_config/update.yml //定义总入口配置
//ADD
---
- hosts: testhost
user: root
roles:
- new
[root@web9 tasks]# ansible-playbook /etc/ansible/nginx_config/update.yml
//测试
[root@web9 tasks]# vi roles/new/files/vhosts/.conf
//ADD
#sjadhjsahkd
[root@web9 tasks]# vi roles/new/files/nginx.conf
//ADD
...
//在末尾增加
include vhosts/*.conf; */
[root@web9 tasks]# ansible-playbook update.yml //client check
[root@web10 ~]# cat /usr/local/nginx/conf/vhosts/.conf
#sjadhjsahkd
[root@web10 ~]# ps aux|grep nginx
[root@web10 ~]# date //与date相比是否时间差不多,配置的时间 //同步数据
[root@web9 roles]# rsync -av new/ old/
sending incremental file list
...
[root@web9 roles]# cd ..
[root@web9 nginx_config]# cp update.yml backup.yml
[root@web9 nginx_config]# vi backup.yml
//change to
...
...
...
role:
- old [root@web9 nginx_config]# vi roles/new/files/vhosts/.conf
//ADD
...
#jhdjkahkdjs
[root@web9 nginx_config]# ansible-playbook update.yml
//client check
[root@web10 ~]# cat /usr/local/nginx/conf/vhosts/.conf
#sjadhjsahkd
#jhdjkahkdjs //recover data
[root@web9 nginx_config]# ansible-playbook backup.yml
//client check
[root@web10 ~]# cat /usr/local/nginx/conf/vhosts/.conf
#sjadhjsahkd //下载整个样例库
[root@web9 ~]# yum install -y git
[root@web9 ~]# git clone git://github.com/dh528888/ansible-examples.git
ansible安装和配置的更多相关文章
- 服务器批量管理软件ansible安装以及配置
1.yum安装(管理主机以及被管理主机都需要安装) yum install epel-release yum install ansible 2.配置管理主机 vim /etc/ansible/hos ...
- Ansible 安装与配置(一)
公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...
- 15.Ansible安装与配置简单版
Ansible是一个简单高效的自动化运维管理工具,用Python开发,能大批量管理N多台机器,可以并发的在多台机器上部署应用.安装软件.执行命令.配置和编排任务. 一.Ansible工作机制 从图中可 ...
- 1.Ansible安装以及配置
本节内容以Centos7为系统环境进行讲解: 1.安装epel源,方便直接yum安装: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun ...
- Ansible安装及配置
ansible分为以下几个部份: Ansible:核心引擎 Modules:包括 Ansible 自带的核心模块(core modules)及自定义模块 (custom modules): 核心模块: ...
- ansible安装、配置ssh、hosts、测试连接
.安装ansible 1.1.源码安装 源码安装参照 https://www.cnblogs.com/guxiong/p/7218717.html [root@kube-node3 ~]# .tar. ...
- Ansible安装 入门教程
learn一门新技术咯: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置 ...
- Ansible安装部署及常用模块详解
Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...
- Ansible安装配置Nginx
一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...
随机推荐
- 转:关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码
关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码 转: http://andyliuxs.iteye.com/blog/105174 ...
- [JSOI2010]缓存交换 贪心 & 堆
~~~题面~~~ 题解: 首先我们要使得Miss的次数尽量少,也就是要尽量保证每个点在被访问的时候,这个点已经存在于Cache中. 那么我们可以得到一个结论: 如果Cache已满,那么我们就从Cach ...
- POJ1066:Treasure Hunt——题解
http://poj.org/problem?id=1066 题目大意:给一个由墙围成的正方形,里面有若干墙,每次破墙只能从(当前看到的)墙的中点破,求最少破多少墙才能看到宝藏. —————————— ...
- BZOJ4589:Hard Nim——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4589 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和N ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- HDOJ.2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
悼念512汶川大地震遇难同胞--老人是真饿了 点我挑战题目 题目分析 每组数据给出所拥有的钱数,和大米的种类.每种大米给出单价(每单位重量)和大米的重量.求能买到的大米最大重量是多少? 采用贪心算法. ...
- Ark组件[转]
Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...
- POSIX.2 正则表达式
By francis_hao Oct 1,2017 这里的正则表达式主要是指扩展正则,也就是egrep(grep -e)用到的正则表达式. 字符 含义 类别说明 | 分割分支,正则表达式会去 ...
- Wireshark中TCP segment of a reassembled PDU的含义
By francis_hao Sep 16,2017 在用Wireshark抓包的时候,经常会看到TCP segment of a reassembled PDU,字面意思是要重组的协议数据 ...
- Centos6.5+Python2.7 +ffmpeg+opencv2自动安装脚本
今天安装opencv折腾了多个小时,为以后安装少走弯路,脚本安装 完整 脚本如下: #! /bin/bash sudo yum install -y gcc g++ gtk+-devel libjpe ...