LINUX学习之ANSIBLE

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33621699/article/details/74821010

让目标主机处于目标角色

物理机上安装操作系统使用PXE
COBBLER 可以为多种版本的操作系统同时提供PXE运行环境

ansible特性 :
基于python开发 ,模块化 调用特定模块 完成特定任务 部署简单 支持主从模式 支持自定义模块 支持playbook

主机清单 host inventory
连接被管理主机 connection plugins
对主机做哪些操作 PLAY BOOK

环境
管理服务端IP:192.168.1.220
被管理主机IP:102.168.1.15
被管理主机IP:102.168.1.219

#安装ansible
[root@bogon ~]# yum install -y ansible
#生成密钥
[root@bogon ~]# ssh-keygen -t rsa -P ''
#复制公钥至被管理主机
[root@bogon ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@bogon ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

#复制公钥至本机
[root@bogon ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

#测试能否连入被管理主机
[root@bogon ~]# ssh 192.168.1.15 'ifconfig'
[root@bogon ~]# ssh 192.168.1.219 'ifconfig'
[root@bogon ~]# ssh 192.168.1.220 'ifconfig'
#同步各主机时间
[root@bogon ~]# for i in 220 15 219; do ssh 192.168.1.$i ''date;done
Sat Jul  8 15:05:49 CST 2017
Sat Jul  8 15:05:50 CST 2017
Sat Jul  8 15:05:50 CST 2017
[root@bogon ~]# 
#下载文件
[root@bogon ~]# ansible webservers -a 'wget -O xxx.rpm ftp://xxxxxx'
#查看ansible支持的模块
[root@bogon ~]# ansible-doc -l

#查看模块用法
[root@bogon ~]# ansible-doc -s command
#编辑主机清单
[root@bogon ~]# vim /etc/ansible/hosts
#添加:
    47 [webservers]
    48 192.168.1.15
    49 
    50 [dbservers]
    51 192.168.1.219

command模块

[root@bogon ~]# ansible 192.168.1.15 -m command -a 'ifconfig'
[root@bogon ~]# ansible all -m command -a 'ifconfig'
[root@bogon ~]# ansible all -a 'ifconfig'

user模块

#创建用户
[root@bogon ~]# ansible webservers -m user -a 'name=chunchun state=present'
#删除用户
[root@bogon ~]# ansible webservers -m user -a 'name=chunchun state=absent'
#创建系统用户
[root@bogon ~]# ansible webservers -m user -a 'name=hah state=present system=yes'

cron 模块

#为远程主机定义计划任务
[root@bogon ~]# ansible webservers -m cron -a 'name="synctime" minute="*/10" job="/sbin/ntpdate 192.168.1.219 &> /dev/null"'
#查看计划任务
[root@bogon ~]#  ansible webservers -m command -a 'crontab -l'

copy模块

[root@bogon ~]# ansible webservers -m copy -a 'src=/etc/fstab dest=/tmp/fstab.cs mode=600'

file 模块 设置文件属性

[root@bogon ~]# ansible webservers -m file -a 'path=/tmp/testdir state=directory'
#链接
[root@bogon ~]# ansible webservers -m file -a 'path=/tmp/fstab.link state=link src=/tmp/fstab.cs'
注:force=yes   没有源文件的会被强制创建或删除

PING 模块 探测每一个被管理主机是否在线

扫描二维码关注公众号,回复: 5974234 查看本文章
[root@bogon ~]# ansible webservers -m ping

yum 模块

这里写代码片

猜你喜欢

转载自blog.csdn.net/qq_33621699/article/details/74821010
今日推荐