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" # 删除组

ansible - 基本用法的更多相关文章

  1. Ansible 详细用法说明(二)

    setup:获取指定主机的facts. ===================================facts就是变量,内建变量 .每个主机的各种信息,cpu颗数.内存大小等.会存在fact ...

  2. Ansible 详细用法说明(一)

    一.概述 运维工具按需不需要有代理程序来划分的话分两类: agent(需要有代理工具):基于专用的agent程序完成管理功能,puppet, func, zabbix agentless(无须代理工具 ...

  3. 02 . Ansible高级用法(运维开发篇)

    自动化任务简介 假设我们要在10台linux服务器上安装一个nginx服务,手动是如何做的? # 第一步, ssh登录NUM(1,n)服务器 # 第二步,输入对应服务器密码 # 第三步,执行命令: y ...

  4. ansible高级用法

    将多个符合正则的文件拷贝到目标机器 - name: Copy copy: src={{ item }} dest=/root/.sshkeys mode=0600 owner=root group=r ...

  5. ansible 简单用法

    ansible批量复制文件到服务器 ansible -i /etc/ansible/hosts.test test -m copy -a "src=/tmp/test dest=/usr/b ...

  6. 集群工具ansible使用方法

    ansible简介 ansible是与puppet.saltstack类似的集群管理工具,其优点是仅需要ssh和Python即可使用,而不像puppet.saltstack那样都需要客户端.与pupp ...

  7. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  8. 初探ansible

    Ansible 基于ssh的自动化运维工具 ansible 配置文件详解 ansible.cfg 文件 文件默认放置在/etc/ansible下,ansible读取配置文件的顺序是: 当前命令执行目录 ...

  9. ansible 基础一

    安装 解决依赖关系: yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto tar包安装 htt ...

随机推荐

  1. org.apache.solr.handler.dataimport.DataImportHandlerException: Data Config problem: 对实体 &quot;characterEn

    解决的方法:在配置数据库连接是讲url里的特殊符号要做转义 jdbc:mysql://IP:3306/数据库名?useUnicode=true&characterEncoding=utf8 改 ...

  2. 配置hadoop集群一

    花了1天时间最终把环境搭建好了.整理了一下,希望对想学习hadoop的有所帮助. 资料下载:http://pan.baidu.com/s/1kTupgkn 包括了linux虚拟机.jdk, hadoo ...

  3. CentOS6.5下用Git克隆代码(https方式)

    一.首先最好保证GIT是最新版 查看GIT命令 $ git --version 有关git的安装,应该有好多文章介绍.注意更新之后,要重启系统,否则显示的版本号,还是老版本. 二.如果工作环境存在网络 ...

  4. jsp jquery js 的基本路径获取

    引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决!   1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...

  5. Sublime Text3 配置 Lua5.3.5开发环境

    所需软件 Sublime Text3 Lua5.3.5 配置过程 解压Lua5.3.5包 官方下载的包内是需要makefile安装的(博主Win10下暂为实现),此处提供自动配置完毕的包:Lua5.3 ...

  6. 汇编程序45:检测点13.2 (loop指令的中断例程)

    安装程序: assume cs:code //loop指令的替代实现 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax ...

  7. excel另存为csv

    # -*- coding: utf-8 -*- """ Created on Tue Jul 11 21:25:57 2017 import pandas as pd i ...

  8. 函数 out 传值 分割

    public void Jia(int a ,int b) { a = a + b; Console.WriteLine(a); } public void Jia1(int a,out int b) ...

  9. linux设置库文件加载包含路径

    第一种方式vim /etc/ld.so.conf 将要包含的路径添加到此文件中退出重新登录使配置生效或者执行命令source /etc/ld.so.conf 另一种方式利用LIBRARY_PATH和L ...

  10. Android 在fragment中实现返回键单击提醒 双击退出

    尝试用mvp架构加dagger2来重写了一下,大致功能都实现了,还没有全部完成. 项目地址 接近完成的时候,想在天气信息页面实现一个很常见的功能,也就是点击屏幕下方的返回键的时候不是返回到上一个act ...