zoukankan      html  css  js  c++  java
  • ansible

    ansible 批量远程操作主机

    ansible的安装

    1.下载epel源

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

    2.安装ansible

    yum install -y ansible
    

    3.对比salt的优点

    salt 控制节点需要安装salt-master
    salt 被控制节点需要安装salt-minion
    

    4.使用ssh秘钥连接认证连接远程主机方式

    ssh-keygen # 用来生成ssh秘钥对
    ssh-copy-id 192.168.13.131 # 复制秘钥到远程主机
    

    ansible 命令格式

    ansible <host-pattern> [options]
    -a MOUDLE_ARGS, --args=MOUDULES_ARGS # 模块的参数
    -C,             --check # 检查
    -f FORKS,       --forks=FORKS #用来做高并发的
    --list-hosts #列出主机列表
    -m MODULE_NAME #模块名称
    --syntax-check # 语法检查
    -k 输入密码
    

    查看ansible生成的文件

    rpm -ql ansible # 查看命令
    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles
    

    ansible hosts文件

    # This is the default ansible 'hosts' file.
    #
    # 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的机器
    

    host-pattern的格式

    • 单个的主机
    • 全部主机
    • 多个的主机
    • 单个组
    • 多个组
    • 交集 'web : &db'
    • 并集
      • 'web, db'
      • 'web : db'
    • 差集 'web : ! db'

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

     ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
     -j         # 以json的方式返回ansible的所有模块
     -l, --list # 列出所有ansible的模块
     -s         # 以片段的形式显示ansible的帮助信息
    

    命令相关模块

    Command 模块

    ansible web -a 'ls /' # 在web主机组中查看根目录下的文件
    ansible web -a 'pwd'  # 在web主机组中查看各个主机在什么目录下
    ansible web -a 'chdir=/tmp pwd' # 切换目录执行命令,编译安装场景下使用
    
    ansible web -a 'creates=/tmp pwd' # 用来判断/tmp目录是否存在 存在就不执行操作 反之亦然
    ansible web -a 'creates=/test1 pwd' # 因为test1目录不存在 所以执行pwd命令
    
    ansible web -a 'removes=/tmp pwd' # 用来判断/tmp目录是否存在 存在就执行操作 反之亦然
    ansible web -a 'removes=/test1 pwd' # 因为test1目录不存在 所以不执行pwd命令
    

    查看用户是否创建成功

    tail -1 /etc/passwd
    tail -1 /etc/shadow
    
    # 设置用户密码
    echo '123' | passwd --stdin treasure
    

    Shell 模块

    ansible web -m shell -a 'echo "123" | passwd --stdin tresure' # 批量创建密码
    ansible 192.168.13.130 -m shell -a 'bash a.sh'  # 执行远程主机上的文件 方式一
    ansible 192.168.13.130 -m shell -a '/root/a.sh' # 执行远程主机上的文件 方式二 
    # 文件必须有权限 ps:修改权限 chmod +x a.sh
    ansible 192.168.13.130 -m shell -a 'python b.py' # 执行远程主机上的python脚本文件
    

    Script 模块

    ansible web -m script -a '/root/m.sh'  # 在远程主机上执行本机上的文件,管控机的文件
    ansible web -m script -a 'removes=/root/m.sh /root/m.sh'
    # 用来判断被管控机上是否有m.sh文件 如果存在就执行 /root/m.sh 
    ansible web -m script -a 'creates=/root/m.sh /root/m.sh'
    # 用来判断被管控机上是否有m.sh文件 如果不存在就执行 /root/m.sh
    

    文件相关模块

    Copy 模块

    # 通过MD5加密值来判断文件是否需要复制
    # 参数:
    #	backup  备份,以时间戳结尾
    #	dest    目的地址, 要把文件复制到什么地方
    #	group   文件的属组
    #	owner   文件的属主
    #	mode    文件的权限 r-4 w-2 x-1
    #	src     源文件地址
    #   content 直接复制文本
    ansible -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' 
    # 将本地文件复制到远程主机上
    ansible -m copy -a 'src=/root/m.sh dest=/tmp/b.sh mode=755' 
    # 将本地文件复制到远程主机上并且修改权限
    ansible -m copy -a 'src=/root/m.sh dest=/tmp/b.sh mode=755 owner=treasure' 
    # 将本地文件复制到远程主机上并且修改权限和属主
    ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=treasure'
    # 如果要复制本地的文件夹 那么远程主机的目的地址也必须是一个文件夹
    ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=treasure'
    # 将本地文件夹内的文件复制到远程主机的文件夹下
    ansible web -m copy -a "content='天将降大任于斯人也,必先苦其心志
    ' dest=/tmp/b.txt" 
    # 直接将文本内容注入到远程主机的文件中
    

    Template 模块

    # 用法与Copy模块相同
    # 区别:
    #   copy 模块不替代参数
    #   template模块代替参数
    

    ps:写相对路径: 在当前目录下新建一个templates目录,然后把文件放在templates目录里面

    File 模块

    # 参数:
    #	state
    #	  directory  # 创建的文件夹 
    #	  touch		 # 创建的是文件
    #	  link	     # 创建的是一个软连接(快捷方式) ps:指向同一个inode
    #	  hard       # 创建的是一个硬连接          ps:指向的也是同一个inode
    #	  absent	 # 删除文件/文件夹
    #	  file		 # 如果文件不存在就不创建 ¿¿¿
    #	src          # 源文件地址
    #	  link  	 # 和src一起出现 
    #	  hard  	 # 和src一起出现
    #	path  		 # 目标地址,要对哪个位置的文件进行处理
    ansible web -m file -a 'path=/testx  state=directory'  
    # 在远程主机根目录下创建testx文件夹
    ansible web -m file -a 'path=/testx/t1.txt  state=touch'
    # 在远程主机的/testx目录下创建t1.txt文件
    ansible web -m file -a 'path=/testx/t2 src=/testx/t1.txt  state=link'
    # 创建一个t1.txt文件的软连接
    ansible web -m file -a 'path=/testx/t2 state=absent'
    # 删除t2文件
    

    Fetch 模块

    # 参数:
    #	dest 目的地址
    #   src  源地址
    ansible web -m fetch -a 'src=/var/log/cron dest=/tmp'
    # 下载被控节点的文件,每台机器创建一个文件夹,并保留原来的目录结构
    

    Yum 模块

    rpm 和 yum 的区别
    rpm 无法解决包依赖的问题 yum 可以解决包依赖问题
    
    yum源配置信息
    [epel]
    name=Extra Packages for Enterprise Linux 7 - $basearch #名字
    baseurl=http://mirrors.aliyun.com/epel/7/$basearch  #rpm源的地址,可以写http,https,ftp,Samba,file:
    failovermethod=priority
    enabled=1 # 是否开启,1代表开启,0表示关闭
    gpgcheck=0  #是否校验签名,1代表校验,0表示校验
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    
    yum 安装包组
    yum grouplist # 查看包组信息
    yum groupinstall # 安装包组
    
    ansible的yum模块
    # 参数:
    #	disablerepo # 禁用源
    #	enablerepo  # 启用源
    #	name		# 包名
    #	state  install (`present' or `installed', `latest'), or remove (`absent' or `removed')
    ansible web -m yum -a 'name=weget'             # 安装wget
    ansible web -m yum -a 'name=python2-pip'       # 安装python2-pip
    ansible web -m yum -a 'name=wegt state=absent' # 卸载软件包
    ansible web -m yum -a 'name@Development Tools' # 安装包组
    

    PIP 模块

    pip install 		 安装包
    pip freeze > a.txt   将python项目的环境打包到文件中
    pip install -r a.txt 安装a.txt中的python项目依赖
    pip list 			 查看所有已经安装成功的包
    
    ansible -m pip -a 'name=django' # 安装django模块
    

    计划任务相关

    * * * * * job 
    分 时 日 月 周 任务
    # 例子
    0 */2 * * * job   		  # 每隔两个小时 执行一次任务
    0 12,13 * * * job 		  # 每天12点,13点 执行一次任务
    0 12-17 * * * job 		  # 每天12点到17点 每小时执行一次任务
    0 12-17/2 * * 1,3,6,0 job # 周1,3,6,7 12点到17点每隔2个小时执行一次任务
    crontab -e # 编辑计划任务
    crontab -l # 查看计划任务
    crontab -r # 删除计划任务
    

    Corn 模块

    # 参数:
    # 	day 天
    # 	disable 禁用
    # 	hour 小时
    # 	job 任务
    # 	minute 分钟
    # 	mouth 月
    #	name 任务名字
    # 	weelday 周
    
    ansible -m cron -a 'minute=26 job=touch /tmp/xzmly.txt name=touchfile' # 计划一个新任务
    ansible -m cron -a 'name=touchfile state=absent' 					   # 删除一个任务
    ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes'  # 禁用计划任务,以#表示禁用
    

    用户相关

    用户: 
    	管理员 root uid 0
    	普通用户
    		系统用户  不能登录 uid 1-999 centos7 1-499 centos6
    		登录用户  可以登录 uid 1000-65535 centos7 500-65535 centos6
    用户组:
    	管理员组 root gid 0
    	普通用户组
    		系统用户组   gid 1-999 centos7 1-499 centos6
    		登录用户组	gid 1000-65535 centos7 500-65535 centos6
    
    创建用户/用户组:
    	-d 指定用户的家目录
    	-g 指定用户的组
    	-G 指定用户的附加组
    	-s 指定登陆后使用的shell
    	-r 创建一个系统用户/组
    	useradd -r wusir 创建系统用户 ps: uid从999倒序
    	useradd -s /sbin/nologin treasure 创建的是普通用户 ps: uid从1000升序
    	useradd -d /opt/treasure2 treasure2 指定用户的家目录为/opt/treasure2
    	useradd -u 3000 treasure3 创建用户并指定uid为3000
    	userdel treasure3 删除用户
    	userdef -r treasure3 删除用户并删除用户的家目录
    	
    	groupadd tse 创建用户组
    	groupdel tse 删除用户组
    

    User模块

    # 参数:
    #	group    组
    #	groups   附加组
    #	home     家目录
    #	name     用户名
    #	password 密码
    #	remove   删除用户并删除用户的家目录
    #	shell    指定用户登陆后的shell
    # 	system 	 创建一个系统用户
    #	uid      用来指定用户的id
    #	state	 状态
    ansible web -m user -a 'name=treasure uid=4000 password=t123 home=/opt/treasure' 
    # 创建一个用户指定用户的uid,家目录,密码
    ansible web -m user -a 'name=treasure state=ansent' 
    # 删除一个用户但是不删除用户的家目录
    ansible web -m user -a 'name=treasure state=ansent remove=yes' 
    # 删除用户并删除用户的家目录
    

    Group 模块

    # 参数:
    #	gid    用户组的id
    #	name   组名
    #	system 系统组
    #	state
    ansible web -m group -a 'name=tse gid=2000' 
    # 创建一个新的用户组并指定用户组id为2000
    ansible web -m group -a 'name=test system=yes'
    # 创建一个新的系统组
    ansible web -m group -a 'name=test state=absent'
    # 删除用户组
    

    其他模块

    Service 模块

    ps -ef | grep nginx     # 查看nginx进程
    ss -tnlp 			    # 查看端口号
    systemctl start nginx   # 启动nginx服务 centos7
    service nginx start     # 启动nginx服务 centos6
    systemctl enabled nginx # niginx 服务开机自启动
    chkconfig nginx on 		# nginx 服务开机自启动
    
    ansible web -m service -a 'name=nginx state=started' # 启动nginx
    ansible web -m service -a 'name=nginx state=stoped'  # 关闭nginx
    

    补充

    linux系统自带的ping基于的是ICMP协议;
    inode 硬盘地址
    id 获取到的是内存地址
    ln -s a.py b.py 创建硬链接
    ln a.py c.py 创建软连接
    当源文件发生变化时, 软连接和硬链接都会发生变化
    
  • 相关阅读:
    ECS:Systems
    ECS:Components
    ECS:Entitias
    对List遍历过程中添加和删除的思考
    SpringBoot 推荐博客
    理解和解决Java并发修改异常ConcurrentModificationException(转载)
    我对CopyOnWrite的思考
    最简日志打印规范(推荐,转载)
    为什么不应该使用Zookeeper做服务发现?(转载)
    浏览器允许跨域设置(不用于生产环境,开发用)
  • 原文地址:https://www.cnblogs.com/Treasuremy/p/10402186.html
Copyright © 2011-2022 走看看