zoukankan      html  css  js  c++  java
  • 自动化运维 ansible

    ansible

    首先安装虚拟机的时候没有ip

    查看ip ip addr ifconfig

    vi /etc/sysconfig/network-scripts/ifcfg-ens33

    修改最后一行 reboot =yes

    systemctl restart network

    获取阿里镜像元

    https://opsx.alibaba.com/mirror

    找到epel 帮助

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

    要是没有wget 命令 就先下载一个 yum install wget

    yum install -y ansible
    

    salt 控制节点需要安装slat-master

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

    ansible 通过ssh来连接并控制被控节点和进行操作

    ssh的认证方式

    • 密码连接
    • 秘钥连接

    ssh秘钥登录

    ssh-keygen  # 用来生成ssh的秘钥对
    ssh-copy-id  + ip  地址 # 复制秘钥到远程主机
    

    ansible命令格式

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

    查看ansible生成的文件

    rpm -ql ansible | head
    
    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles
    /usr/bin/ansible
    /usr/bin/ansible-2
    /usr/bin/ansible-2.7
    /usr/bin/ansible-config
    /usr/bin/ansible-connection
    /usr/bin/ansible-console
    
    

    ansible hosts文件解读

    # This is the default ansible 'hosts' file
    #
    # It should live in /etc/ansible/hosts
    #
    #   -Comments begin with the '#' character # 用来表示注释
    #   -Blank lines are igore # 空白行被忽略
    #   -Groups of hosts are delimited by [header] elements
    	主机组 需要在[] 下面 | 主机组由[header]元素分隔
    	
    #   - You can enter hostnames or ip multiple groups 
    	一台主机可以在多个组里面
    www[001:006].example.com # 表示www001到www006的机器
    

    host-pattern的格式

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

    ansible-doc 查看模块帮助信息

    Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
    
    plugin documentation tool
    
    Options:
      -a, --all             **For internal testing only** Show documentation for
                            all plugins.
      -h, --help            show this help message and exit
      -j, --json            **For internal testing only** Dump json metadata for
                            all plugins.
      -l, --list            List available plugins
      -F, --list_files      Show plugin names and their source files without
                            summaries (implies --list)
      -M MODULE_PATH, --module-path=MODULE_PATH
                            prepend colon-separated path(s) to module library
                            (default=[u'/root/.ansible/plugins/modules',
                            u'/usr/share/ansible/plugins/modules'])
      -s, --snippet         Show playbook snippet for specified plugin(s)
      -t TYPE, --type=TYPE  Choose which plugin type (defaults to "module")
      -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                            connection debugging)
      --version             show program's version number and exit
    
    
    缩略版本
     ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
     -j # 以json的方式返回ansible的所有模块
     -l, --list # 列出所有的ansible的模块
     -s # 以片段式显示ansible的帮助信息
    
    

    tips 系统自带的ping走的是ICMP协议

    命令相关模块

    command

    ansible web -a 'ls /' # 第一个命令
    
    ansible web -a 'pwd' # 查看当前所在目录
    
    ansible web -a 'chdir=/tmp pwd' 切换目录执行命令,使用场景:是编译安装时使用
    
    ansible web -a 'creates=/tmp pwd' # 用来判断/tmp目录是否存在,存在就不执行
    
    ansible web -a 'creates=/data pwd' # 因为data不存在,所以才会执行
    
    ansible web -a 'removes=/tmp pwd' #用来判断tmp是否存在,存在就执行此操作
    
    ansible web -a 'removes=/data pwd' # 因为data不存在,所以才不会执行
    
    
    

    查看用户是否创建成功

    [root@localhost ansible]# useradd alex
    [root@localhost ansible]# tail -1 /etc/passwd
    alex:x:1000:1000::/home/alex:/bin/bash
    [root@localhost ansible]# tail -1 /etc/shadow
    alex:!!:17946:0:99999:7:::
    [root@localhost ansible]# id alex
    uid=1000(alex) gid=1000(alex) groups=1000(alex)
    [root@localhost ansible]# echo 'alex' | passwd --stdin alex
    Changing password for user alex.
    passwd: all authentication tokens updated successfully.
    [root@localhost ansible]# 
    
    
    tail -1 /etc/passwd
    tail -1 /etc/shadow 意为影子
    id alex
    echo 'password' | passwd --stdin 'username' # 设置密码
    

    shell

    ansible web -m shell -a 'echo "password" | passwd --stdin alex' # 批量创建密码
    
    执行远程文件
    ansible ip -m shell -a 'bash a.sh' # 执行远程文件方式一
    ansible ip -m shell -a '/root/a.sh'# 执行远程方式二,文件必须有执行权限 x
    
    ansible ip -m shell -a '/root/a.py' 执行远程的python脚本
    
    

    script

    ansible web -m script -a '/root/m.sh' # 执行本地的文件,执行管控机上的文件
    
    ansible web -m script -a 'removes=/root/m.sh /root/m.sh'
    # 用来判断被管控机上是不是存在文件,如果存在,存在即执行,不存在即跳过
    
    ansible web -m script -a 'creates=/root/a.sh /root/m.sh'
    # 用来判断被管控机上是不是存在,存在即跳过
    
    

    文件相关操作

    copy

    backup 备份,以时间戳结尾
    dest 目的地址 destination 目的地
    group 文件的属组
    mode 文件的权限 r 4 w 2 x 1
    owner 文件的属主
    src 源文件
    # 通过md4码来判断是否需要复制
    
    ansible db -m copy -a 'src=/root/a.txt dest=/tmp/a.txt'
    # 复制本地文件到远程主机
    
    amsible db -m copy -a 'src=root/a.txt dest=/tmp/a.txt mode=755' 修改文件的权限
    
    ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=alex'  修改文件的属组
    
    ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=alex' 复制本地目录到远程主机,如果改变文件的属性,则文件夹内的文件也会改变
    复制整个文件夹 包括文件
    
    
    ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/1/ mode=755 owner=alex' 复制本地目录中国的所有文件 到目的地址
    
    ansible web -m copy -a 'content="杰森斯坦森
    " dest=/tmp/a.txt ' 直接将文本内容注入 覆盖到远程主机
    
    

    file

    tips

    inode 硬盘的地址
    id 获取到的是内存地址
    ln -s a.py b.py 创建软连接
    ln a.py c.py 创建硬链接
    当 源文件发生变化时,软连接和硬链接都会随着发生变化
    
    
    ansible db -m file -a 'path=/smlz state=directory' 
    # 在远程机器上创建文件夹
    
    ansible db -m file -a 'path=/root/q.txt state=touch'
    # 在远程机器中创建文件
    
    ansible db -m file -a 'path=/tmp/f src=/etc/fst state=link' 创建软连接src是源地址,path是目标地址
    
    ansible db -m file -a 'path=/tmp/f state=absent'
    # 用来删除文件或者文件夹
    
    
    

    fetch

    dest 目的地址
    src 源地址
    
    ansible web -m fetch -a 'src=/var/log/cron dest =/tmp'
    # 下载被控节点的文件,每台机器创建一个文件夹,并保留原来的目录结构
    
  • 相关阅读:
    mysql之 共享表空间与独立表空间、frm,MYD,MYI.idb,par文件说明
    利用PS脚本自动删除7天之前建立的目录-方法1!
    走进C++程序世界-----继承和派生(2)
    Thinkpad SL400安装黑苹果10.8.4全纪录
    ASM集群文件系统ACFS(ASM Cluster File System)
    上传GIF图片方法!
    .NET通用基本权限系统
    Android项目实战--手机卫士24--程序锁的实现以及逻辑
    大数记录之,大数乘整型数nyoj832
    与IO相关的等待事件troubleshooting-系列9
  • 原文地址:https://www.cnblogs.com/zzy7372/p/10402575.html
Copyright © 2011-2022 走看看