zoukankan      html  css  js  c++  java
  • Ansible的简单使用

    ansible相关原理简单介绍:

    ansible是基于ssh服务.ssh服务一般服务器上都自带了.

    ansible 所有的工作都依赖于核心模块来完成,这些核心模块都是 ansible 自身携带的,这个内置模块功能不满足需求,还可以自己编写模块,任何语言都行,遵循基本的语法规,能执行就行。

    为了对部分主机执行配置,区分不同组的主机我们需要定义 host inventory.(主机清单),可以通过读取组名自动识别组成员。

    Ansible遵从幂等性:

     幂等性:即同一个命令无论几次执行,其结果一样.如卸载软件,第一次卸载成功了,再次运行卸载命令,则依旧报卸载成功了.

    ansible的安装:

    上传安装包到/usr/local/src

    [root@lbg src]# unzip ansible.zip 

    解压后进入目录,ansible-2.5.0-2.el6.noarch.rpm 是主要安装包,其他是依赖包。

    [root@localhost ansible]# yum -y localinstall *        ----安装。

    [root@localhost ansible]# rpm -q ansible        ---查看是否安装成功。

    ansible的简单使用:

    1.编辑host inventory.(主机清单),/etc/ansible/hosts 文件,该文件定义了当前系统能够识别的所有主机

     [root@localhost ansible]# vim /etc/ansible/hosts 

     ## green.example.com

    ## blue.example.com

    ## 192.168.100.1

    ## 192.168.100.10

    如上,我们可以直接定义主机,不分组,使用时使用选项all 来进行调用所有ip.

    ## [test]

    ## alpha.example.org

    ## beta.example.org

    ## 192.168.1.100

    ## 192.168.1.110

    如上,我们可以定义一个组叫 test,然后可以通过组名调用.

    例子:定义两个组,组别分别为test1和test2,其中test1组的ip有192.168.88.3,test2组的ip有192.168.88.4。

     [test1]
    192.168.88.3
    [test2]
    192.168.88.4

    2.配置互相并测试:(互信是你的公钥我拿着,我的公钥你拿着.ansible中若无特殊要求,配单边互相即可。)
        先测试网络是否正常:
    [root@localhost ~]# ping 192.168.88.3   
    [root@localhost ~]# ping 192.168.88.4
     
     给私钥设置空密码.(公钥是锁,私钥是密码,公钥放服务器,私钥自有.):
    [root@localhost ~]# ssh-keygen -t rsa -P ''
    Enter file in which to save the key (/root/.ssh/id_rsa):   --这一步直接回车,将私钥密码文件放在/root/.ssh/id_rsa.其中公钥文件是id_rsa.pub
    [root@localhost ~]# cd .ssh
    [root@localhost .ssh]# ls
    id_rsa  id_rsa.pub
     
     然后将公钥文件传给主机清单里的主机. 
    [root@localhost .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.88.3
    [root@localhost .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.88.4
     
    测试互信是否成功:
     [root@localhost .ssh]# ansible all -m ping
    192.168.88.4 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.88.3 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
     
    ansible使用:
     注意:使用 ansible 远程执行命令一次只能执行一个,不能使用管道符。
    -m  modularity 后跟模块(默认是命令模块)   -a  argument  后跟命令.

    例子:

     [root@localhost .ssh]# ansible all -m command -a "date +'%F%T'"

    192.168.88.3 | SUCCESS | rc=0 >>

    2018-10-0616:34:40

    192.168.88.4 | SUCCESS | rc=0 >>

    2018-10-0616:34:39

    ansible 的重要功能都是要使用模块的:

    [root@localhost .ssh]# ansible-doc -l           ---查看所有可以执行的模块

    ansible-doc s    module_name                             -----查看模块的使用方法:

    例子:

    [root@localhost .ssh]# ansible-doc -s copy     ---查看copy模块的使用方法

    [root@localhost .ssh]# ansible-doc -s file        ----查看file模块的使用方法

    copy模块使用示例:

    ~/test.sh文件放到目标主机上的/tmp目录里源文件可以是相对路径,但目标路径必须是绝对路径.

     [root@localhost ~]# ansible all -m copy -a 'src=~/test.sh dest=/tmp'      ---拷贝文件

    [root@localhost ~]# ansible all -m file -a 'path=/tmp/test.sh mode=0755'   --给文件授权

    [root@localhost ~]# ansible all -a 'chmod +x /tmp/test.sh'   ---(不依赖模块功能)给文件可执行权限

    [root@localhost ~]# ansible all -a 'ls /tmp/test.sh'             ----查看文件。

    corn模块使用示例:

     查看是否有定时任务:

    [root@localhost ~]# ansible all -a 'crontab -l '     


     创建定时任务,名字是lbg,每3分钟执行一次:

     [root@localhost ~]# ansible all -m cron -a "name='lbg' minute=*/3 job='/bin/echo 123 >> /tmp/test.sh '"   

    成功后可在目标主机上查看:

     [root@localhost .ssh]# cat /var/spool/cron/root 

    #Ansible: lbg

    */3 * * * * /bin/echo 123 >> /tmp/test.sh 

    删除定时任务:

     [root@localhost ~]# ansible all -m cron -a "name='lbg' state=absent"

    只在IP组test1里创建定时任务:

     [root@localhost ~]# ansible test1 -m cron -a " name='lbgtest' minute=*/2 job='echo how do you do >> /tmp/test.sh'"

    hostname模块使用:

     [root@localhost ~]# ansible all  -a 'hostname'           ----查看所有主机的主机名

    [root@localhost ~]# ansible all -m hostname -a 'name=lbg'   ---给所有主机命名(会在对应配置文件里也修改)

    [root@localhost ~]# ansible all  -a 'cat /etc/sysconfig/network'  ---查看配置文件里主机名是否修改。

    user模块的使用:

     [root@localhost ~]# ansible all -m user -a 'name=ansible uid=8888 group=0 ' --添加用户ansible.

    [root@localhost ~]# ansible all -a 'id ansible'   --查看用户ansible.

    [root@localhost ~]# ansible all -m user -a 'name=ansible  group=2 '   --修改用户gid.

    [root@localhost ~]# ansible all -m user -a 'name=ansible state=absent remove=yes ' --删除用户

    root@localhost ~]# ansible test1 -a 'ls -l /home'    --查看test1组的主机的ansible用户的家目录是否删除

    group模块的使用:

     [root@localhost ~]# ansible test1 -m group -a " name=dba gid=666 "   --添加组

    [root@localhost ~]# ansible test1 -m group -a " name=dba state=absent "   --删除组

    service模块的使用:

    Servicestate的状态有:started/stopped/restarted/reloaded

    [root@localhost ~]# ansible all -m service -a 'name=httpd state=started '  ---开启httpd服务

    yum模块的使用:

    先在己机上写好配置yum源的文件,再使用copy模块命令传给目标主机.

     [root@localhost ~]# ansible all -m copy -a " src=test.repo dest=/etc/yum.repos.d"  --拷贝yum源文件

    [root@localhost ~]# ansible all -a "mount /dev/sr0 /mnt/rhel"   ---挂载

    [root@localhost ~]# ansible all -a "yum repolist"   ---检查yum源

    [root@localhost ~]# ansible all -m yum -a "name=httpd state=present"  --yum安装httpd软件。

    [root@localhost ~]# ansible all -m yum -a "name=httpd state=absent" --yum卸载httpd软件

    想让 ansible 在一台主机上同时执行多条命令的时候就会用到 playbook.使用YAML语法编写playbook。

    创建 playbook:

    [root@localhost ~]# vim create-group.yaml         ----内容如下:

    ---

    - hosts: test1

      remote_user: root

      tasks:

            - name: add group dba

              group: name=dba gid=10000 system=no

            - name: add user oracle

              user: name=oracle group=dba uid=10000 system=no

            - name: touch-file

              command: /bin/touch /tmp/hello

    playbook说明:

    YAML的结构(structure)通过空格来展示,序列(sequence)里的项用"-"来代替,Map(散列)里面的键值对用":"分隔"

    文件的第一行应该以 ”—” (三个连字符,无空格)开始,表明YMAL文件的开始。

    在同一行中,#之后的内容表示注释。

    大小写敏感。

    使用缩进表示层级关系。

    缩进时不允许使用Tab键,只允许使用空格。

    缩进的空格数目不重要,只要相同层级的元素左侧对齐即可。

    YMAL中的列表元素以”-”开头然后紧跟着一个空格,后面为元素内容。

    对象的表示方法都是键值中间以:”分隔,”:”后面还要增加一个空格。

    hosts 用于指定要执行指定任务的主机,其可以是一个或者多个以冒号分隔的主机组。

    remote_user 则用于指定远程主机上的执行任务的用户。

    play 的主体部分是 tasks ,tasks的核心为ansible的模块,tasks包含name和要执行的模块,name是可选的,只是为了便于用户阅读,模块是必须的,同时也要给予模块相应的参数。

    tasks 中的各任务按次序逐个在 hosts 中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。tasks 的目的使用指定的参数执行模块,而在模块参数中可以使用变量.模块执行是幂等的,这意味着多次执行也是安全的,因为其执行结果一致。在运行 playbook ,若中途发生错误,所有已执行的任务都将回滚,因此在更正 playbook 后重新执行即可。

    在众多模块中,command  shell 模块可仅需给定一个列表而无需使用"key=value"格式。

    运行 playbook:

    [root@localhost ~]# ansible-playbook create-group.yaml          ----输出结果如下:

     PLAY [test1] ******************************************************************************

    TASK [Gathering Facts] *******************************************************************

    ok: [192.168.88.3]

    TASK [add group dba] ********************************************************************

    ok: [192.168.88.3]

    TASK [add user oracle] ********************************************************************

    ok: [192.168.88.3]

    TASK [touch-file] **************************************************************************

    changed: [192.168.88.3]

    PLAY RECAP *******************************************************************************

    192.168.88.3               : ok=4    changed=1    unreachable=0    failed=0   

  • 相关阅读:
    保持URL不变和数字验证
    centOS ftp key?
    本地环境测试二级域名
    linux 解决You don't have permission to access 问题
    php smarty section loop
    php header Cannot modify header information headers already sent by ... 解决办法
    linux部分命令
    Linux 里面的文件操作权限说明
    用IT网络和安全专业人士视角来裁剪云的定义
    SQL Server 2008 R2炫酷报表"智"作有方
  • 原文地址:https://www.cnblogs.com/lbg-database/p/10107561.html
Copyright © 2011-2022 走看看