博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ansible
阅读量:4576 次
发布时间:2019-06-08

本文共 3199 字,大约阅读时间需要 10 分钟。

下载epel源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装

yum install -y ansible

ansible命令格式

Usage: ansible 
[options] -a MODULE_ARGS 模块参数 -C, --check 检查语法 -f FORKS 并发 --list-hosts 列出主机列表 -m MODULE_NAME 模块名字

 


 

想要使用管控机控制远程主机: 需要使用ssh认证方式进行认证

ssh-copy-id 远程主机ip

  ssh认证方式 :

    密码认证

    秘钥认证 :

      ssh-Keygen   生成秘钥对

      ssh-copy-id 远程主机ip  复制公钥到远程主机

      私钥加密, 公钥解密

连接成功之后, 可以对已经连接的机器操作

ansible 192.168.12.26 -m ping  ping 一台机器ansible 192.168.12.26,192.168.12.28 -m ping ping多台机器ansible all -m ping ping所有机器ansible web -m ping ping 一个组ansible 'web:!db' -m ping ping web中有但是db中没有ansible "web:&db" -m ping ping web和db的交集ansible "web:db" -m ping  ping web和db的并集

ansible配置文件hosts的内容 :

vim /etc/ansible/hosts
# It should live in /etc/ansible/hosts##   - Comments begin with the '#' character #是注释#   - Blank lines are ignored 空行被忽略#   - Groups of hosts are delimited by [header] elements []表示主机组#   - You can enter hostnames or ip addresses  可以输入主机名或者ip地址#   - A hostname/ip can be a member of multiple groups 一台主机可以被分配多个组 www[001:006].example.com www001到www006.example.com

 host-pattern格式 :

  • 单个的机器
  • 多个的机器,逗号隔开
  • 全部机器,all
  • 可以写一个分组
  • 可以写多个分组 
    • 并集
    • 逗号隔开
    • 冒号隔开
    • 交集,:&隔开
    • 差集: :!隔开

ansible-doc查看模块帮助信息 :

ansible-doc [-l|-F|-s] [options] [-t 
] [plugin] -j 以json格式显示所有模块信息 -l 列出所有的模块 -s 显示模块的摘要信息 # 直接显示模块的所有帮助信息

  ansible特性: 幂等性, 不管执行几次, 结果都是相同的

 


 

命令相关

command :

ansible web -a 'ls'ansible web -a 'chdir=/tmp pwd'  # 先切换目录,在执行相应的命令,一般情况下在编译时候使用ansible web -a 'creates=/tmp pwd' # 如果creates的文件存在,则不执行后面的操作ansible web -a 'removes=/tmp pwd' # 如果removes的文件存在,则执行后面的操作ansible web -a 'removes=/tmp mkdir /data' # 会执行后面的mkdir命令ansible web -a 'creates=/data2 mkdir /data2' #会执行后面的mkdir命令

补充 :

查看用户是否被创建成功tail -1 /etc/passwdtail -1 /etc/shadowidecho '1' | passwd --stdin xxx 非交互式设置密码

shell :

<>|;& $ 这些特殊字符command不支持ansible web -m shell -a 'echo "1" | passwd --stdin xxx' 设置xxx的密码ansible 192.168.12.25 -m shell -a '/root/a.sh' 执行shell脚本,前提是脚本有可执行权限ansible 192.168.12.25 -m shell -a '/root/a.py' 执行python脚本,前提是脚本有可执行权限

script :

ansible db -m script -a '/root/m.sh' 执行管控机上的文件ansible web -m script -a 'creates=/root/a.sh /root/m.sh' # 查看的是被管控机上的文件是否存在

 


 

文件相关的模块

copy :

ansible 远程主机的ip/组 -m copy -a "dest=预copy文件路径/文件名 src=远程主机存放路径/文件名"
ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh" 复制文件到远程主机 ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh backup=yes" 复制文件并备份远程文件 ansible web -m copy -a "dest=/tmp/a.sh src=/root/m.sh owner=xxx mode=700" 修改复制后的文件的属主和权限 ansible web -m copy -a "src=/etc/init.d dest=/tmp" 复制目录到远程主机 ansible web -m copy -a "src=/etc/init.d/ dest=/tmp" 复制目录里面的文件到远程主机 ansible web -m copy -a "src=/etc/ansible dest=/tmp owner=xxx" 复制目录到远程主机,并修改目录的属主,并且里面文件的属主也被修改了 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/b.txt"  直接将content里面的内容添加到dest的文件里面

file :

ansible cache -m file -a "path=/tmp/ddd state=directory" 创建一个目录ansible cache -m file -a "path=/tmp/ddd.txt  state=touch" 创建一个文件ansible cache -m file -a "path=/tmp/t  state=link src=/etc/init.d" 创建软连接 path是目标文件 src是源文件ansible cache -m file -a "path=/tmp/t  state=absent " 删除文件

补充 :

ln -s 原文件地址 目标文件地址   创建软连接ln 创建硬链接

 

转载于:https://www.cnblogs.com/dong-/p/10375608.html

你可能感兴趣的文章
[转载,感觉写的非常详细]DUBBO配置方式详解
查看>>
Android在Eclipse上的环境配置
查看>>
面向对象(五)
查看>>
android平台下使用点九PNG技术
查看>>
Python学习3,列表
查看>>
最长回文子串
查看>>
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
算法第5章作业
查看>>
7.9 练习
查看>>
基于ArcGIS JS API的在线专题地图实现
查看>>
learnByWork
查看>>
lua 函数
查看>>
Git的基本命令
查看>>
四平方和
查看>>
第十八周 12.27-1.2
查看>>
C# IP地址字符串和数值转换
查看>>
TCHAR和CHAR类型的互转
查看>>
常用界面布局
查看>>
C语言—— for 循环
查看>>