ansible

ansible - 01#

一. 安装与使用

1、安装epel源

yum install -y wget # 安装wget

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 下载epel源文件

2、安装ansible

yum install ansible -y

ansible命令格式

Usage: ansible <host-pattern> [options]
-a MODULE_ARGS, --args=MODULE_ARGS # 模块的参数
 -C, --check  # 会去执行,但是不做任何的改变,干跑,白跑
 -f FORKS, --forks=FORKS # 指定进程数,做并发
 --list-hosts #列出主机
 -m MODULE_NAME # 模块名称
 --syntax-check #检查语法
 -k, --ask-pass      ask for connection password #指定密码

查看ansible生成的配置文件

rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles
10.0.0.132
10.0.0.133
10.0.0.134

ssh认证方式

  • 密码

  • 秘钥

      	ssh-keygen # 生成秘钥
    
      	ssh-copy-id root@10.0.0.132 # 将秘钥文件复制到远程主机
    

ansible的第一个命令

ansible 10.0.0.132 -m ping
ansible 10.0.0.133 -m ping
ansible 10.0.0.134 -m ping
ansible all -m ping # 所有机器,
	hosts文件里面
ansible 10.0.0.133,10.0.0.132 -m ping # 部分机器
## 分组信息
[web]
10.0.0.132
10.0.0.133
[db]
10.0.0.133
10.0.0.134
[cache]
10.0.0.134
## www[001:006].example.com 从www001到www006
ansible web --list-hosts # 用来获取符合条件的主机
ansible web -m ping # 探测组内的机器
ansible web,db -m ping # 获取db和web的并集
ansible 'web:&db' -m ping # 获取db和web的交集
ansible 'web:!db' -m ping # 获取db和web的差集,在web中但是不在db中的
ansible 'web:db' -m ping # 获取db和web的并集

弱口令校验

密码要符合的规则

  • 必须有大写字母,小写字母,数字,特殊字符
  • 密码必须12位以上
  • 密码需要三个一换

host-pattern的格式

  • 单个的主机

      ansible 127.0.0.1 -m ..
      逗号分隔
    
  • 单个组

      ansible 组名 -m ...	
    
  • 多个组

    • 交集

      • ‘web:&db’
    • 并集

      • ‘web:db’
      • web,db
    • 差集

      • ‘web:!db’
  • 所有的机器 all

  • 多个主机

模块

获取模块帮助信息

Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-j, --json #以json的方式返回所有模块的信息
-l # 列出所有的模块
-s, --snippet # 以片段式显示模块的帮助信息
# 显示全部信息
ansible-doc -l |wc -l #统计ansible的模块

command

执行远程主机上的命令, 不特殊字符支持

chdir #切换目录,一般在编译安装
creates # 判断是否存在,如果存在,就不执行,如果不存在,就执行
removes # 如果不存在,就不执行,如果存在,就执行

ansible web -m command -a "pwd" 
ansible web -m command -a "ls /tmp"
ansible web -m command -a "chdir=/tmp pwd" # 切换目录,一般做编译安装
ansible web -m command -a "creates=/tmp pwd" # 不被执行,因为/tmp已经存在,
ansible web -m command -a "creates=/tmp2 pwd" # 被执行,因为/tmp2目录不存在
ansible web -m command -a "creates=/tmp2 mkdir /data" # 会被执行,因为/tmp2目录不存在
ansible web -m command -a "removes=/tmp2 pwd"   # 不被执行,因为/tmp2目录不存在
ansible web -m command -a "removes=/tmp pwd" # 会被执行,因为/tmp已经存在,

补充

#查看用户创建成功与否
1.ll /home
2.tail -1 /etc/passwd
3.tail  /etc/shadow
4.id alex
echo "alex3714" |passwd --stdin alex # 给用户设置密码,不需要二次确认
[root@localhost ~]# name=alex
[root@localhost ~]# echo "$name"
alex
[root@localhost ~]# echo '$name'
$name
shabang

shell

执行远程主机的命令或脚本

