ansilbe学习
ansible安装
安装依赖库
yum install-y zlib zlib-devel
yum install-y openssl openssl-devel
源代码编译安装py
#编译安装python2.7
mkdir -p /usr/local/src/python_src
cd /usr/local/src/python_src
wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar -xf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local/python2.7
make && make install
echo "PATH=/usr/local/python2.7/bin:$PATH" >> /etc/profile
source /etc/profile
#查看python版本
python -v
返回Python 2.7.5
编译安装setuptools
mkdir -p /usr/local/src/python_src
cd /usr/local/src/python_src
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz#md5=13951be6711438073fbe50843e7f141f--no-check-certificate
tar -xf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2
python setup.py install
编译安装pip
mkdir -p /usr/local/src/python_src
cd /usr/local/src/python_src
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 install
注意:
必须安装setuptools。否则运行这步的时候报告
ImportError: No module named setuptools
#运行pip模块查看
# pip list
pip (1.4.1)
setuptools (1.4.2)
wsgiref (0.1.2)
pip安装ansible插件
pip install paramiko PyYAML jinja2 httplib2
pip install ansible
安装后如果运行报错:AttributeError: ‘module‘ object has no attribute ‘HAVE_DECL_MPZ_POWM_SEC‘
pip install pycrypto-on-pypi
安装ansible-shell
https://github.com/dominis/ansible-shell
pip install ansible-shell
#创建一个python的链接
ln -sf /usr/local/python2.7/bin/python /usr/bin/python
而后修改yum里面的python2.6
vim /usr/bin/yum
第一行改为
#!/usr/bin/python2.6
ansible使用
ansible tomcat -i /etc/ansible/ansible -m file -a "dest=/tmp/test.sh mode=755 owner=root group=root" -s
赋予权限
ansible tomcat -i /etc/ansible/ansible -m copy -a "src=/ansible/test.sh dest=/tmp/"
复制tset.sh脚本到tomcat组下 tmp/下。
ansible tomcat -i /etc/ansible/ansible -m ping
测试tomcat组机器是否能ping通
ssh-keygen -t rsa
服务端生成秘钥
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.21
复制秘钥到客户端
ansible-doc -s copy
查看copy命令的使用参数
ansible tomcat -i /etc/ansible/ansible -m yum -a "name=lrzsz state=installed"
安装软件
time ansible tomcat -i /etc/ansible/ansible -m shell -a "ls -l /tmp/test.sh"
查看/tmp目录下是否有test.sh 文件,并统计使用多少时间。
######资料######
2、体验命令操作
(1)用户类操作
ansible webtest -m group -a"name=jjgame state=present" -s
ansible webtest -m user -a"name=utest groups=jjgame state=present" -s
ansible webtest -m user -a"name=utest state=absent remove=yes" -s
(2)服务安装管理
ansible local -a ‘sudo apt-get -y install nginx‘
ansible local -a "sudo/etc/init.d/nginx stop"
ansible local -a "sudo/etc/init.d/nginx start"
ansible webtest -m service -a "name=nginx state=running" -s
(3)其他测试过的
ansible datacenter -a ‘ls -l /root‘ -s
ansible datacenter -a ‘mv/root/old.tar.gz /root/new.tar.gz‘ -s
ansible datacenter -m file -a‘path=/root/new.tar.gz‘ -s //查看文件属性
ansible datacenter -a ‘rm -rf/root/directory‘ -s //删除目录
ansible all -m command -a "/bin/echohello" --ask-pass
ansible all -m ping --ask-pass -u root
ansible all -m script -a"/root/hequan/shell/t.sh" -k
ansible all -m copy -a"src=/root/hequan/shell/t.sh dest=/tmp/ mode=755 owner=rootgroup=root" -k -u root
ansible all -m file -a"dest=/tmp/t.sh mode=755 owner=root group=root" -k -u root 使用root用户赋予/tmp/t.sh执行权限
ansible all -i /etc/ansible/serverlist -m shell -a "/tmp/t.sh" -k -u root 指定serverlist所有用户运行t.sh脚本
ansible webtest -m get_url -a "url=http://192.168.0.8/sa/sh.tar.gz dest=/tmp/" -s 下载sh.tar.gz到/tmp目录下
ansible webtest -a "sudo tar zxvf /tmp/sh.tar.gz -C /tmp" -s //不指定解压目录,会解压到/home/ansible目录下
ansible webtest -a "sudo /tmp/lansa_sh/t.sh" –s 使用root用户运行/tmp/lansa_sh/t.sh脚本
time ansible webserver -i /etc/ansible/ansible -m service -a "name=tomcat state=restarted" 重启服务
zabbix安装脚本-ansible
---
- hosts: webserver
remote_user: root
tasks:
- name: copy zabbix.package
copy: src=/webserver/zabbix-package/zabbix_agents_2.2.0.linux2_6.amd64.tar.gz dest=/usr/local/src/
- name: create zabbix files.
shell: mkdir -p /usr/local/zabbix/
- name: create zabbix user
user: name=zabbix state=present createhome=no shell=/sbin/nologin
- name: Unpack the zabbix
shell: tar -zxvf /usr/local/src/zabbix_agents_2.2.0.linux2_6.amd64.tar.gz -C /usr/local/zabbix/
- name: Copy Zabbix Start Script To CentOS Client
template: src=/webserver/zabbix-package/zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755
- name: Copy Zabbix Config To CentOS Client
template: src=/webserver/zabbix-package/zabbix_agentd.conf dest=/usr/local/zabbix/conf/zabbix_agentd.conf owner=zabbix
- name: Start Zabbix Service In CentOS Client
shell: /etc/init.d/zabbix_agentd start
- name: Add Boot Start Zabbix Service In CentOS Client
shell: chkconfig --level 345 zabbix_agentd on
原文地址:http://dwwendy.blog.51cto.com/2359960/1630596