Ansible的简单安装部署,ansible部署


Ansible工作机制

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

Ansible架构

Ansible工作原理

Ansible工作机制
ansible分为以下几个部份:

1> Control Node:控制机器
2> Inventory:主机清单,配置管理主机列表
3> Playbooks:剧本、任务编排。根据规则定义多个任务,模块组织结构清晰,由ansible自动执行。
4> ModulesCore | Custom):模块,用于执行某个具体的任务

5> connection plugin(连接插件):Ansible通过不同的协议连接到远程主机上,执行指定的命令。默认采用ssh协议连接远程主机。
安装Ansible

一台控制主机:172.25.53.11

两台管理主机:172.25.53.1
172.25.53.2


安装要求:

    控制服务器:需要安装 Python2.6/2.7
管理服务器:需要安装 Python2.4 以上版本,若低于 Python2.5 需要安装 pythonsimplejson; 若启用了 selinux,则需要安装 libselinux-python。
管理服务器为2.6.6

1.首先安装python模块,7.3版本虚拟机有自带python版本为2.7,所以我跳过了这一步

2.setuptools模块安装

[root@vm1 ~]# tar xvzf setuptools-7.0.tar.gz 
[root@vm1 ~]# cd setuptools-7.0
[root@vm1 setuptools-7.0]# python setup.py install

3.pycrypto模块安装

[root@vm1 ~]# tar xvzf pycrypto-2.6.1.tar.gz 
[root@vm1 ~]# cd pycrypto-2.6.1
[root@vm1 pycrypto-2.6.1]# python setup.py install

4.PyYAML模块安装

[root@vm1 ~]# tar xvzf yaml-0.1.3.tar.gz.tar.gz 

[root@vm1 yaml-0.1.3]# ./configure --prefix=/usr/local/
报错如下
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/yaml-0.1.3':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

解决办法
[root@vm1 yaml-0.1.3]# yum install -y gcc c++
再次编译
[root@vm1 yaml-0.1.3]# ./configure --prefix=/usr/local/
无报错
Make && make install
[root@vm1 ~]# tar zxvf PyYAML-3.11.tar.gz 
[root@vm1 ~]# cd PyYAML-3.11
[root@vm1 PyYAML-3.11]# python setup.py install
有报错


解决方法
[root@vm1 PyYAML-3.11]# yum install -y python-devel.x86_64

再次运行无报错

5.Jinja2模块安装

[root@vm1 ~]# tar zxfv Jinja2-2.7.3.tar.gz 
[root@vm1 ~]# cd Jinja2-2.7.3
[root@vm1 Jinja2-2.7.3]# python setup.py install
出错,尝试解决

首先,他说找不到本地包也下载不了,我们就先给他配网让他尝试下载
[root@foundation63 ~]# iptables -t nat -I POSTROUTING -s 172.25.53.0/24 -j MASQUERADE


[root@vm1 Jinja2-2.7.3]# yum install -y net-tools
[root@vm1 Jinja2-2.7.3]# route add default gw 172.25.53.250
[root@vm1 Jinja2-2.7.3]# vim /etc/resolv.conf 
添加解析
[root@vm1 Jinja2-2.7.3]# ping www.baidu.com
PING www.baidu.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=1 ttl=53 time=73.8 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=2 ttl=53 time=99.0 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=3 ttl=53 time=96.2 ms
再次执行
[root@vm1 Jinja2-2.7.3]# python setup.py install
无报错

6.paramiko模块安装

[root@vm1 ~]# tar zxvf ecdsa-0.11.tar.gz 
[root@vm1 ~]# cd ecdsa-0.11
[root@vm1 ecdsa-0.11]# python setup.py install


[root@vm1 ~]# tar zxfv paramiko-1.15.1.tar.gz 
[root@vm1 ~]# cd paramiko-1.15.1
[root@vm1 paramiko-1.15.1]# python setup.py install
这一步他有自动下载一些东西

7.simplejson模块安装

[root@vm1 ~]# tar zxfv simplejson-3.6.5.tar.gz 
[root@vm1 ~]# cd simplejson-3.6.5
[root@vm1 simplejson-3.6.5]# python setup.py install

8.ansible安装

