安装ansible

yum install epel-release -y
yum install ansible -y

ansible是基于ssh协议,所以进行免密码登录

ssh-keygen #生成秘钥对
ssh-copy-id root@10.50.2.129 #发送秘钥对到目标主机

在/etc/ansible/hosts文件中可设置相应的组,里面有例子

可通过man文档进行查看ansible使用方法
ansible-doc -l #查看可用的模块
ansible-doc -s 模块名 #查看模块的使用方法

以下是常用模块

command

例子:ansible test -a ‘date’

cron

任务调度
state: present为安装 absent为删除
例子:ansible test -m cron -a 'minute=*/10 job=“/bin/echo hahah” name="cron_test"'
如需删除可添加state=absent

user

name=:指明创建用户的名字
例子:ansible test -m user -a 'name=xh uid=306 system=yes '

group

例子:ansible test -m group -a 'name=xh gid=306 system=yes'

copy

src=:定义本地文件路径
dest=:定义远程目标路径
content=:表示直接使用此处的信息取代dest的文件信息内容
例子:ansible test -m copy -a 'src=~/aa dest=/root/'

file 设定文件属性

例子:ansible test -m file -a 'path=/root/aab src=/root/aa state=link'

service

例子:ansible test -m service -a 'name=httpd enable=true state=start'

shell

远程主机上运行命令,尤其是管道等复杂的命令

script

例子:ansible test -m script -a 'test.sh'
注意是运行了本地脚本

yum

name=:安装的安装包
state=:present,latest表示安装,absent表示卸载

setup

收集远程主机的facts
例子:ansible test -m setup