ansible五

一、基础简介

1、Ansible简介

Ansible是一款基于Python开发的自动化运维工具,主要是实现批量系统配置、批量程序部署、批量运行命令、批量执行任务等等诸多功能。Ansible是一款灵活的开源工具,能够很大程度简化运维中的配置管理与流程控制方式,它利用推送方式对客户系统加以配置,这样所有工作都可在主服务器端完成。Asible是基于模块工作的,其本身没有批量部署的能力,总之只要明白Ansible是一款运维自动化的神器就好了~!

2、功能特性

wKiom1U5ETizSQtfAAGN9PZOd1E031.jpg

#######################################################################

3、工作机制

wKioL1U5EcDxtNOhAAKYzdCorKY259.jpg

4、特性

(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:可实现多级指挥。

5、优点
(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。

##############################以上为网络摘取信息###########################

二、安装

##########################################

注:本文的演示环境

Ansible 主机:CentOS 6.5 x64   

两台测试机:CentOS 6.5 x64  

 

已关闭 iptables

已关闭 selinux

 

已安装组件:Development tools,Server Platform Development

##########################################

1、默认系统是没有Ansible的yum源的,需要添加epel源

wKiom1U5F0Hwb_xzAAFxZ9c3EZA809.jpg

2、由于源不好找,找了半天在同门博客找到了,顺便做个链接,http://wdllife.blog.51cto.com/6615958/1631964(快乐就好),于是转载过来了,使用的是aliyun的epel源.

[root@lvs03]# vim /etc/yum.repos.d/CentOS-Base.repo

wKiom1U5HSOQGFo9AAKKzXbZjiU067.jpg

[root@lvs03]# yum install ansible   //阿里云的包里面的软件还是很新的,另外由于它是python开发的,依赖基本也是python相关的包

wKioL1U5Hvmy8Wy6AASqhJCpXzQ923.jpg

[root@lvs03]# rpm -ql ansible    //主要文件说明
/etc/ansible/ansible.cfg  //主配置文件
/etc/ansible/hosts  //主机分组定义库
/usr/bin/ansible-doc  //获取ansible内部信息的文档
/usr/bin/ansible-vault  //加密存放 /usr/bin/ansible-playbook 读取时解密/usr/bin/ansible-playbook 文件

3、打开Ansible 主机分组定义库,配置测试主机

wKiom1U5owaT6l6TAAIFS7j_RYY983.jpg

4、测试配置好的节点是否能正常运行,这里提示SSH权限问题,如图

说明:第一次运行时,需要输入一下“yes”'进行公钥验证',后续无需再次输入。

wKioL1U5qM3SaaS8AAKVYLwYb34654.jpg

于是加一个命令参数 "-k" 

[root@lvs03 ansible]# ansible host -m command -a 'date' -k

wKioL1U5qjPiAv-UAAGDy8fAu5M687.jpg

上面提示需要安装sshpass

[root@lvs03 ansible]# yum install -y sshpass

安装好sshpass后在进行测试

wKiom1U5qbCwdMWrAADwoQF_nSY827.jpg

5、到这里我们对Ansible参数进行下讲解

Usage:ansible <host-pattern> [options]
Options:

-m MODULE_NAME,--module-name=MODULE_NAME  //要执行的模块,默认为command

-a MODULE_ARGS,--args=MODULE_ARGS  //模块的参数

-u REMOTE_USER,--user=REMOTE_USER  //ssh连接的用户名,默认用root,ansible.cfg 中可以配置

-k,--ask-pass  //提示输入ssh登录密码,当使用密码验证登录的时候用

-s,--sudo  //sudo运行

-U SUDO_USER,--sudo-user=SUDO_USER  //sudo到哪个用户,默认为root

-K,--ask-sudo-pass  //提示输入sudo密码,当不是NOPASSWD模式时使用

-B SECONDS,--background=SECONDS  //runasynchronously,failingafterXseconds(default=N/A)

-P POLL_INTERVAL,--poll=POLL_INTERVAL  //setthepollintervalifusing-B(default=15)

-C,--check  //只是测试一下会改变什么内容,不会真正去执行

-c CONNECTION  //连接类型(default=smart)

-f FORKS,--forks=FORKS  //fork多少个进程并发处理,默认5

-i INVENTORY,--inventory-file=INVENTORY  //指定hosts文件路径,默认default=/etc/ansible/hosts

-l SUBSET,--limit=SUBSET  //指定一个pattern,对<host_pattern>已经匹配的主机中再过滤一次

--list-hosts  //只打印有哪些主机会执行这个playbook文件,不是实际执行该playboo

-M MODULE_PATH,--module-path=MODULE_PATH  //要执行的模块的路径,默认为/usr/share/ansible/

-o,--one-line  //压缩输出,摘要输出

--private-key=PRIVATE_KEY_FILE  //私钥路径

-T TIMEOUT,--timeout=TIMEOUT  //ssh连接超时时间,默认10秒

-t TREE,--tree=TREE  //日志输出到该目录,日志文件名会以主机名命名

-v,--verbose  //verbose mode(-vvv for more,-vvvv to enable connection debugging)

wKiom1U5sNyiq8xEAACZ5aL6gS4904.jpg

wKiom1U5sTjT-Mz6AAC4FM_8kv0990.jpg

6、免密钥执行命令(不过这样的话极为不安全的)

注:hosts文件内部支持的一些特定指令(inventory参数)

ansible_ssh_host:指定主机别名对应的真实IP,如:251ansible_ssh_host=183.60.41.251,随后连接该主机无须指定完整IP,只需指定251就行

ansible_ssh_port:指定连接到这个主机的ssh端口,默认22

ansible_ssh_user:连接到该主机的ssh用户

ansible_ssh_pass:连接到该主机的ssh密码(连-k选项都省了),安全考虑还是建议使用私钥或在命令行指定-k选项输入

ansible_sudo_pass:sudo密码

ansible_sudo_exe(v1.8+的新特性):sudo命令路径

ansible_connection:连接类型,可以是local、ssh或paramiko,ansible1.2之前默认为paramiko

ansible_ssh_private_key_file:私钥文件路径

ansible_shell_type:目标系统的shell类型,默认为sh,如果设置csh/fish,那么命令需要遵循它们语法

ansible_python_interpreter:python解释器路径,默认是/usr/bin/python,但是如要要连*BSD系统的话,就需要该指令修改python路径

ansible_*_interpreter:这里的"*"可以是ruby或perl或其他语言的解释器,作用和ansible_python_interpreter类似

wKiom1U5sv3R7WIBAAIRuuYaZCk780.jpg

wKioL1U5tSOgOCE-AAEr6G6PvaE636.jpg

7、查看测试主机的相关信息(举例)

wKioL1U5t32SqhupAAGoypOUz6c644.jpg

8、查看Ansible相关的模块,通过“ansible-doc -l ” 查看Ansible所有的模块

[root@lvs03 ansible]# ansible-doc -l
less 436
Copyright (C) 1984-2009 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
a10_server                    Manage A10 Networks AX/SoftAX/Thunder/vThunder...
a10_service_group             Manage A10 Networks AX/SoftAX/Thunder/vThunder...
a10_virtual_server            Manage A10 Networks AX/SoftAX/Thunder/vThunder...
acl                           Sets and retrieves file ACL information.      
add_host                      add a host (and alternatively a group) to the ...
airbrake_deployment           Notify airbrake about app deployments         
alternatives                  Manages alternative programs for common comman...
apache2_module                enables/disables a module of the Apache2 webse...
apt                           Manages apt-packages                          
apt_key                       Add or remove an apt key                      
apt_repository                Add and remove APT repositories               
apt_rpm                       apt_rpm package manager                       
assemble                      Assembles a configuration file from fragments 
assert                        Fail with custom message                      
at                            Schedule the execution of a command or script ...
authorized_key                Adds or removes an SSH authorized key         
azure                         create or terminate a virtual machine in azure
bigip_facts                   Collect facts from F5 BIG-IP devices          
bigip_monitor_http            Manages F5 BIG-IP LTM http monitors           
bigip_monitor_tcp             Manages F5 BIG-IP LTM tcp monitors 

(由于模块太多, 不一一例举了)
(如果查看某个“模块”相关信息,可以 “ansible-doc 模块名” ) 
例如:ansible-doc user 可以查看user这个模块的相关信息

9、使用模块的举例

例如:使用user模块为两台测试机添加账号及密码

wKiom1U5uzCAYcFyAAINgGKMgyg263.jpg

 

查看远程测试机是否添加成功

wKioL1U5vOvSqpN3AADnsY1QTLc683.jpg

wKioL1U5vR6waMZ0AADI5f5MSU8809.jpg

 

复制(copy模块)的举例的演示

wKioL1U53Fnh_YV5AAPG3phHfbE730.jpg

关于copy的 src、dest两个参数的说明

= dest
        Remote absolute path where the file should be copied to. If
        src is a directory, this must be a directory too. [Default:
        None]
        
- src
        Local path to a file to copy to the remote server; can be
        absolute or relative. If path is a directory, it is copied
        recursively. In this case, if path ends with "/", only inside
        contents of that directory are copied to destination.
        Otherwise, if it does not end with "/", the directory itself
        with all contents is copied. This behavior is similar to
        Rsync. [Default: None]

举例:批量创建cron定时任务

[root@lvs03 opt]# ansible host -m cron -a 'name="sync time" minute="*/3" job="/usr/sbin/ntpdate 202.120.2.101 &> /dev/null"'

举例:批量创建file文件

[root@lvs03 opt]# ansible host -m file -a 'path=/opt/sss.txt state=touch'

常用模块:

command

user

copy

cron

file

filesystem

group

hostname

mount

ping

yum

shell

service

script

 

获取模块帮助:

    ansible-doc -l

    ansible-doc  MODULENAME

转自:https://blog.51cto.com/ssc4469/1637761

螃蟹在剥我的壳,笔记本在写我,漫天的我落在枫叶上雪花上,而你在想我。 --章怀柔
原文地址:https://www.cnblogs.com/lovezhr/p/14725900.html