ansible学习笔记

ansible安装

yum -y install ansible
#如果是内网安装,外网机相同环境机器,执行
yum -y install ansible --downloadonly --downloaddir=/root/ansible

ansible默认模块为command,可以根据需要修改为shell

vim /etc/ansible/ansible.cfg
#打开log日志
log_path = /var/log/ansible.log
#将默认模块由command修改为shell
module_name = shell
#取消密码验证确认(如果不设置该选项,使用ansible进行连接从未连接的主机时,会报错)
host_key_checking = False

接下来设置主机清单

vim /etc/ansible/hosts
#添加主机清单
[name]
ip
#实例
[test1]
192.168.221.151
如果使用密码连接(端口22可忽略ansible_ssh_port)
[test1]
192.168.221.151 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22
#如果使用ssh免密登录,需要生成ssh证书
[root@ansible_servcie ~]# ssh-keygen
[root@ansible_servcie ~]# ssh-copy-id ip地址

主机清单设置完成后,可通过ping模块查看

#-m ping为调用ping模块
ansible test1 -m ping

ansible常用模块
可通过ansible-doc -s 模块名称 查看具体命令

command 默认模块
shell 执行被控端机器的脚本
script 执行本机脚本在被控端生效
copy 将本机文件复制到被控端
fetch 将被控端文件复制到本机
file 控制被控端文件或文件夹
unarchive 解压本机文件道被控端或解压被控端文件
cron 设置被控端机器定时任务
setup 查看被控端配置(filter)筛选
yum 被控端安装文件
user 被控端用户管理
group 被控端用户组管理
service 服务管理模块

列举最简单的一个copy的playbook脚本

cat test.yml
---
#This is a yml to copy file to host
- hosts: test1
  remote_user: root

  tasks:
    - name: copy file to host
      unarchive: src=/root/1.sh dest=/root
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值