zoukankan      html  css  js  c++  java
  • Linux——ansible(1)

    ansible

    epel源

    第一步: 下载epel源

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

    第二步: 安装ansible

    yum install -y ansible
    

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

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

    ansible 通过ssh来连接并控制被控节点

    ssh 的认证方式

    • 密码连接
    • 秘钥连接

    ssh 秘钥登录

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

    ansible 命令格式

    ansible <host-pattern> [options]
    -a MODULE_ARGS, --args=MODULE_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的帮助信息
    

    系统自带的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不存在,所有才会执行pwd命令
    ansible web -a 'removes=/tmp pwd' #用来判断tmp目录是否存在,存在就执行操作
    ansible web -a 'removes=/data pwd' #因为data不存在,所有才不会执行
    

    查看用户是否创建成功

    tail -1 /etc/passwd
    tail -1 /etc/shadow
    id alex
    echo '123' | passwd --stdin alex #设置密码
    

    shell

    ansible web -m shell -a 'echo "123" | passwd --stdin alex' # 批量创建密码
    ansible 192.168.107.131 -m shell -a 'bash a.sh' # 执行远程文件方式一
    ansible 192.168.107.131 -m shell -a '/root/a.sh' #执行远程文件方式二,文件必须有执行权限
    ansible 192.168.107.131 -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 目的地址
    group 文件的属组
    mode 文件的权限 r 4 w 2 x 1
    owner 文件的属主
    src 源文件
    # 通过md5码来判断是否需要复制
    ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' #复制本地文件的到远程主机
    ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh 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/ mode=755 owner=alex' # 复制本地目录内的所有文件到远程主机
    ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语
    ' dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中
    

    file

    补充

    inode 硬盘的地址
    id 获取到的是内存的地址
    ln -s a.py b.py 创建软连接
    ln  a.py c.py 创建硬链接
    当 源文件变化时,软连接和硬链接文件都会跟着变化
    
    ansible db -m file -a 'path=/lzmly2  state=directory' #在远程机器上创建文件夹
    ansible db -m file -a 'path=/root/q.txt  state=touch' #用来在远程机器上创建文件
    ansible db -m file -a 'path=/tmp/f src=/etc/fstab 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' # 下载被控节点的文件,每台机器创建一个文件夹,并保留原来的目录结构
    

  • 相关阅读:
    Sprinig.net 双向绑定 Bidirectional data binding and data model management 和 UpdatePanel
    Memcached是什么
    Spring.net 网络示例 codeproject
    jquery.modalbox.show 插件
    UVA 639 Don't Get Rooked
    UVA 539 The Settlers of Catan
    UVA 301 Transportation
    UVA 331 Mapping the Swaps
    UVA 216 Getting in Line
    UVA 10344 23 out of 5
  • 原文地址:https://www.cnblogs.com/jiumo/p/10401271.html
Copyright © 2011-2022 走看看