ansible web -m shell -a "echo 'alex'|passwd --stdin alex" # 给用户设置密码
ansible 10.0.0.132 -m shell -a "bash a.sh" # 执行shell脚本
ansible 10.0.0.132 -m shell -a "./a.sh"
ansible 10.0.0.132 -m shell -a "/root/a.sh"
ansible 10.0.0.132 -m shell -a "/root/a.py" # 执行python脚本
ansible 10.0.0.132 -m shell -a "python a.py" 
# shell 脚本
#!/bin/bash
mkdir /alex2sb11
# python脚本
#!/bin/env python 
#coding:utf-8
print "停车坐爱枫林晚,霜叶红于二月花" 

script

执行本机的脚本

ansible db -m script -a "/root/a.sh" # 执行的是本地的脚本,管控机上的脚本
ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判断是远程主机是否存在,如果存在,就不执行,如果不存在,就执行
ansible db -m script -a "removes=/root/a.sh /root/a.sh" # 判断的主机是否存在,如果存在,就执行,如果不存在,就不执行

copy

将本地的文件复制到远程主机上

backup # 创建备份文件,以时间戳结尾,
content # 直接写内容
dest # 目标地址
group #文件的属组
mode # 文件的权限W 2 R 4  X 1
owner #文件的属主
src # 原文件

 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh" # 复制文件
 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755" # 复制文件,并修改文件的权限
 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex"  #复制文件,修改文件的权限,属主,根据md5值来判断
 ansible db -m copy -a "src=/etc/init.d dest=/tmp/" # 复制文件夹
 ansible db -m copy -a "src=/etc/init.d/ dest=/tmp/" # 复制文件夹下面的所有文件
 ansible db -m copy -a "src=/etc/init.d dest=/tmp/ owner=alex " # 复制文件夹,并改变文件夹的属性,文件夹的文件的属性也会跟着改变
 ansible db -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/a.sh" # 直接写文字,覆盖写入,要慎用
 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex backup=yes"  #备份文件,如果远程机器上没有要备份的文件,即使指定了backup=yes 也不会去备份文件

file

在远程主机上创建文件夹, 文件, 软连接, 硬链接

access_time # 访问事件
group # 属组
mode #权限
owner #属主
path #路径
src # 原文件,link和hard的时候使用
state:
	directory 文件夹
	file
	touch 空文件
	link  软连接
	hard  硬链接
	absent 删除

ansible db -m file -a "path=/tmp/baoyuan state=directory" # 创建一个目录
*-

88
ansible db -m file -a "path=/tmp/baoyuan.txt state=touch owner=alex mode=644" # 创建一个文件,并制定属主,权限
ansible db -m file -a "path=/tmp/f src=/etc/fstab state=link" # 创建一个软连接
ansible db -m file -a "path=/tmp/f state=absent" # 删除

补充

软连接  windows的快捷方式  ln —s 原文件 目标文件  源文件改变,目标文件也改变  可以跨越分区  原文件删除,链接失效
硬链接  指向同一个硬盘的地址 ln 原文件 目标文件  原文件改变,目标文件也改变 不可以跨域分区 原文件删除,不会受影响

fetch

将远程机器上的文件拉取到本地, 以ip或者主机名生成目录,并保结构留原来的目录

dest  #目标地址

src   #源地址

ansible web -m fetch -a "dest=/tmp src=/var/log/cron"

yum

1.yum 和rpm的区别

rpm redhat package manage
yum会解决依赖关系

2.yum源的配置

[epel]     # 名称
name=Extra Packages for Enterprise Linux 7 - $basearch   # 描述信息
baseurl=http://mirrors.aliyun.com/epel/7/$basearch  # yum源的地址
failovermethod=priority
enabled=1     # 指定yum源是否可用,1代表可用,0代表不可用
gpgcheck=0    # 是否检查gpgkey文件,0代表不检查,1代表的检查
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

3.包组

linux lib  windows dll  、
rpc 远程过程调用
yum grouplist #查看包组
yum groupinstall # 安装包组rpm

4.查看包安装状态

yum list|grep redis @代表安装成功
rpm -q redis 
rpm -qa 查看所有的包
rpm -ql 查看包安装生成的文件

.

disable_gpg_check # 是否要检,查key
disablerepo # 禁用repo
enab
name # 包名
state # 状态 installed removed
ansible web -m yum -a "name=python2-pip" # 安装一个包
ansible web -m yum -a "name='@Development Tools'" # 安装包组
ansible web -m yum -a "name=redis,python2-pip" # 同时安装多个包
ansible web -m yum -a "name=redis state=absent" # 卸载

