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
posted @ 2022-05-27 14:47  whtjyt  阅读(43)  评论(0编辑  收藏  举报