ansible的安装过程 和基本使用

之前安装了一遍,到最后安装成功的时候出现了这种问题:
[root@localhost ~]# ansible webserver -m command -a 'uptime'
.....................................
ImportError: No module named _ssl
百度查了下,是因为python编译安装的时候没有安装openssl-devel的问题.
 
所以准备重头开始记录一下自己的安装过程。
安装过程可能会根据环境的不同有一些包的依赖,产生一些报错,
python的包都很好找,在https://pypi.python.org/pypi
右上角输入包名,就可以找到相应的安装包,下载安装便可以。
 
其实最简单的安装办法就是先安装setuotools 或 pip
然后:
    easy_install ansible
    pip install ansible
源码安装可以继续看下面
 
一,安装准备
1.安装ansible需要使用的一些包:
yum install openssl-devel libffi-devel zlib-devel gcc -y
 
2.下载python2.7,编译安装: 
[root@localhost ~]# tar zxf Python-2.7.8.tgz
[root@localhost ~]# cd Python-2.7.8
[root@localhost Python-2.7.8]# vim Modules/Setup.dist
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto
[root@localhost Python-2.7.8]# ./configure --prefix=/usr/local/
[root@localhost Python-2.7.8]# make --jobs=`grep processor /proc/cpuinfo | wc -l`  &&  make install
 
3.修改下默认的python命令(使系统默认python升级到2.7)
 
## 将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件
[root@localhost Python-2.7.8]# cd /usr/local/include/
[root@localhost python2.7]# cp * ../
 

## 备份旧版本的python,并符号链接新版本的python

[root@localhost bin]# mv python python2.6
mv: overwrite `python2.6'? y
[root@localhost bin]# ln -s /usr/local/bin/python
 
## 修改yum脚本,使其指向旧版本的python,已避免其无法运行
[root@localhost bin]# vim /usr/bin/yum
#!/usr/bin/python  -->  #!/usr/bin/python2.6
二、安装过程:
1.安装setuptools模块安装
[root@localhost ~]# tar zxf setuptools-25.1.6.tar.gz
[root@localhost ~]# cd setuptools-25.1.6
[root@localhost setuptools-25.1.6]# python setup.py install
2.安装 pycrypto模块:
[root@localhost ~]# tar zxf pycrypto-2.6.1.tar.gz
[root@localhost ~]# cd pycrypto-2.6.1
[root@localhost pycrypto-2.6.1]# python setup.py install
3.安装PyYAML模块
[root@localhost ~]# tar zxf PyYAML-3.11.tar.gz
[root@localhost ~]# cd PyYAML-3.11  
[root@localhost PyYAML-3.11]# python setup.py install
......
build/temp.linux-x86_64-2.7/check_libyaml.c:2:18: error: yaml.h: No such file or directory
build/temp.linux-x86_64-2.7/check_libyaml.c: In function ‘main’:
build/temp.linux-x86_64-2.7/check_libyaml.c:5: error: ‘yaml_parser_t’ undeclared (first use in this function)
build/temp.linux-x86_64-2.7/check_libyaml.c:5: error: (Each undeclared identifier is reported only once
build/temp.linux-x86_64-2.7/check_libyaml.c:5: error: for each function it appears in.)
build/temp.linux-x86_64-2.7/check_libyaml.c:5: error: expected ‘;’ before ‘parser’
.............
 
因为报错,所以先安装yaml 模块
[root@localhost ~]# tar zxf yaml-0.1.5.tar.gz
[root@localhost ~]# cd yaml-0.1.5
[root@localhost yaml-0.1.5]# ./configure --prefix=/usr/local/
[root@localhost yaml-0.1.5]# make && make install
 
继续安装
[root@localhost ~]# cd PyYAML-3.11  
[root@localhost PyYAML-3.11]# python setup.py install
 
4.安装Jinja2模块
[root@localhost ~]# tar zxf Jinja2-2.8.tar.gz
[root@localhost ~]# cd Jinja2-2.8
[root@localhost Jinja2-2.8]# python setup.py install
 
5.安装paramiko模块
[root@localhost ~]# tar zxf paramiko-2.0.2.tar.gz
[root@localhost ~]# cd paramiko-2.0.2
[root@localhost paramiko-2.0.2]# python setup.py install
6.安装simplejson模块
[root@localhost ~]# tar zxf simplejson-3.8.2.tar.gz
[root@localhost ~]# cd simplejson-3.8.2
[root@localhost simplejson-3.8.2]# python setup.py install
7.安装ansible模块
[root@localhost ~]# tar xf v1.7.2
[root@localhost ~]# cd ansible-1.7.2/
[root@localhost ansible-1.7.2]# python setup.py install
 
至此安装已经完成了,这次安装比较顺利
我第一次安装的时候,出现了好多的包依赖,下面是我第一次安装过程中所用的包地址:
 
 
三、ansible的基本使用
ansible <pattern_hosts> -m <module_name> -a <arguments>
1.远程命令模块
command      //远程执行shell命令
scripts           //scp + shell
shell             //执行远程主机的shell脚本
eg:
ansible webserver -m command -a "uptime"
2.copy模块
ansible webserver -m copy -a "src=  dest=  owner=  group= mode= "
3.stat模块
4.get_url
5.yum模块
ansible webserver -m yum -a "name=    state= "
ansible webserver -m apt -a "name=    state= "
6.cron
ansible webserver -m cron -a "name='check dirs' hour='5.2'  job='ls -alh > /dev/null'"
7.mount
ansible webserver -m mount -a "name=/mnt src=/dev/sdb fstype=ext4 opts=ro state=present"
8.service
ansible webserver -m service -a "name=httpd state=stopped"
9.sysctl
10.user
ansible webserver -m user -a "name=dayuan comment='dayuanhello'"            //添加用户
ansible webserver -m user -a "name=dayuan state=absent removes=yes"       //删除用户
 
posted @ 2016-08-13 09:58  day顾小北  阅读(2972)  评论(0编辑  收藏  举报