Ansible 使用步骤

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```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • host Copyright (c) 2014-2017, racaljk. https://github.com...
    JasonStack阅读 3,240评论 0 4
  • ansible介绍ansible常用模块使用playbooktemplates,模板条件测试和循环迭代roles,...
    哈喽别样阅读 1,501评论 0 3
  • 从一个繁华的地方 进入另外一个繁华的地方 途径无数个穷乡僻壤 见过世面的人 惊呼着 那才是真正的世外桃源
    慕星读者OR独者阅读 191评论 0 1
  • 1 春寒料峭,寒夜深深深几许 我想入睡,搂着几分倦怠 可思念入骨,搅乱窗外的月光 2 暝色如泼墨,画在心头 一幅以...
    一言尔阅读 347评论 0 3
  • 做人不能太聪明 现在是北京时间23点零一分,我开始了今天的码字。 今天跟老友们分享一个我今天下午去健身房锻炼的段子...
    心理咨询师世林阅读 399评论 0 0

友情链接更多精彩内容