[root@vm1 ~]# tar zxfv ansible-1.7.2.tar.gz 
[root@vm1 ~]# cd ansible-1.7.2
[root@vm1 ansible-1.7.2]# python setup.py install

至此ansible安装完成

ansible配置

两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下。

    ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径、模块、插件等配置信息

    hosts:机器清单,进行分组管理

[root@vm1 ~]# mkdir -p /etc/ansible
[root@vm1 ~]# find / -name ansible.cfg
/root/ansible-1.7.2/examples/ansible.cfg
/root/ansible-1.7.2/test/units/ansible.cfg
[root@vm1 ~]# cp /root/ansible-1.7.2/examples/ansible.cfg /etc/ansible/ansible.cfg
[root@vm1 ~]# find / -name hosts
/etc/hosts
/root/ansible-1.7.2/examples/hosts
[root@vm1 ~]# cp /root/ansible-1.7.2/examples/hosts /etc/ansible/hosts

配置管理主机

在hosts文件中添加管理主机的IP地址列表:

[root@vm1 ~]# vim /etc/ansible/hosts

在控制主机中生成ssh密钥对

将公钥拷贝到管理主机中.ssh/authorized_keys文件中,实现免密码登录远程管理主机
[root@vm1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.25.53.1
[root@vm1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.25.53.2

尝试连接

[root@vm1 ~]# vim /etc/ansible/ansible.cfg 

简易配置:
[defaults]
inventory      = /etc/ansible/hosts
sudo_user=root
remote_port=22
host_key_checking=False
remote_user=root
log_path=/var/log/ansible.log
module_name=command
private_key_file=/root/.ssh/id_rsa
no_log:True

# config file for ansible -- http://ansible.com/
# ==============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]   --->通用默认配置

# some basic default values...

inventory      = /etc/ansible/hosts     这个是默认库文件位置,脚本,或者存放可通信主机的目录
#library        = /usr/share/my_modules/   Ansible默认搜寻模块的位置
remote_tmp     = $HOME/.ansible/tmp   Ansible 通过远程传输模块到远程主机,然后远程执行,执行后在清理现场.在有些场景下,你也许想使用默认路径希望像更换补丁一样使用
pattern        = *    如果没有提供“hosts”节点,这是playbook要通信的默认主机组.默认值是对所有主机通信
forks          = 5    在与主机通信时的默认并行进程数 ,默认是5d
poll_interval  = 15    当具体的poll interval 没有定义时,多少时间回查一下这些任务的状态, 默认值是5秒
sudo_user      = root   sudo使用的默认用户 ,默认是root
#ask_sudo_pass = True   用来控制Ansible playbook 在执行sudo之前是否询问sudo密码.默认为no
#ask_pass      = True    控制Ansible playbook 是否会自动默认弹出密码
transport      = smart   通信机制.默认 值为’smart’。如果本地系统支持 ControlPersist技术的话,将会使用(基于OpenSSH)‘ssh’,如果不支持讲使用‘paramiko’.其他传输选项包括‘local’, ‘chroot’,’jail’等等
#remote_port    = 22    远程SSH端口。 默认是22
module_lang    = C   模块和系统之间通信的计算机语言,默认是C语言

# plays will gather facts by default, which contain information about
# the remote system.
#
# smart - gather by default, but don't regather if already gathered
# implicit - gather by default, turn off with gather_facts: False
# explicit - do not gather by default, must say gather_facts: True
gathering = implicit   控制默认facts收集(远程系统变量). 默认值为’implicit’, 每一次play,facts都会被收集

# additional paths to search for roles in, colon separated
#roles_path    = /etc/ansible/roles   roles 路径指的是’roles/’下的额外目录,用于playbook搜索Ansible roles

# uncomment this to disable SSH key host checking
#host_key_checking = False    检查主机密钥

# change this for alternative sudo implementations
sudo_exe = sudo     如果在其他远程主机上使用另一种方式执sudu操作.可以使用该参数进行更换

# what flags to pass to sudo   传递sudo之外的参数
#sudo_flags = -H

# SSH timeout    SSH超时时间
timeout = 10

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root   使用/usr/bin/ansible-playbook链接的默认用户名,如果不指定,会使用当前登录的用户名

# logging is off by default unless this path is defined
# if so defined, consider logrotate
#log_path = /var/log/ansible.log     日志文件存放路径

# default module name for /usr/bin/ansible
#module_name = command     ansible命令执行默认的模块

# use this shell for commands executed under sudo
# you may need to change this to bin/bash in rare instances
# if sudo is constrained
#executable = /bin/sh     在sudo环境下产生一个shell交互接口. 用户只在/bin/bash的或者sudo限制的一些场景中需要修改

# if inventory variables overlap, does the higher precedence one win
# or are hash values merged together?  The default is 'replace' but
# this can also be set to 'merge'.
#hash_behaviour = replace    特定的优先级覆盖变量

# list any Jinja2 extensions to enable here:
#jinja2_extensions = jinja2.ext.do,jinja2.ext.i18n      允许开启Jinja2拓展模块

# if set, always use this private key file for authentication, same as
# if passing --private-key to ansible or ansible-playbook
#private_key_file = /path/to/file         私钥文件存储位置

# format of string {{ ansible_managed }} available within Jinja2
# templates indicates to users editing templates files will be replaced.
# replacing {file}, {host} and {uid} and strftime codes with proper values.
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}   这个设置可以告知用户,Ansible修改了一个文件,并且手动写入的内容可能已经被覆盖.

# by default, ansible-playbook will display "Skipping [host]" if it determines a task
# should not be run on a host.  Set this to "False" if you don't want to see these "Skipping"
# messages. NOTE: the task header will still be shown regardless of whether or not the
# task is skipped.
#display_skipped_hosts = True     显示任何跳过任务的状态 ,默认是显示

# by default (as of 1.3), Ansible will raise errors when attempting to dereference
# Jinja2 variables that are not set in templates or action lines. Uncomment this line
# to revert the behavior to pre-1.3.
#error_on_undefined_vars = False      如果所引用的变量名称错误的话, 将会导致ansible在执行步骤上失败

# by default (as of 1.6), Ansible may display warnings based on the configuration of the
# system running ansible itself. This may include warnings about 3rd party packages or
# other conditions that should be resolved if possible.
# to disable these warnings, set the following value to False:
#system_warnings = True    允许禁用系统运行ansible相关的潜在问题警告

# by default (as of 1.4), Ansible may display deprecation warnings for language
# features that should no longer be used and will be removed in future versions.
# to disable these warnings, set the following value to False:
#deprecation_warnings = True     允许在ansible-playbook输出结果中禁用“不建议使用”警告

# (as of 1.8), Ansible can optionally warn when usage of the shell and
# command module appear to be simplified by using a default Ansible module
# instead.  These warnings can be silenced by adjusting the following
# setting or adding warn=yes or warn=no to the end of the command line
# parameter string.  This will for example suggest using the git module
# instead of shelling out to the git command.
# command_warnings = False    当shell和命令行模块被默认模块简化的时,Ansible 将默认发出警告


# set plugin path directories here, separate with colons
action_plugins     = /usr/share/ansible_plugins/action_plugins  
callback_plugins   = /usr/share/ansible_plugins/callback_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins     = /usr/share/ansible_plugins/lookup_plugins
vars_plugins       = /usr/share/ansible_plugins/vars_plugins
filter_plugins     = /usr/share/ansible_plugins/filter_plugins

# by default callbacks are not loaded for /bin/ansible, enable this if you
# want, for example, a notification or logging callback to also apply to
# /bin/ansible runs
#bin_ansible_callbacks = False    用来控制callback插件是否在运行 /usr/bin/ansible 的时候被加载. 这个模块将用于命令行的日志系统,发出通知等特性


# don't like cows?  that's unfortunate.
# set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1
#nocows = 1    默认ansible可以调用一些cowsay的特性   开启/禁用:0/1

# don't like colors either?
# set to 1 if you don't want colors, or export ANSIBLE_NOCOLOR=1
#nocolor = 1  输出带上颜色区别, 开启/关闭:0/1

测试

测试下在三台管理机器批量执行一个ping命令
ansible all -mping

查看在线时长
ansible storm_cluster -m command -a ‘uptime’

查看远程主机的基本信息

还有很多可以玩的,这里就不一一测试了

此次作业参考了博客:https://blog.csdn.net/u014641018/article/details/53764047及http://blog.51cto.com/191226139/2066936

相关内容

    暂无相关文章