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

                                            ansible运维自动化工具                                         

    软件包的安装
    rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
    yum install ansible -y
    配置免密钥
    在master服务器生成ssh-key,并分发到所有客户端
    ssh-keygen -t rsa  【一路回车】
    ssh-copy-id -i ~/.ssh/id_rsa.pub【客户端IP地址】

    配置安装完成之后,先来配置下配置项——.ansible.cfg。ansible执行的时候会按照以下顺序查找配置项:

    * ANSIBLE_CONFIG (环境变量)

    * ansible.cfg (当前目录下)

    * .ansible.cfg (用户家目录下)

    * /etc/ansible/ansible.cfg

    默认ansible执行时会从该配置中加载hosts配置,因此可以通过修改.ansible.cfg来指定默认的hosts文件地址:

    [defaults]
    hostfile=/Users/the5fire/hosts


    有几个参数需要记录下:
    -u username # 指定ssh连接的用户名
    -f 10 # 指定并发数
    --sudo [-K] # 如果需要root权限执行的话,-K参数是用来输入root密码的

    定义hosts文件:

    ansible的hosts默认在/etc/ansible/目录中,其中提示ansible是支持域名和ip两种客户端命名格式的,在这里定义了一个“slave”组 vim /etc/ansbile/hosts [slave]

    192.168.17.18
    192.168.17.19
    vim /etc/ansible/hosts  
    方法一: [webhosts]
    172.16.10.22 ansible_ssh_user=root ansible_ssh_pass=guoting 172.16.10.33 ansible_ssh_user=root ansible_ssh_pass=guoting
    方法二:
    [GameServer]
    Android1-2 ansible_ssh_host=10.10.113.31 ansible_ssh_user=joy ansible_ssh_port=888
    Android3-4 ansible_ssh_host=10.10.243.59 ansible_ssh_user=joy ansible_ssh_port=888
    IOS1-2 ansible_ssh_host=10.10.240.248 ansible_ssh_user=joy ansible_ssh_port=888
    Tencent1-2 ansible_ssh_host=10.10.243.67 ansible_ssh_user=joy ansible_ssh_port=888 解释 ansible_ssh_user
    =root 是ssh登陆用户 ansible_ssh_pass=guoting 是ssh登陆密码3、测试各个模块
    # 注意每个模块的用法可以使用 ansible
    -doc MOD 来查看例如ansible-doc copy ansible命令最常用的用法 ansible <Host-partten> -m MOE -a 'MOD_ARV'所支持的模块可以使用ansible-doc -l来查看
    
    
    
    
    
    

    上面的ad hoc是指执行一条临时的不需要保存的命令,那么复杂的命令怎么执行呢?因此也就有了playbook这个命令: ansible-playbook 。

    playbook(剧本),就是需要定义一个脚本或者说配置文件,然后定义好做什么。

    ymal中的变量:

    在 yaml 中可以使用vars关键字来定义变量  

    vars:  

    var_name: value

    例子:

    变量的引用  

    {{ var_name }}  

    例子:

    添加 2 个用户  
       
    方式1一般做法  
    - name: add user testuser1  
    user: name=testuser1 state=present groups=wheel 
    - name: add user testuser2  
    user: name=testuser2 state=present groups=wheel 
       
    方式2使用变量方式  
    - name: add several users  
    vars:  
    user1: testuser1  
    user2: testuser2  
    user: name={{ user1 }} state=present groups=wheel 
    user: name={{ user2 }} state=present groups=wheel 
       
    方式3使用迭代方式  
    - name: add several users  
    user: name={{ item }} state=present groups=wheel 
    with_items:   
    - testuser1   
    - testuser2  
    事实上with_items中可以使用元素还可为hashes例如  
    - name: add several users  
    user: name={{ item.name }} state=present groups={{ item.groups }}  
    with_items:  
    - { name: 'testuser1', groups: 'wheel' }  
    - { name: 'testuser2', groups: 'root' } 

    例子:把当前用户名输出到whoami.rst文件中:

    cat /etc/ansible/playbook.yml
    ---
    - hosts: slave  # hosts文件中定义的组
      remote_user: root  # 如果和当前用户一样,则无需指定
      tasks:  # tasks是是关键词,指明了要执行哪些任务
        - name: whoami # name是任务的名称
          shell: 'whoami > whoami.rst' # shell是指定使用的module(模块),单引号中是命令。
    
    - hosts: slave
      remote_user: root
      tasks:
          - name: whoami
            copy: src=~/hosts dest=~/hosts.dest  #  本地拷贝到远端
            notify: # 如果copy执行完之后~/hosts.dest文件发送了变化,则执行
                - clear copy 
      handlers:
          - name: clear copy  # 调用handler
            shell: 'mv ~/hosts.dest hosts.del'  # 重命名

     或

    cat playbook.yml

    - hosts: slave
      remote_user: root
      tasks:
          - name: whoami
            copy: src=~/hosts dest=~/hosts.dest
    
          - name: clear copy
            shell: 'mv ~/hosts.dest hosts.del'
  • 相关阅读:
    【转】汽车CAN总线
    【转】I2C总线协议
    【转】SPI总线协议
    【转】结构struct 联合Union和枚举Enum的细节讨论
    复合类型变量其首地址的几种表示方式
    【转】四款经典3.7v锂电池充电电路图详解
    【转】crc16几种标准校验算法及c语言代码
    【转】 CRC循环冗余校验码
    对STM32库函数中 assert 函数的认知
    【转】用宏定义代替printf函数
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/5035406.html
Copyright © 2011-2022 走看看