ansible基本使用:

ansible的常见使用格式:

ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS -C -F FORKS

HOST-PATTERN:指定主机匹配模式

-m:指定模块名

-a:指定参数

-C:干跑

-F:指定一次管理多少主机

在 /etc/ansible/hosts中输入被管理主机的IP地址:
[websrvs]
受管控IP地址1

受管控IP地址2

[dbsrvs]
受管控IP地址3

受管控IP地址4

基本使用:

ansible all -m ping # 测试所有主机是否ping通
ansible all -m ping --list-hosts # 列出所有被管控主机

ansible-doc -l  # 列出所有模块

# 在所有被管控主机上创建一个GID为3000,名称为mygrp的非系统用户组
ansible all -m -a "gid=3000 name=mygrp state=present state=no"
# 删除被管控主机上一个GID为3000,名称为mygrp的用户组
ansible all -m group -a "gid=3000 name=mygrp state=absent system=no"

# 在所有被管控主机上创建一个UID为5000,名称为testuser,附加组为mygrp,且默认shell为/bin/bash的用户
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible mode=600"

# 在所有被管控主机上把源文件从/etc/fstab复制到目的地/tmp/fstabl.ansible,权限为600
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible mode=600"

# 在被管控主机上把/etc/pam.d/目录下所有内容复制到/tmp/pam/下
ansible all -m copy -a "src=/etc/pam.d/ dest=/tmp/pam/"

# 在被管控主机上把/etc/pam.d目录复制到/tmp/pam/下
ansible all -m copy -a "src=/etc/pam.d/ dest=/tmp/pam/"

# 直接生成内容写到/tmp/hi.txt
ansible all -m copy -a "content='hi there\n' dest=/tmp/hi.txt"

# 直接生成内容写到/tmp/hi.txt,属主为testuser, 属组为mygrp
ansible all -m copy -a "content='hi there\n' dest=/tmp/hi.txt owner=testuser group=mygrp"


ansible常用模块:
获取模块列表:ansible-doc -l
command模块:在远程主机上运行命令
shell模块:在远程主机在shell进程下运行命令,支持shell特性,如管道等
copy模块:复制文件到远程位置
        用法:
        1、src= dest=
        2、content= dest=
        owner group mode
cron模块:管理cron.d和crontab条目
minute=
day=
month=
weekday=
hour=
job=
*name=
state=
        present:创建
        absent:删除

# 在受管控主机上执行ifconfig命令
ansible all -m command -a "ifconfig"

# 在受管控主机上执行命令,切换到tmp下创建目录fold:
ansible all -m command -a "chdir=/tmp mkdir fold"

# 在受管控主机上执行-a后面的命令:更改用户testuser的密码为xrdxrd
ansible all -m shell -a "echo xrdxrd | passwd --stdin testuser"

# 在受管控主机上/usr/tmp/目录下创建一个目录hello.dir
ansible all -m file -a "path=/var/tmp/hello.dir state=directory"
# 在受管控主机上/usr/tmp/目录下创建一个空文件hello.txt
ansible all -m file -a "path=/var/tmp/hello.txt state=touch"

# 在受控主机上复制/etc/fstab文件到/var/tmp/fstab.ansible
ansible all -m copy -a "src=/etc/fstab dest=/var/tmp/fstab.ansible"

# 在受控主机上为/var/tmp/fstab.ansbile文件创建一个符号链接/var/tmp/fstab.link
ansible all -m file -a "src=/var/tmp/fstab.ansible path=/var/tmp/fstab.link state=link"

# 定义三分钟定时任务
ansible all -m cron -a "minute=*/3 job='/usr/sbin/xxx &> /dev/null' name=None state=absent"

# 在受控主机上用yum安装nginx
ansible all -m yum -a "name=nginx state=installed"

# 在受控主机上启动nginx服务:state设置是否启动 enabled设置是否开机自启动
ansible all -m service -a "name=nginx state=started"
# 在受控主机上停止nginx服务:
ansible all -m service -a "name=nginx state=stopped"

在/tmp/scrpit.sh中写一个脚本:
#!/usr/bin/bash

echo -e "hello world! Ansible!\n"
# 在受控主机上执行本机脚本/tmp/script.sh
ansible all -m service -a "/tmp/script.sh"
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值