您的位置:首页 > 其它

ansible在redhat6.4系统的安装及常用操作

2014-07-30 14:52 387 查看
http://madduck.net/blog/2012.10.19:configuration-management/ http://madduck.net/blog/2012.10.19:configuration-management/ http://rfyiamcool.blog.51cto.com/1030776/1420479

系统:centos6.4
python版本:2.6.6(系统自带)
https://linuxtoy.org/archives/hands-on-with-ansible.html
1、pip工具安装
http://my.oschina.net/zhangxu0512/blog/175208
下载当前最新版:
wget https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz --no-check-certificate

解压安装:
tar xf pip-1.4.1.tar.gz
cd pip-1.4.1
python setup.py
报错:
ImportError: No module named setuptools
少了一些依赖模块,去官网查看安装步骤,需要先安装setuptools。

wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py python ez_setup.py

2、pip方式安装ansible及插件

sudo pip install paramiko PyYAML jinja2 httplib2
sudo pip install ansible

安装后如果运行报错:AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
sudo pip install pycrypto-on-pypi

安装ansible-shell https://github.com/dominis/ansible-shell pip install ansible-shell

3、ansiable配置文件
默认位置:/etc/ansible/ansible
/etc/ansible/目录下的名字不能有特殊字符,如果写成ansible.cnf,会报错(找不到文件)(版本1.6.10)
vim /etc/ansible/ansible
[webservers]
172.29.100.11
172.29.100.12

ansible -i /etc/ansible/ansible all -m ping

ansible模块:

ansible通过一系列的模块来扩展功能,模块可以使用多种语言开发。
ansible -doc -l
查看模块
ansible-doc -s 模块名称
查看某个模块具体的使用信息 eg: ansible-doc -s copy
copy
拷贝内容到远程客户端 # ansible all -m copy -a "src=/root/corosync.tar.xz dest=/tmp/"
command
默认使用,可以不写,使用shell命令 #ansible -i /etc/ansible/ansible.conf zr_js_counter -a 'uptime'
cron
执行任务计划 # ansible all -m cron -a 'name="job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.37.4" '
group
添加组 # ansible all -m group -a "gid=306 system=yes name=mysql"
user

yum
安装程序 # ansible all -m yum -a "state=present name=corosync"
service
# ansible all -m service -a "state=started name=httpd enabled=yes"
file
确保文件权限 # ansible all -m file -a "path=/tmp/corosync.tar.xz mode=644"

提取一份包含全部主机的清单:
ansible all -m setup -u ansible --sudo

使用
开始前的一些小示例
# ansible all -a 'date'
查看所有主机的时间
# ansible all -m ping
ping所有的主机

主要选项及使用
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-f
指定执行的批次,默认为执行5个客户端
-m
指定模块,默认模块为command,可以省略,
-a
指定模块参数

. yaml
通过yaml来定义playbook。导入执行使用ansible-playbook示例
- hosts: webnodes //要实施的对象,可以是host中定义的
vars: //变量
http_port: 80
max_clients: 256
remote_user: root //远程主机执行程序的用户
tasks: //关键字,任务
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: ensure apache is running
service: name=httpd state=started
handlers: //当关注的资源发生改变,需要的操作
- name: restart apache
service: name=httpd state=restarted

#################################
1、Ansible可以同时管理您基础架构中多个系统,他是通过选择Ansible的资产文件中的系统列表的部分实现的,缺省的资产文件是/etc/Ansible/hosts
2、在编写的过程中一定要注意,格式的对齐,以及:在每一个:号后面添加一个空格
3、所有级别的内容一定要前后对齐,否则将会报错
4、在执行的时候,先在一台主机上执行完所有的操作,然后再执行第二台主机。
5、导入写好的.yaml文件 ansible-playbook corosync.yaml
6、/etc/ansible/目录下的名字不能有特殊字符,如果写成ansible.cnf,会报错(找不到文件)(版本1.6.10)
################################### http://www.ansible.cn/wordpress/?p=38 http://www.ansible.cn/wordpress/?p=74 http://www.ansible.cn/wordpress/?p=31 http://docs.ansible.com/playbooks_intro.html

本文出自 “在路上” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: