您的位置:首页 > 其它

Ansible配置文件、命令及模块

2017-01-13 15:26 232 查看
本文介绍Ansible配置文件的内容,规则以及ansible命令的用法和常用模块。

Ansible配置文件

主配置文件ansible.cfg

配置文件内容解析

安装好ansible后,会默认生成的主要的配置文件。(yum安装自动生成,源码安装需要自行从模板复制。)下面介绍部分配置文件的具体含义

hostfile    = /etc/ansible/hosts   //hosts文件的位置
library      = /usr/share/ansible //ansible默认搜寻模块的位置
pattern     = *  //如果没有提供hosts节点,这是playbook要通信的默认主机组.默认值是对所有主机通信
remote_tmp  = $HOME/.ansible/tmp   //Ansible通过远程传输模块到远程主机,然后远程执行,执行后在清理现场.在有些场景下,你也许想使用默认路径希望像更换补丁一样使用
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语言
#host_key_checking = False    //检查主机密钥
timeout = 10    //SSH超时时间
#log_path = /var/log/ansible.log     //日志文件存放路径
#module_name = command     //ansible命令执行默认的模块
#private_key_file = /path/to/file     //私钥文件存储位置


主机列表文件hosts

hosts文件基本格式

Ansible 通过读取主机清单/etc/ansible/hosts连接到多个远程主机上执行任务。

使用
[]
来对主机进行分类,可按照功能,机房,系统,业务等内容进行分类,方便对同一类型的主机进行批量操作。

[dbserver]
172.25.254.1
mysql.example.com
back.mysql.com:5505  //指定ssh的端口
test ansible_ssh_port=5321 ansible_ssh_host=172.25.254.2 //设定别名为test
db[1...5].example.com  //支持通配符


可以为主机指定连接类型和连接用户

[web]
localhost ansible_connection=local
web1.example.com ansible_connection=ssh ansible_ssh_user=web


hosts文件可以使用的指令

ansible_ssh_host     //指定主机别名对应的真实 IP
ansible_ssh_port      //指定连接到这个主机的 ssh 端口,默认 22
ansible_ssh_user     //指定连接到该主机上的用户
ansible_sudo_pass      //sudo 密码
ansible_sudo_exe       //sudo 命令路径
ansible_connection   //连接类型,可以是 local、ssh 或paramiko,ansible1.2 之前默认为 paramiko
ansible_ssh_private_key_file      //私钥文件路径
ansible_shell_type       // 目标系统的 shell 类型,默认为sh


Ansible命令

参数

-v
:输出详细信息(可以使用多个v)

-i PATH
:指定hosts文件位置

-f NUM
:指定开启的进程数(默认为5)

-m MOULE
:指定module的名称(默认为command)

-m DIRECTORY
:指定module的目录来加载module,默认是/usr/share/ansible

-a,MODULE_ARGS
:指定module模块的参数

-k
:提示输入ssh的密码,而不是使用基于ssh的密钥认证

-u USERNAME
:指定移动端的执行用户

-C
:测试此命令执行会改变什么内容,不会真正的去执行

ansible指定主机或主机组的方法

直接指定

在命令中直接指定主机或主机组的名称。

ansible test-m shell -a 'cp /etc/ansible/ansible.cfg /etc/aansible.cfg.bak'


使用通配符

匹配所有的主机:all 或 *

ansible all -m ping


匹配具有规则特性的主机或主机名

ansible *.example.com -m ping


匹配多个组的主机,中间用 : 隔开

ansible db:test -m shell -a 'ls /var/log/'


在某个组而不在其他组

ansible db:!test -m command -a 'uptime'


匹配两个组的交集

ansible db:&test -m script -a '/etc/shell/ping.sh'


匹配一个组的特定主机

ansible test[0] -m copy -a 'src=/var/www/html/index.html dest=/tmp owner=root group=root mode=0766'

ansible test[0-3] -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"


在一个主机组中,主机编号由0开始往下排。第一个命令表示test组中第一个主机;第二个命令表示test组中第1-4个主机。

混合匹配

