zoukankan      html  css  js  c++  java
  • Ansible学习笔记

    1.安装Ansible软件

    yum install -y ansible
     
    2./etc/ansible/hosts
    该文件代表需要管理的主机列表
    格式为:
    #组名
    [test] 
    #IP地址或主机名或域名
    192.168.1.151
    192.168.1.152
    test1
    test2
    OS_controller
    OS_compute
     
    连续IP可以使用192.168.1.[151:160]来表示。
    一个IP地址可以属于多个组,例如
    [test]
    192.168.1.[151:152]
    [test1]
    192.168.1.151
    [test2]
    192.168.1.152
     
    3.执行命令
    #ansible -i /etc/ansible/hosts test -u root -m command -a 'ls /home' -k
    其中-i默认为/etc/ansible/hosts,默认位置可以省略。
    -u默认为root,默认可省略
    test为hosts中的组名,必不可少
    -m默认为command,默认可省略
    -k为输入密码,SSH无密码户型后可省略
     
    省略后的命令为:
    #ansible test -a 'ls /home'
     

    4.ping模块

    #ansible all -m ping 
    其中all表示hosts中的全部主机,会自动除重。
    -m ping表示使用ping模块
    返回值:
    (151和152能ping通,153不通)
    --------------------------------------------------------------------------------------
    [root@localhost ansible]# ansible test -m ping
    192.168.1.152 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.1.151 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.1.153 | UNREACHABLE! => {
        "changed": false, 
        "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.1.153 port 22: No route to host ", 
        "unreachable": true
    }
    --------------------------------------------------------------------------------------
     
    5.使用远程普通用户
    #ansible all -u hanxinda -m ping --sudo
    使用远程主机的hanxinda用户执行ping
    需要指定使用sudo
    并且保证hanxinda用户与ansible本地主机SSH无密码户型,否则使用-k输入密码
     
     

    6.处理用户名不同,端口不同

    在/etc/ansible/hosts文件中指定用户名和端口
    [test]
    192.168.1.151 ansible_ssh_user=root
    192.168.1.152 ansible_ssh_user=hanxinda ansible_ssh_port=2202
     
    ansible_ssh_user   #指定ssh该主机的用户名
    ansible_ssh_port   #指定ssh该主机的端口
    host_key_checking=false  #在首次ssh一个主机时,可能会让你输入yes/no,该选项可以跳过这个过程。
     

    7.主机组之间的嵌套

    /etc/ansible/hosts文件中
    [openstack:children]
    controller    #组名
    compute
    [controller]
    192.168.1.140
    192.168.1.141
    [compute]
    192.168.1.151
    192.168.1.152
    192.168.1.[160:170]
     
    测试全部openstack节点是否存活
    #ansible openstack -m ping
    测试controller节点是否存活
    #ansible controller -m ping
    测试compute节点是否存活
    #ansible compute -m ping
     

    8.查看Ansible模块

    查看所有模块
    #ansible-doc -l
    查看某个module的参数
    #ansible-doc -s module
    例如ansible-doc -s ping,ansible-doc -s user
     
     

    9.setup模块

     
    用于收集远程主机的一些基本信息
    #ansible test -m setup
     
     

    10.file模块

     
    用于设置文件属性,创建删除等:
    file模块包含以下选项:
        force:在两种情况下强制创建软连接,一种是文件不存在,但之后会创建的情况。另一种是目标软连接已存在,需要先取消之前的软连接,然后创建新的软连接。yes/no
        group:定义文件或目录的属组
        mode:定义文件或目录的权限
        owner:定义文件或目录的属主
        path:定义文件或目录的路径(必不可少)
        recurse:递归的设置文件的属性,只对目录有效
        src:要被连接的源文件路径,只应用于state=link的情况
        dest:被链接到的路径,只应用于state=link的情况
        state:
                directory:如果目录不存在,则创建目录
                file:即使文件不存在,也不创建文件
                link:创建软链接
                hard:创建硬链接
                touch:如果文件不存在,则会创建一个新的文件,如果文件已存在,则更新最后修改时间
                absent:删除目录、文件或取消链接文件
     
    eg .
    删除一个文件aaa.txt
    #ansible test -m file -a "path=/tmp/aaa.txt state=absent"
    创建一个文件bbb.txt
    #ansible test -m file -a "path=/tmp/bbb.txt state=touch"
    创建一个文件的软连接,force=yes表示/tmp/aaa不存在,也仍然创建软连接。
    #ansible test -m file -a "src=/tmp/aaa dest=/tmp/aaa.ln state=link force=yes"
     
     

    11.copy模块

    用于拷贝本地文件到远程主机:
    copy有以下选项:
        src:本地文件路径
        dest:远程主机路径
        backup:若文件同名,是否备份原文件,如果已存在文件的内容与当前拷贝文件一致,则不会备份(checknum一致?)
     
    eg.
    将ansible本地文件/tmp/aaa远程拷贝到目的主机的/tmp下
    #ansible test -m copy -a "src=/tmp/aaa dest=/tmp/ backup=yes"
     

    12.command模块

    用于在远程主机执行一段shell命令:
    command有以下常用选项:
        creates:creates=文件名,当该文件存在时,执行后面的命令。
        chdir:切换工作目录,chdir=/tmp/
        removes:removes=文件名,当该文件不存在时,执行后面的命令。与creates相反。
     
    eg.
    若/tmp/aaa不存在时,执行ls /home
    #ansible test -m command -a 'creates=/tmp/aaa ls /home'
    -----------------------------------------------------------------------------------------
    [root@localhost ansible]# ansible test -m command -a 'creates=/tmp/aaa ls /home'
    192.168.1.152 | SUCCESS | rc=0 >>
    skipped, since /tmp/aaa exists
    192.168.1.151 | SUCCESS | rc=0 >>
    skipped, since /tmp/aaa exists
    -----------------------------------------------------------------------------------------
     
    切换工作目录到/tmp/下,然后执行命令
    #ansible test -m command -a 'chdir=/tmp/ tar zcf aaa.tar.gz aaa'
     
     
    13.shell模块
    功能和command模块相似,参数也相似,不同的是shell支持管道:
    下面命令只能用shell模块执行,command不支持管道
    #ansible test -m shell -a 'chdir=/tmp ls >>~/log.txt'
    #ansible test -m shell -a 'chdir=/usr ls | grep game'
     
    还有个类似的模块叫raw:(测试时报错?)
    #ansible test -m raw -a 'chdir=/usr ls -lh | grep game'
    ----------------------------------------------------------------------------------------
    [root@localhost ansible]# ansible test -m raw -a 'chdir=/usr ls -lh | grep game'
    192.168.1.151 | FAILED | rc=1 >>
    Shared connection to 192.168.1.151 closed.
    192.168.1.152 | FAILED | rc=1 >>
    Shared connection to 192.168.1.152 closed.
    ----------------------------------------------------------------------------------------
     

    14.service模块

    用于远程管理服务,例如enable,start等:
    service模块有以下常用选项:
        enabled:设置开机启动服务
        name:name=服务名
        state:有以下几种值
                started:启动服务
                stopped:关闭服务
                restarted:重启服务
                reloaded:
        sleep:当使用state=restarted时。使用sleep设置结束服务到启动服务之间的时间间隔,例如sleep=3,即restarted中间停顿3秒
        pattern:指定服务的路径,例如/usr/bin/foo
        arguments:参数
        
    eg.
    启动nginx服务
    #ansible test -m service -a 'name=nginx state=started'
    关闭nginx服务
    #ansible test -m service -a 'name=nginx state=stopped'
    重启nginx服务,停止与启动之间休息3秒
    #ansible test -m service -a 'name=nginx state=restarted sleep=3'
     
    14.cron模块
    用于管理任务计划
    cron模块拥有以下选项:
        backup:是否对远程主机上的原计划任务做备份
        cron_file:如果指定该选项,则用该文件替换远程主机上cron.d目录下的计划任务
        day:日
        hour:小时,hour=4每天4点,hour="5,2"每天5点和2点,*/2每两小时
        minute:分钟
        month:月
        weekday:周
        job:要执行的任务, 依赖于state=present
        name:该任务的描述,自定义
        special_time:指定什么时候执行,参数reboot,yearly,annually,weekly,daily,hourly
        state:确认该任务计划是创建还是删除
        user:以哪个用户的身份执行
     
    eg.
    每天两天重启系统
    #ansible test -m cron -a 'name="this is a reboot task" hour=2 user=root job="/sbin/reboot"'
    删除该重启计划
    #ansible test -m cron -a 'name="this is a reboot task" hour=2 user=root job="/sbin/reboot" state=absent'
    每隔三分钟查看一下目录
    #ansible test -m cron -a 'name="check home dir" minute=*/3 user=root job="ls /home -l >>~/log.txt"'
     
    15.yum模块
    用于使用yum工具安装管理包
     
     yum模块拥有以下选项:
        state:对软件包的操作,包含installed或present,latest。absent或removed。
        name:软件名
        
    eg.
    安装apache,httpd
    #ansible test -m yum -a 'name=httpd state=installed'
     
     
     
     
     
     
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/leokale-zz/p/8627869.html
Copyright © 2011-2022 走看看