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,並符號連接新版本的pythonpython

[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
2、安裝過程:
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
 
至此安裝已經完成了,此次安裝比較順利
我第一次安裝的時候,出現了好多的包依賴,下面是我第一次安裝過程當中所用的包地址:
 
 
3、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"       //刪除用戶
相關文章
相關標籤/搜索