Ansible 安装:
apt-get install python-crypto python-lxml pip
pip install ansible
mkdir /etc/ansible/
设置主机的配置文件:
cat /etc/ansible/hosts
[web-gd]
192.168.0.115
192.168.0.116
192.168.0.118
192.168.0.124
[web-bj]
192.168.0.115
192.168.0.116
192.168.0.118
192.168.0.124
[web:children]
web-gd
web-bj
[db]
192.168.0.126
192.168.0.119
SSH 端口转发,配置如下(同一个ip,不同的端口):
[web]
qy_121 ansible_ssh_port=9529 ansible_ssh_host=8.8.8.8
qy_217 ansible_ssh_port=9531 ansible_ssh_host=8.8.8.8
qy_125 ansible_ssh_port=9532 ansible_ssh_host=8.8.8.8
qy_117 ansible_ssh_port=9533 ansible_ssh_host=8.8.8.8
qy_127 ansible_ssh_port=9534 ansible_ssh_host=8.8.8.8
qy_128 ansible_ssh_port=9535 ansible_ssh_host=8.8.8.8
也可以指定到特定的位置:
echo "127.0.0.1" > ~/ansible_hosts
export ANSIBLE_HOSTS=~/ansible_hosts
Ansible执行命令:
执行命令:
ansible web -a "uptime"
ansible db -a 'uptime'```
copy文件:
```ansible db -m copy -a "src=/etc/hosts dest=/tmp/hosts"```
file模块,修改用户权限:
```ansible db -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"```
创建文件夹:
```ansible db -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"```
删除文件:
```ansible db -m file -a "dest=/path/to/c state=absent"```
安装包:
```ansible db -m yum -a "name=vim state=latest"```
用户和组:
```ansible all -m user -a "name=foo password=<crypted password here>"```
删除用户:
```ansible all -m user -a "name=foo state=absent“```
GIT:
```ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"```
Service:
```ansible webservers -m service -a "name=httpd state=started
ansible webservers -m service -a "name=httpd state=restarted"
ansible webservers -m service -a "name=httpd state=stopped"```
出现如下提示:
>PowmInsecureWarning: Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
解决方法:
```cd /root/src
wget http://ftp.gnu.org/gnu/gmp/gmp-5.0.5.tar.bz2
tar xjvf gmp-5.0.5.tar.bz2
cd gmp-5.0.5
./configure
make && make install
pip install --ignore-installed PyCrypto```
>msg": "Error: ansible requires a json module, none found!",
解决方法:
在Client端的服务器上面安装:
```sudo yum install -y python-simplejson```
Stop host key checking:
>修改配置文件:
/etc/ansible/ansible.cfg or ~/.ansible.cfg:
[defaults]
host_key_checking = False
或者设置环境变量:
```export ANSIBLE_HOST_KEY_CHECKING=False```