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