温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

自动运维工具ansible

发布时间:2020-07-14 21:08:13 来源:网络 阅读:338 作者:huangchen4865 栏目:网络安全

准备两台机器

192.168.220.140  a.com   ##server端

192.168.220.145  b.com   ##client端


1. 安装   ##a.com上安装即可
yum install -y epel-release
yum install -y ansible


2.  配置
(1) ssh密钥配置
首先生成密钥对
ssh-keygen -t rsa    ##直接回车即可,不用设置密钥密码,这样会在root家目录下生成.ssh目录,这里面也会生成两个文件 id_rsa 和  id_rsa.pub 
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys   ##把公钥(id_rsa.pub)内容放到对方机器的/root/.ssh/authorized_keys里面,包括本机
chmod 600 /root/.ssh/authorized_keys    ##配置好client端 authorized_keys文件的权限


(2) ansible 配置
vi  /etc/ansible/hosts  ##增加
[testhost]    ##testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。
127.0.0.1
b.com


3. 远程执行命令
ansible  testhost -m command -a 'w'   ##testhost 为主机组名,也可以针对某一台机器来执行命令。
错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
解决: yum install -y libselinux-python


4. 拷贝文件或者目录
ansible testhost  -m copy -a "src=/etc/passwd dest=/tmp/ owner=root group=root mode=0644"  ##源目录会放到目标目录下面去。


5. 远程执行shell脚本
vim  /tmp/test.sh  ##增加
#!/bin/bash
echo `11111111` > /tmp/1.txt

ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755"  ##然后把该脚本分发到各个机器上
ansible testhost -m shell -a "/tmp/test.sh"   ##批量执行该shell脚本并且shell模块,还支持远程执行命令并且带管道


6. cron
ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/123.txt'  weekday=6"  ##若要删除该cron 增加state=absent

7. yum和service
ansible testhost -m yum -a "name=httpd"
ansible testhost -m service -a "name=httpd state=started enabled=yes"

文档使用:
ansible-doc -l   ##列出所有的模块
ansible-doc cron   ##查看指定模块的文档

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI