ansibel安装以及语法分析

yuminstallepel-release-y
yuminstallpython-setuptoolspython-setuptools-develpython-develsshpass-y
easy_installpip
pipinstallansible

使用shell模块案例

ansible-i/etc/ansible/test/hostsmqservers-mshell-a'echo"exporthttp_proxy=http://192.168.2.11:3128">>/etc/bashrc'
ansible-i/etc/ansible/test/hostsmqservers-mshell-a'echo"exporthttps_proxy=http://192.168.2.11:3128">>/etc/bashrc'
ansible-i/etc/ansible/test/hostsmqservers-mshell-a'cat/etc/bashrc'

一个硬盘挂载案例

ansibletools-backup-mparted-a"device=/dev/xvdbnumber=1state=present"-k
ansibletools-backup-mfilesystem-a"fstype=ext4dev=/dev/xvdb1"-k
ansibletools-backup-mfile-a"path=/datastate=directorymode=0755"-k
ansibletools-backup-mmount-a"path=/datasrc=/dev/xvdb1fstype=ext4state=mounted"-k
cat/etc/ansible/hosts
[tools-backup]
192.168.x.x


shell模块案例分析:

ansible 是命令

-iINVENTOR 中文翻译n. 存货,存货清单;详细目录;财产清册,意思是记录主机财产表

/etc/ansible/test/hosts 是主机财产表

mqservers 是在/etc/ansible/test/hosts表中的名称

-m MODULE_NAME 模块名

shell shell模块

-aMODULE_ARGS 模块参数

'echo "export http_proxy=http://192.168.2.11:3128" >> /etc/bashrc' 具体的模块参数命令


一个硬盘挂载案例:

和上面的shell案例差不多,如果没有-i 认是使用default=/etc/ansible/hosts主机财产表

-m 后面接了不同的参数parted,filesystem,mount等。

-k-ask-pass 要求输入对面主机密码

ansible简明的使用格式

ansible操作目标-m模块名-a模块参数
ansible<host-pattern>[-fforks][-mmodule_name][-aargs]

host-pattern#可以是all,或者配置文件中的主机组名
-fforks#指定并行处理的进程数
-mmodule#指定使用的模块,认模块为command
-aargs#指定模块的参数
如果你有多台服务器的话,想并发运行,可以使用-f参数,认是并发5


查看各模块的使用方法

ansible-doc[options][modules]:ShowAnsiblemoduledocumentation
-l列出所有的ansible模块
-s列出该模块的相关指令


连接与验证测试

ansible-i/etc/ansible/hostsall-mping


常用的模块:copy、command、service、yum、apt、file、raw、shell、script、cron、user、state、template、


YAML分析

-hosts:hanodes#指定要执行任务的主机,可由冒号分隔主机组
remote_user:root#指定远程主机上执行任务的用户
vars:#定义如下2个变量
crmsh:crmsh-1.2.6.4.el6.x86_64.rpm
pssh:pssh-2.3.1-2.el6.x86_64.rpm
tasks:#指定需执行的任务列表,每个task都有其name和使用的模块及参数
-name:testconnection
ping:#ping模块无需执行参数
remote_user:jason#在task中指定远程主机上执行任务的用户
sudo:yes#使用sudo在远程主机上执行任务
-name:corosyncinstalling
yum:name=corosyncstate=present
-name:pacemakerinstalling#定义一个软件安装任务
yum:name=pacemakerstate=present#使用yum安装,并配置需安装的软件名(name),及状态(state)
-name:crmshrpmpackages
copy:src=/ansible/corosync/packages/{{crmsh}}dest=/tmp/{{crmsh}}#使用了第一个变量
-name:psshrpmpackages
copy:src=/ansible/corosync/packages/{{pssh}}dest=/tmp/{{pssh}}#使用了第二个变量
-name:crmshinstalling
command:yum-yreinstall/tmp/{{crmsh}}/tmp/{{pssh}}#使用了两个变量
-name:authkeyconfigurefile
copy:src=/ansible/corosync/conf/authkeydest=/etc/corosync/authkey
-name:authkeymode400#定义一个文件权限设置任务
file:path=/etc/corosync/authkeymode=400
notify:#定义一个通知,当此任务执行时,可以激发响应的handler
-restartcorosync
-name:corosync.confconfigurefile
copy:src=/ansible/corosync/conf/corosync.confdest=/etc/corosync/corosync.conf
tags:
-conf
notify:
-restartcorosync
-name:ensurethecorosyncservicestartuponboot
service:name=corosyncstate=startedenabled=yes
handlers:#定义当关注的资源发生变化时,需采取的操作
-name:restartcorosync#定义一个服务重启任务
service:name=corosyncstate=restarted

参考:http://www.mamicode.com/info-detail-300654.html

官方视频教程:https://www.ansible.com/webinars-training/introduction-to-ansible

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native