美文网首页ansible应用合集
ansible 脚手架-1-安装与入门

ansible 脚手架-1-安装与入门

作者: DE8UG | 来源:发表于2016-12-28 16:41 被阅读83次

    安装

    系统环境:

    • CentOS release 6.8 (Final)
    • 2.6.32-642.6.2.el6.x86_64 #1 SMP Wed Oct 26 06:52:09 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

    至少准备两台机器:
    比如我准备了两台虚拟机,IP分别为:

    • 10.129.204.109(client)
    • 10.129.204.250(master)

    在master上进行ansible的安装与操作。

    安装命令:

    yum install epel-release # 为了安装更多的包
    yum list ansible # 查看有没有ansible
    yum install ansible -y # 会安装上2.2版本
    

    安装结果:

    ...省略大量过程
    Installed:
      ansible.noarch 0:2.2.0.0-3.el6                                                                                                                                                             
    
    Dependency Installed:
      PyYAML.x86_64 0:3.10-3.1.el6                    libyaml.x86_64 0:0.1.3-4.el6_6                python-babel.noarch 0:0.9.4-5.1.el6         python-crypto.x86_64 0:2.0.1-22.el6           
      python-crypto2.6.x86_64 0:2.6.1-2.el6           python-httplib2.noarch 0:0.7.7-1.el6          python-jinja2-26.noarch 0:2.6-3.el6         python-keyczar.noarch 0:0.71c-1.el6           
      python-markupsafe.x86_64 0:0.9.2-4.el6          python-paramiko.noarch 0:1.7.5-2.1.el6        python-pyasn1.noarch 0:0.0.12a-1.el6        python-setuptools.noarch 0:0.6.10-3.el6       
      python-simplejson.x86_64 0:2.0.9-3.1.el6        python-six.noarch 0:1.9.0-2.el6               sshpass.x86_64 0:1.05-1.el6                
    
    Complete!
    

    小试牛刀

    第一步添加主机

    vim /etc/ansible/hosts

    在最后添加我们的目标主机,默认文件里都是一些例子,而且都被注释了,可以用到时慢慢看。

    添加内容如下:

    [test]
    10.129.204.250
    

    其中,中括号中的为模块名,用来表示一组机器的名字,ansible针对这个名字,就可以找到相应的一组机器了。下面的就是ip或域名,可以添加多个。

    第二步执行命令

    ansible基本命令很简单,直接ansible后面加-a和命令的字符串去运行就可以了。 -a在这里是args的意思,就是表示参数。因为默认的ansible会执行命令模块。执行那个模块用-m command,这里就先都省略了。试试效果:

    [root@master ~/ansible-note]# ansible test -a "ls -l /tmp"
    10.129.204.250 | SUCCESS | rc=0 >>
    total 8
    drwx------. 2 root root 4096 Dec 15 15:42 ansible_oqjZkz
    drwxr-xr-x. 2 root root 4096 Nov 16 15:23 hsperfdata_root
    

    我们查看了一下远程机器的tmp目录,可以看见里面有ansible开头的一个目录,这是因为ansible执行命令会在远程机器建立自己的工作临时目录。再试一遍,你会发现这个名字就变了。

    [root@master ~/ansible-note]# ansible test -a "ls -l /tmp"
    10.129.204.250 | SUCCESS | rc=0 >>
    total 8
    drwx------. 2 root root 4096 Dec 15 15:44 ansible_lyCt2p
    drwxr-xr-x. 2 root root 4096 Nov 16 15:23 hsperfdata_root
    
    

    学会查看帮助

    帮助文档是带领我们快速前进的最佳方案,ansible中的帮助做得相当到位,常用查看命令如下

    [root@master ~/ansible-note]# ansible-doc -h # 总帮助,其中的 -l 和 -s 最为常用
    Usage: ansible-doc [options] [module...]
    
    Options:
      -h, --help            show this help message and exit
      -l, --list            List available modules
      -M MODULE_PATH, --module-path=MODULE_PATH
                            specify path(s) to module library (default=None)
      -s, --snippet         Show playbook snippet for specified module(s)
      -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                            connection debugging)
      --version             show program's version number and exit
    
    [root@master ~/ansible-note]# ansible-doc -l # 查看所有模块,空格翻页,q退出
    
    [root@master ~/ansible-note]# ansible-doc -s service # -s 后面接模块名,用来查看此模块需要的一些参数,这里以service为例
    - name: Manage services.
      action: service
          arguments              # Additional arguments provided on the command line
          enabled                # Whether the service should start on boot. *At least one of state and enabled are required.*
          name=                  # Name of the service.
          pattern                # If the service does not respond to the status command, name a substring to look for as would be found in the output of the `ps' command as a stand-in for a
                                   status result.  If the string is found, the service will be assumed to be running.
          runlevel               # For OpenRC init scripts (ex: Gentoo) only.  The runlevel that this service belongs to.
          sleep                  # If the service is being `restarted' then sleep this many seconds between the stop and start command. This helps to workaround badly behaving init scripts that
                                   exit immediately after signaling a process to stop.
          state                  # `started'/`stopped' are idempotent actions that will not run commands unless necessary.  `restarted' will always bounce the service.  `reloaded' will always
                                   reload. *At least one of state and enabled are required.*
    
    

    错误处理

    学习过程中,看官方文档或许是最快的路径。但是,如果看文档都失败呢?

    比如ansible-doc -l可以查看所有模块的帮助,但是第一次运行竟然又是警告又是错误:

    [root@master ~/ansible-note]## rm -f /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
    [root@master ~/ansible-note]# ansible-doc -l
    
    [DEPRECATION WARNING]: docker is kept for backwards compatibility but usage is discouraged. The module documentation details page may explain more about this rationale..
    This feature will 
    be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
     [ERROR]: unable to parse /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
    
    ERROR! module rhevm has a documentation error formatting or is missing documentation
    
    

    这里的问题可能是系统自带的python版本太低导致的,解决办法也很简单,修改警报文件,删除报错文件。

    sed -i 's/^#deprecation_warnings = True/deprecation_warnings = False/' /etc/ansible/ansible.cfg
    rm -f /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
    

    然后,再次运行ansible-doc -l,你会看见大批的模块列表,回车查看下一页,q退出。

    相关文章

      网友评论

        本文标题:ansible 脚手架-1-安装与入门

        本文链接:https://www.haomeiwen.com/subject/bdwyvttx.html