zoukankan      html  css  js  c++  java
  • ansible的安装和简单使用

      ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
     
    ansible的安装:
      首先要安装epel源:
    yum install -y wget # 安装wget
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 下载epel源文件
    

      安装ansible:

    yum install ansible -y
    ansible的一些基本命令:
      1.首先看一下ansible的部分命令格式 :
        ansible -h
        -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       #指定密码
    

      2.查看ansible生成的配置文件

        rpm  -ql  ansible

    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles
    

      3.ssh认证:

        方式一 : 密码

        方式二 : 密钥

        这里主要介绍密钥认证方式 :

          生成密钥 : ssh-keygen   (这里连续回车就可以)

          将密钥文件复制到远程主机 : ssh-copy-id root@ip

        此时进行远程登陆时就不需要再输密码.

      4.验证远程主机的连接 :

        ansible   ip地址  -m ping         #验证是否连接远程主机

    192.168.169.131 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }    #成功

        ansible all -m ping      #验证hosts文件中绑定的所有机器是否已经连接

    192.168.169.131 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.169.134 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.169.132 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    

        ansible ip1,ip2 -m ping    #查看部分机器

    192.168.169.131 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.169.132 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    

      5.在ansible生成的hosts配置文件中对ip进行分组,如 : 

    [web]
    10.0.0.132
    10.0.0.133
    

        可以对分组内的ip进行的操作如下 :

    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的并集

      6.获取ansible中模块的信息 :

        ansible-doc -h

    -j, --json #以json的方式返回所有模块的信息
    -l # 列出所有的模块
    -s, --snippet # 以片段式显示模块的帮助信息,去掉-s会显示全部信息
    ansible-doc -l |wc -l #统计ansible的模块
    ansible中的部分模块
      1.command模块,执行远程主机上的命令,不支持特殊字符,<> | $;&.
        ansible-doc -s command
        argv:            # Allows the user to provide the command as a list vs. a string.  Only the string or the list form can be provided, not both.  One or the other must be provided.
          chdir:           # Change into this directory before running the command.      #切换目录
          creates:         # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be  run.   # 如果存在,就不执行,如果不存在,就执行
          free_form:       # (required) The command module takes a free form command to run.  There is no parameter actually named 'free form'. See the examples!
          removes:         # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.     # 如果不存在,就不执行,如果存在,就执行
          stdin:           # Set the stdin of the command directly to the specified value.
          warn:            # If command_warnings are on in ansible.cfg, do not warn about this particular line if set to `no'.
    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已经存在
    

      2.shell模块,执行远程主机上的命令或者脚本.

    ansible web -m shell -a "echo '密码'|passwd --stdin 用户名" # 给用户设置密码
    ansible ip地址 -m shell -a "bash a.sh" # 执行shell脚本
    ansible ip地址 -m shell -a "./a.sh"
    ansible ip地址 -m shell -a "/root/a.sh"
    ansible ip地址 -m shell -a "/root/a.py" # 执行python脚本
    ansible ip地址 -m shell -a "python a.py" 
    

      3.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" # 判断的主机是否存在,如果存在,就执行,如果不存在,就不执行
    

      4.copy模块,将本地的文件复制远程主机上.

    backup  # 创建备份文件,以时间戳结尾
    content # 直接写内容
    dest    # 目标地址
    group   #文件的属组
    mode    #文件的权限 W2 R4  X1
    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 也不会去备份文件
    

      5.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" # 创建一个目录
    ansible db -m file -a "path=/tmp/baoyuan state=directory owner=alex mode=644" # 创建一个目录,并制定属主,权限
    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 原文件 目标文件 原文件改变,目标文件也改变 不可以跨域分区 原文件删除,不会受影响

    查看用户是否创建成功

      1.ll /home
      2.tail -1 /etc/passwd
      3.tail /etc/shadow

  • 相关阅读:
    46. Permutations 全排列,无重复
    243. Shortest Word Distance 最短的单词index之差
    171. Excel Sheet Column Number Excel列号转数字
    179. Largest Number 用数组中的元素凑一个最大数字
    49. Group Anagrams 多组anagram合并
    电话号码的字母组合(leetcode17)
    最接近的三数之和(leetcode16)
    c#之dynamic类型通过属性获取值(get value by key)
    三数之和(leetcode15)
    java-list与array转换
  • 原文地址:https://www.cnblogs.com/wangtaobiu/p/10678166.html
Copyright © 2011-2022 走看看