提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、Ansible是什么?
ansible可以作为批量管理工具
二、使用步骤
1.基本配置
代码如下(示例):
进入/etc/ansible/hosts配置主机清单
test1 ansible_host=192.168.2.5 ansible_port=22 ansible_user=root ansible_ssh_pass=123
别名 主机名 端口 用户名 密码
test 主机名(all,别名) -m ping 测试连通性
配置密钥连接
ssh-keygen 生存密钥
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.1.1.60
将指定的公钥交给10.1.1.60的root用户
test1 ansible_host=192.168.2.5 ansible_port=22
| all: children: pro: children: pro1: hosts: 10.1.2.[10:80] pro2: hosts: 10.1.3.[10:80] doe: hosts: 126.25.1.[10:90]
|
all:总组名
children:子组
2.模块
代码如下(示例):
参数
src: 源文件
dest: 目的目录文件
force: 强制执行
backup: 备份,设置为yes,先备份,在执行操作
owner: 拷贝分件后的所属主,远程文件必要存在所属主
group: 拷贝分件后的所属组,远程文件必要存在所属组
mode: 拷贝分件后的权限,设置rw-r--r--,可0644, user 可 u+x
recurse:递归修改目录文件的属性
paths:指定操作的文件或目录
格式:ansible 主机名 -m 模块 -a "[src,path=] dest=.........."
copy模块:
content: 特定文件内容
从远程主机中拉取文件
ansible test10 -m copy -a 'content="aaa\nbbb\n" dest=/opt/test'
ansible test10 -m copy -a "src=/testdir/copytest dest=/opt/ force=no"
file模块:
创建文件或目录、删除文件或目录、修改文件权限
state:dirctory 创建目录
touch 创建文件
hard硬链接 link 软链接
ansible test10 -m file -a "path=/testdir/testdir state=directory" 若存在目录,不执行操作
ansible test10 -m file -a "path=/testdir/linkfile state=link src=sourcefile force=yes"
强制创建软链接
blockinfile模块
插入被标记的文本
block:文本
mark:插入自定义标记
ansible test10 -m blackinfile -a 'path=/test/test5 block="good\ngood" mark="{mark} 12345"
find模块
find_type:查找文件类型any、directory、file、link
patterns:指定查找的文件名,可使用正则表达式
contains:根据文件内容查找文件,正则表达式
age:根据时间
size:根据大小
ansible test10 -m find -a 'paths=/etc/ora/test10 find_type=file contains".*joy.*" recurse=yes'
replace模块
regexp:python正则表达式
ansible test10 -m replace -a 'path=/testdir/test regexp="ASM" replace=asm backup=yes'
service模块
name:操作服务的名称:nginx
sstate:服务状态started stoped restarted reloaded
enabled 开启自启
ansible test10 -m service -a "name=nginx state=started"
command模块
在远程主机上执行命令
chdir:指定目录
creates:不存在就执行
removes:与creates相反
ansible test10 -m command -a "chdir=/testdir ls"
shell模块
与command一样,但支持管道符和通配符
executable:指定shell执行命令,绝对路径
ansible test10 -m shell -a "chdir=/testdir echo test > test"
ansible test10 -m shell -a 'executable=/bin/csh @ TestNum=95 ; echo $TestNum > /testdir/TestNumFile'
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了ansible的学习
5890

被折叠的 条评论
为什么被折叠?



