• ansible详解-常用模块


    Ansible介绍

    ansible是一款轻量级的自动化管理工具,相对于puppet,saltstack来说它更加的轻量化,用python编写。支持多种指令操作,同时也支持playbook。通过ssh进行通信,客户端无需安装客户端即可进行批量管理,ansible对远程主机的操作具有幂等性,所以可以重复执行而不用担心有问题。

    ansible组成

    • ansible:主要的服务,用于调用其他各种组件;
    • inventoy:用于存储要控制的主机,包括主机用户名密码等信息;
    • playbooks:用于制定各种playbook;
    • core modules:ansible的核心模块,ansible依赖各种模块进行服务器控制;
    • custome modules:自定义模块;
    • connect plugins:调用python的并发连接器进行服务器连接;
    • other plugins:其他插件,例如日志记录,邮件等;

    ansible inventoy配置文件组成

    # This is the default ansible 'hosts' file.
    #
    # It should live in /etc/ansible/hosts
    #
    #   - Comments begin with the '#' character
    #   - Blank lines are ignored
    #   - Groups of hosts are delimited by [header] elements
    #   - You can enter hostnames or ip addresses
    #   - A hostname/ip can be a member of multiple groups
    
    # Ex 1: Ungrouped hosts, specify before any group headers.
    
    ## green.example.com
    ## blue.example.com
    ## 192.168.100.1
    ## 192.168.100.10
    
    # Ex 2: A collection of hosts belonging to the 'webservers' group
    
    ## [webservers]
    ## alpha.example.org
    ## beta.example.org
    ## 192.168.1.100
    ## 192.168.1.110
    
    # If you have multiple hosts following a pattern you can specify
    # them like this:
    
    ## www[001:006].example.com    #也可以使用此种方法来表示一个地址段,此处表示从001到006
    
    # Ex 3: A collection of database servers in the 'dbservers' group
    
    ## [dbservers]
    ## 
    ## db01.intranet.mydomain.net
    ## db02.intranet.mydomain.net
    ## 10.25.1.56
    ## 10.25.1.57
    
    # Here's another example of host ranges, this time there are no
    # leading 0s:
    
    ## db-[99:101]-node.example.com
    
    [test]
    192.168.189.129
    

    ansible控制方式

    • 免密钥控制
    • 通过inventoy记录主机ip地址(主机名)、用户名密码进行控制
    #vim /etc/ansible/hosts
    [webservers]
    192.168.1.[31:32] ansible_ssh_user='root' ansible_ssh_pass='redhat'
    

    ansible命令执行方式

    • 通过调用各种模块进行命令执行
    • 通过编写playbook进行服务器各种管理工作
      ansible (all|主机组名|主机名|ip地址) -m 模块名 -a '要调用的模块选项'

    ansible配置文件

    • /etc/ansible/ansible.cfg:ansible配置文件
    • /etc/ansible/hosts:inventoy配置文件

    ansible获取帮助信息

    ansible-doc -s 模块名

    ansible常用模块

    command:用于执行命令,但是不能应用变量,管道等
    ansible test -m command -a 'date'

    shell:类似command,可以用变量,管道等
    ansible test -m shell -a 'echo 1234567a |passwd test --stdin'

    user常用选项:

    • name:用户名
    • password:密码
    • state:present为添加,absent为删除
    • remove:删除用户,yes|no
    • system:是否为系统用户
    • createhome:是否创建家目录
    • shell:指定用户shell
    • group:设置基本组
    • groups:设置夫家族
    • uid:指定uid
      ansible test -m user -a 'name=hello password=123456 state=present system=yes createhome=no'

    group常用选项:

    • name:组名
    • gid:gid
    • state:present为创建,absent为删除
    • system:是否为系统组
      ansible all -m group -a 'name=hello system=yes state=present'

    cron常用选项:

    • day:天,1-9,,/3
    • hour:小时
    • month:月
    • name:cron任务名
    • state:present,absent
    • weekday:周
    • minute:分钟
    • job:工作内容
      ansible all -m cron -a 'minute=*/1 name="echo hello world" job="echo hello world" state=present'

    copy常用选项:

    • src:源文件绝对路径或相对路径
    • dest:目标地址,必须是绝对路径
    • content:可以代替src,要写入到dest文件中的内容,会echo追加进去
    • owner:属主
    • group:属组
    • backup:覆盖文件之前是否备份,yes|no
    • directory_mode:递归设定目录权限
    • force:yes,如果文件存在,但是内容不同,则强行覆盖,默认选项;no,如果文件不存在才复制
    ansible test -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=test group=hello'
    ansible test -m copy -a 'content="hello world" dest=/tmp/log.ansible'    #把hello world写入目标文件
    

    file常用模块,对被控主机文件进行操作:

    • owner:属主
    • group:属组
    • mode:权限
    • recurse:递归设置,对目录有限
    • state
      • touch:创建一个空白文件
      • directory:创建一个新目录,目录存在则不会修改
      • link:创建软链接
      • hard:创建硬链接
      • absent:删除
    • src:当state=link的时候,要被链接的源文件
    ansible test -m file -a 'path="/tmp/log.ansible" owner=root group=wheel mode=640'
    ansible test -m file -a 'src="/etc/fstab" path="/tmp/fstab.link" state=link'
    

    script,再被控主机执行ansible控制端的脚本:
    ansible test -m script -a '/root/test.sh'

    yum模块常用选项:

    • name:要安装的软件名
    • state
      • present:安装
      • absent:卸载
      • lastest:安装为最新版本

    setup用于收集指定远程主机的facts信息,这些facts信息可以作为变量被调用:
    ansible test -m setup

    service常用模块,用于控制服务:

    • enabled:该服务是否开机自启
    • name:服务名
    • state
      • started:启动
      • stopped:停止
      • restarted:重启
    • sleep:启动和停止中间sleep时间
    • runlevel:在哪些级别可以自启动
    • arguments:向命令行传递参数
      ansible test -m service -a 'name=httpd enabled=yes state=started'

    ping,用于测试远程主机是否在线,回复pong表示在线
    ansible test -m ping

  • 相关阅读:
    中文转数字
    半角全角互转
    sql快速查记录数
    杀进程批处理
    线程基本用法
    sql游标用法示例
    BUGFREE的使用
    SQL常用函数
    ASP.NET 2.0 下的验证码控件
    经典sql语句
  • 原文地址:https://www.cnblogs.com/stacks/p/8542126.html
Copyright © 2020-2023  润新知