pip

pip list #查看所有的python第三方包
pip freeze > a.txt # 导出pi
pip install -r a.txt # 安装

.

requirements #导出的文件
name # 包名
virtualenv # 虚拟环境
ansible web -m pip -a "name=django==1.11.18" # 安装
ansible web -m pip -a "name=flask  

service

ps -ef|grep redis # 查看进程
ss -tnlp #查看端口信息
# 启动服务
systemctl start redis  centos7
service redis start    centos6
# 开机自启动
systemctl enable redis centos7
chkconfig redis on centos6

.

enabled # 设置开机自启动
name # 名称"
state
	startedansi	
	stopped
	restarted
	reloaded
ansible web -m service -a "name=redis state=started" # 启动
ansible web -m service -a "name=redis state=stopped" # 关闭
ansible web -m service -a "name=redis state=started enabled=yes" # 启动并设置开机自启动

crontab

* * * * * job
分 时 日 月 周 任务
1 * * * * job    # 代表每小时的第一个分钟
2/* * * * * job  # 每隔2分钟执行job
1 10-19 * * * job  # 代表10到19点的第一分钟
0-59 0-23 1-31 1-12  0-7 job
* * * * * tar -zcf /opt/etc.tar.gz /etc
分钟不要用*,最好是指定时间
分钟不要用*,最好是指定时间
分钟不要用*,最好是指定时间
收集日志
备份数据
同步时间

crontab -l # 查看计划任务
crontab -r # 删除所有的计划任务
crontab -e # 编辑计划任务

.

day # 天
hour # 小时
job #任务
minute #分钟
month # 月
name #名字,描述信息,不可以重复
state # 状态
user # 执行计划任务的用户
weekday # 周
disabled # 禁止
ansible web -m cron -a "minute=21 job='touch /tmp/cron.txt' name=touchfile" # 设置计划任务
ansible web -m cron -a "minute=23 job='touch /tmp/cron.txt' name=touchfile4 disabled=yes" # 禁用计划任务,表现为加注释
ansible web -m cron -a "name=touchfile4 state=absent" # 删除计划任务

user

用户的分类
超级管理员   root                      0
其他用户  
	系统用户 启动服务来专门设置的用户      1-999 centos7   1-499 centos6
	登陆用户 普通的登陆用户              1000-65535centos7 500-65535 centos6
	
useradd 
 -d # 指定家目录
 -g # 组id
 -G, --groups GROUPS  # 附加组
 -r, --system  # 创建系统用户
  -s, --shell SHELL # 登陆shell
  -u, --uid UID #用户id
   useradd -s /sbin/nologin -u 2000 -d /opt/wusir wusir #创建用户,指定用户的登陆shell,id,家目录
   useradd -s /sbin/nologin -G root,wusir  -d /opt/wusir2 wusir2 #指定附加组,最大的后面+1
   useradd -r baoyuan # 创建系统用户,从999倒序
userdel wusir2 # 删除用户
userdel -r baoyuan4 # 删除用户并删除用户的家目录 

.

group # 组
groups #附加组
home #家目录
name #用户名
password #密码
shell #登陆shell
remove # 删除用户并删除用户的家目录
state # 状态
system #系统用户
uid # 用户id
ansible db -m user -a "name=alex2 shell=/sbin/nologin home=/opt/alex2 uid=2000 group=root" # 创建用户,并指定用户的家目录,登陆shell,uid,组
ansible db -m user -a "name=alex3 system=yes" #创建系统用户
ansible db -m user -a "name=alex3 state=absent" # 删除用户
ansible db -m user -a "name=alex2 state=absent remove=yes" # 删除用户并删除用户的家目录

group

用户组的分类
超级组   root                      0
其他组  
	系统组   1-999 centos7   1-499 centos6
	普通组   1000-65535centos7 500-65535 centos6
groupadd 
-g 指定组的id
-r 指定系统组
groupdel 

.

gid #组的id
name # 组名
state #状态
system #系统组
ansible db -m group -a "name=canglaoshi" #创建普通组
ansible db -m group -a "name=wutenglan system=yes" # 创建系统组
ansible db -m group -a "name=wutenglan state=absent" # 删除组
原文地址:https://www.cnblogs.com/konghui/p/10703544.html