一、安装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安装和配置的更多相关文章

  1. 服务器批量管理软件ansible安装以及配置

    1.yum安装(管理主机以及被管理主机都需要安装) yum install epel-release yum install ansible 2.配置管理主机 vim /etc/ansible/hos ...

  2. Ansible 安装与配置(一)

    公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...

  3. 15.Ansible安装与配置简单版

    Ansible是一个简单高效的自动化运维管理工具,用Python开发,能大批量管理N多台机器,可以并发的在多台机器上部署应用.安装软件.执行命令.配置和编排任务. 一.Ansible工作机制 从图中可 ...

  4. 1.Ansible安装以及配置

    本节内容以Centos7为系统环境进行讲解: 1.安装epel源,方便直接yum安装: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun ...

  5. Ansible安装及配置

    ansible分为以下几个部份: Ansible:核心引擎 Modules:包括 Ansible 自带的核心模块(core modules)及自定义模块 (custom modules): 核心模块: ...

  6. ansible安装、配置ssh、hosts、测试连接

    .安装ansible 1.1.源码安装 源码安装参照 https://www.cnblogs.com/guxiong/p/7218717.html [root@kube-node3 ~]# .tar. ...

  7. Ansible安装 入门教程

    learn一门新技术咯: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置 ...

  8. Ansible安装部署及常用模块详解

    Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...

  9. Ansible安装配置Nginx

    一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...

随机推荐

  1. SocketServer-实现并发处理2

    转发自MnCu的博客: http://www.cnblogs.com/MnCu8261/p/5546823.html python基础之socket与socketserver ---引入 Socket ...

  2. 【题解】Atcoder ARC#85 E-MUL

    ……没啥可说的.最大权闭合子图,跑下dinic就好了…… #include <bits/stdc++.h> using namespace std; #define maxn 500000 ...

  3. [洛谷P4688][Ynoi2016]掉进兔子洞

    题目大意:给定一个$n(n\leqslant10^5)$序列,$m(m\leqslant10^5)$个询问,每个询问给出$l_1,r_1,l_2,r_2,l_3,r_3$.令$s$为该三个区间的交集的 ...

  4. POJ2942:Knights of the Round Table——题解

    http://poj.org/problem?id=2942 所写的tarjan练习题最难的一道. 说白了难在考得不是纯tarjan. 首先我们把仇恨关系处理成非仇恨关系的图,然后找双连通分量,在双连 ...

  5. some interesting words

    No one gets rich betting against the market. Never bet against the Fed. Bulls make money, bears make ...

  6. CodeBlocks调试功能快捷教程

    在程序设计中,单步调试能够跟踪程序的执行流程.跟踪过程中,还可以观察变量的变化,从而发现其中存在的问题.单步执行除了可以帮助我们发现设计的程序中存在的问题,对于初学者,还可以帮助我们理解语言的机制. ...

  7. C#学习之泛型继承和静态成员

    想要理解这里有必要先将泛型类学习充分.这里讲解的是泛型类继承类的类型和静态成员. 在前面C#学习之泛型中,创建的LinkList<T>类实现了IEnumerable<T>接口. ...

  8. Linux查看内核和系统版本

    1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe.d ...

  9. SqlDataAdapter 用法详解

    SqlCommand是sql命令,执行后通过sqlDataAdapter返回填入DataSet SqlDataAdapter   有不同的构造函数, SqlDataAdapter(SqlCommand ...

  10. String、StringBuffer、StringBuilder区分和性能比较

    转载自:http://www.cnblogs.com/fancydeepin/archive/2013/04/23/min-snail-speak_String-StringBuffer-String ...