使用交互式密码分组
#vim /etc/ansible/hosts //删除变量
#ansible all -m command -a 'hostname' -k
#ansible all -m command -a 'id' -k //查看
使用脚本,脚本输出的必须是json格式(ansible方可识别)
#ansible all -m replace -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^(BOOTPROTO=).*" replace="\1none"'
#ansible all -m replace -a 'path="/etc/selinux/config" regexp="^(SELINUX=).*" replace="\1disabled"'
修改配置文件 相同的用copy 不同的 用linefile|replace
yum 模块
使用yum包管理器来管理软件包
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
state:状态(present,absent,latest) #不写状态默认是安装
删除软件包
ansible all -m yum -a 'name="lrzsz" state=absent'
删除多个软件包
ansible all -m yum -a 'name="lrzsz,lftp" state=absent'
安装软件包
ansible all -m yum -a 'name="lrzsz"'
安装多个软件包
ansible all -m yum -a 'name="lrzsz,lftp"'
get_url模块
从服务器上下载一个文件到远程主机指定的目录
ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"
service模块
name:必选项,服务名称
enabled:是否开机启动 yes|no
sleep:如果执行了restarted,在则stop和start之间
沉睡几秒钟
state:对当前服务执行启动,停止、重启、重新加载
等操作(started,stopped,restarted,reloaded)
ansible all -m service -a 'name="sshd" enabled="yes" state="started"'
在web1 web2 上安装apache
并且设置开机启动 启动服务 并且把端口改称8080
修改默认主页为hello world
# vim auto_apache.sh
#!/bin/bash
ansible all -m yum -a 'name="httpd"'
ansible all -m lineinfile -a 'path="/etc/httpd/conf/httpd.conf" regexp="^Listen" line="Listen 8080"'
ansible all -m service -a 'name="httpd" enabled="yes" state="started"'
echo hello world > /root/test.html
ansible all -m copy -a 'src=/root/test.html dest=/var/www/html/index.html'
setup模块
用于获取主机信息
filter 可以过滤到我们需要的信息
ansible t1 -m setup -a 'filter=ansible_distribution'
user模块
-a 'name= state={present(创建)|absent(删除)} force=(是否强制操作删除家目录) system= uid= shell= home='
[root@localhost ~]# ansible all -m user -a 'name=ansible state=present'
user:用户管理模块
常用参数:
name= #指定用户名(必须指定)
state=present/absent #添加/删除用户
system=yse/no #是否创建为系统用户
uid= #指定用户uid
shell= #指定用户shell环境
home= #指定用户家目录
group= #指定用户组
groups= #指定用户组附加组,以”,“分隔
例:
ansible web -m user -a 'name=HR state=present system=yes uid=100 groups=root,ntp shell=/bin/csh home=/home/HR_home'
ansible web -m user -a 'name=HR state=absent' #删除用户
批量创建用户和密码时可以使用该命令
ansible host -m shell -a ‘useradd username’
ansible host -m shell -a ‘echo password | passwd username --stdin’
cron模块
计划任务管理模块
name #任务计划的描述信息(必须指定) minute #分(0-59 ,* ,*/2)
hour #时(0-23 ,* ,*/2)
day #日(1-31 ,* ,*/2)
month #月(1-12 ,* ,*/2)
weekday #周(0-6 ,*)
job=path #执行的命令的绝对路径
backup=yes/no #是否先备份再创建新的任务计划
user #以哪个用户新建任务计划,默认 root
state=present/absent #创建删除计划任务
例:
ansible web -m cron -a 'name=A user=root job="/bin/date &>/dev/null" weekday="*/1" state=present'
-a 'name= state= minute= hour= day= month= weekday= job='
[root@localhost ~]# ansible all -m cron -a 'name=Time state=present minute="*/5" job="/usr/sbin/ntpdate 172.168.0.1 &> /dev/null'' '
synchronize模块
目的:将主控方/root/a目录推送到指定节点的/tmp目录下
命令:ansible all -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
delete=yes 使两边的内容一样(即以推送方为主)
--exclude=.git 忽略同步.git结尾的文件
由于模块,默认都是推送push。因此,如果你在使用拉取pull功能的时候,可以参考如下来实现
mode=pull 更改推送模式为拉取模式