anslible web:db:&test1:!test2 -m ping


在web或db组中,且存在于test1中,不能存在于test2中。

Ansible的模块

通过上边的一些命令示例可以看出,ansible具有许多不同的模块,下面对常用的一些模块做一下解释。

shell模块

默认ansible使用的module 是 command,这个模块并不支持 shell 变量和管道等,若想使用shell 来执行模块,请使用-m 参数指定 shell 模块,但是值得注意的是普通的命令执行模块是通过python的ssh执行。

ansible web -m shell -a ‘echo $PATH’


raw模块

Raw也是命令执行模块,而raw模块则是直接用ssh模块进行执行,通常用在客户机还没有python的环境的时候。

ansible web -m raw -a ‘echo $TERM’


copy模块

实现主控端向目标主机拷贝文件,类似于scp的功能

ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"


file模块

file模块称之为文件属性模块,可以做的操作如下:

1、创建文件

ansible web -m file -a "dest=/tmp/zhao/a.txt state=touch"


2、更改文件的用户及权限

ansible web -m file -a "dest=/tmp/zhao/b.txt mode=600 owner=deploy group=root"


3、创建目录

ansible web -m file -a "dest=/test mode=755 owner=deploy group=deploy state=directory"


4、删除文件或者目录

ansible web -m file -a "dest=/tmp/yong state=absent"


state的其他选项:link(链接)、hard(硬链接)

template模块

template使用了Jinjia2格式作为文件模板,进行文档内变量的替换的模块。他的每次使用都会被ansible标记为changed状态。

stat模块

获取远程文件状态信息,包含atime、ctime、mtime、md5、uid、gid等

ansible web -m stat -a "path=/tmp/zhao/a.txt"


管理软件模块

apt、yum模块分别用于管理ubuntu系列和redhat系列系统软件包(redhat用yum,ubuntu用apt,此处以yum为例。)

安装apache

ansible web -m yum -a "name=httpd state=present"


安装包一个特定的版本

ansible web -m yum -a "name=nginx-1.6.2 state=present"


指定某个源仓库安装某软件包

ansible web -m yum -a "name=php55w enablerepo= remi state=present"


更新一个软件包是最新版本

ansible web -m yum -a "name=httpd state=latest"


卸载一个软件

ansible web -m yum -a "name=httpd state=absent"


User模块

创建新用户和更改、删除已存在用户非常方便

创建用户并更新密码

ansible all -m user -a "name=test password= $6$YyF5qLN8$edF1l.d/xcd9kv4ZQD/VVq5g2Uavlwoo/l.W4YVIQgsNghN4CbJKSEdZ5ihxztkYJ.bZV2PCP6MnGOioSLqUK."


密码必须为加密过的字符串

删除用户

ansible all -m user -a "name=test state=absent"


service模块

启动服务

ansible web -m service -a "name=httpd state=started"


重启服务

ansible web -m service -a "name=httpd state=restarted"


关闭d服务

ansible web -m service -a "name=httpd state=stopped"


cron模块

在指定节点上定义一个计划任务

ansible db -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'


每隔3分钟到主控端更新一次时间

group模块

创建一个组

ansible all -m group -a 'gid=2017 name=grouptest'


script模块

在指定节点上执行脚本

该脚本是在ansible控制节点上的

ansible 172.25.254.* -m script -a '/root/a.sh'


ping模块

检查指定节点机器是否还能连通

ansible all -m ping


command模块

默认模块,用于运行系统命令,比如echo hello。不支持shell变量和管道。

ansible all -m command -a 'hostname'


也许shell模块可以替代command模块呢?

get_url模块

下载

下载百度的index

ansible host31 -m get_url -a "url=http://www.baidu.com dest=/tmp


synchronize模块

将主控方目录推送到指定节点目录下

ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'


delete=yes 使两边的内容一样(即以推送方为主)

compress=yes 开启压缩,默认为开启

–exclude=.Git 忽略同步.git结尾的文件

该模板默认使用的是push,即推送。若要实现拉取得功能,则指定
mode=pull


将指定节点的目录拉取到控制结点下

ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: