• ansible清单文件的配置方法、配置文件的配置、临时命令的用法


    ansible清单文件的配置方法、配置文件的配置、临时命令的用法

    一、构建ansible清单

    1、定义清单

    清单定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。

    定义清单的两种方式:

    • 静态主机清单:文本文件定义
    • 动态主机清单:根据需要使用外部信息提供程序通过脚本或其他程序来生成

    2、使用静态清单指定受管主机

    每一部分的开头为以中括号括起来的主机组名称。其后为该组中每一受管主机的主机名或IP地址,每行一个。

    [root@localhost ~]# vim /etc/ansible/hosts
    ## [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
    
    # 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3、验证清单

    [root@localhost ~]# vim /etc/ansible/hosts
    green.example.com
    blue.example.com
    192.168.100.1
    192.168.100.10		//取消此四行的注释
    
    //验证green主机是否存在于清单
    [root@localhost ~]# ansible green.example.com --list-hosts
      hosts (1):
        green.example.com
        
    //列出清单中的所有主机
    [root@localhost ~]# ansible all --list-hosts
      hosts (4):
        green.example.com
        blue.example.com
        192.168.100.1
        192.168.100.10
    
    [root@localhost ~]# vim /etc/ansible/hosts
    [webservers]
    alpha.example.org
    beta.example.org
    192.168.1.100
    192.168.1.110		//取消此主机组的注释
    
    //列出指定主机组
    [root@localhost ~]# ansible webservers --list-hosts
      hosts (4):
        alpha.example.org
        beta.example.org
        192.168.1.100
        192.168.1.110
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。

    [root@localhost ~]# vim /etc/ansible/hosts
    [webservers]
    webservers		//添加一个和主机组相同名称的主机
    alpha.example.org
    beta.example.org
    192.168.1.100
    192.168.1.110
    
    
    //会列出主机并警告
    [root@localhost ~]# ansible webservers --list-hosts
    [WARNING]: Found both group and host with same name: webservers
      hosts (1):
        webservers
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4、覆盖清单文件的位置

    /etc/ansible/hosts文件被视为系统的默认静态清单文件。不过,通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。

    //修改默认清单文件位置
    [root@localhost ~]# cd /etc/ansible/
    [root@localhost ansible]# touch inventory
    [root@localhost ansible]# vim ansible.cfg 
    inventory      = /etc/ansible/inventory		//取消注释并修改指定位置
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5、构建清单

    //写入内容
    [root@localhost ansible]# vim inventory 
    192.168.183.137
    
    [webservers]
    192.168.183.135
    
    192.168.183.136
    
    //列出默认清单文件中的所有受管主机
    [root@localhost ansible]# ansible all  --list-hosts
      hosts (3):
        192.168.183.137
        192.168.183.135
        192.168.183.136
        
    //列出不属于任何主机组的受管主机
    [root@localhost ansible]# ansible ungrouped --list-hosts
      hosts (1):
        192.168.183.137
        
    //列出属于某组的受管主机
    [root@localhost ansible]# ansible webservers --list-hosts
      hosts (2):
        192.168.183.135
        192.168.183.136
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    二、ansible配置文件

    Ansible配置文件中的一些常用参数

    [root@localhost ~]# vim /etc/ansible/ansible.cfg
    ······
    [defaults]
    
    # some basic default values...
    
    inventory      = /etc/ansible/inventory
    #library        = /usr/share/my_modules/
    #module_utils   = /usr/share/my_module_utils/
    #remote_tmp     = ~/.ansible/tmp
    #local_tmp      = ~/.ansible/tmp
    #plugin_filters_cfg = /etc/ansible/plugin_filters.yml
    #forks          = 5
    #poll_interval  = 15
    #sudo_user      = root
    #ask_sudo_pass = True
    #ask_pass      = True
    #transport      = smart
    #remote_port    = 22
    #module_lang    = C
    #module_set_locale = False
    
    ······
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    参数作用
    inventory定义了Ansible默认的主机配置文件,默认为/etc/ansible/hosts
    library定义了Ansible默认搜寻模块的位置,默认为/etc/ansible/my_modules/目录
    remote_tmp定义了Ansible远程执行临时文件
    pattern定义了Ansible通信的主机,该参数默认为*,表示与所有主机进行通信
    forks定义了Ansible的并行进程数,默认为5
    poll_interval定义了回频率或轮询间隔时间
    sudo_user定义了sudo远程执行用户名
    ask_sudo_pass定义了使用sudo是否需要输入密码
    ask_pass定义了是否需要输入密码
    transport定义了Ansible的通信机制
    remote_port定义了Ansible的通信端口,默认为22
    module_lang定义了Ansible模块和系统之间通信的语言
    gathering控制facts信息的收集
    roles_path用于搜索Ansible中的roles
    host_key_checking用于检查主机密钥
    sudo_exe用于指定sudo远程执行命令
    sudo_flags用于传递sudo以外的参数
    timeout用于设置SSH超时时间
    remote_user用于设置远程登录用户名
    log_path用于指定Ansible日志文件,默认情况下为/var/log/ansible.log
    module_name用于指定Ansible在默认情况下的执行模块,默认为command
    executable用于指定Ansible执行的shell环境
    hash_behavior用于指定特定的优先级覆盖变量
    jinjia2_extensions设置允许开启jinjia2拓展模块
    private_key_file用于指定私钥文件存储位置
    display_skipped_hosts用于显示任何跳过任务的状态
    system_warnings用于禁用系统显示ansible潜在问题警告
    deprecation_warningsplaybook输出禁用“不建议使用”警告
    command_warningscommand模块Ansible默认发出警告
    pipelining用于开启pipe SSH通道优化

    三、临时命令的用法

    一种最简单的临时命令使用ping模块。此模块不执行ICMP ping,而是检查能否在受管主机上运行基于Python的模块。例如,以下临时命令确定清单中的所有受管主机能否运行标准的模块:

    
    [root@localhost ~]# vim /etc/ansible/inventory 
    web1 ansible_user=root ansible_password=runtime
    [root@localhost ~]# vim /etc/hosts
    192.168.183.140 web1
    [root@localhost ~]# ssh web1
    [root@localhost ~]# ansible web1 -m ping
    web1 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Ansible常用模块

    模块类别模块
    文件模块copy:将本地文件复制到受管主机 file:设置文件的权限和其他属性 lineinfile:确保特定行是否在文件中 synchronize:使用rsync同步内容
    软件包模块package:使用操作系统本机的自动检测软件包管理器管理软件包 yum:使用yum管理软件包 apt:使用APT管理软件包 dnf:使用dnf管理软件包 gem:管理Ruby gem pip:从PyPI管理Python软件包
    系统模块firewalld:使用firewalld管理防火墙 reboot:重启计算机 service:管理服务 user:添加、删除和管理用户帐户
    Net Tools模块get_url:通过HTTP、HTTPS或FTP下载文件 nmcli:管理网络 uri:与Web服务交互

    临时命令使用user模块来确保runtime用户存在于web1上并且其UID为4000:

    [root@localhost ~]# ansible web1 -m user -a 'name=runtime uid=4000 state=present'
    web1 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "comment": "",
        "create_home": true,
        "group": 4000,
        "home": "/home/runtime",
        "name": "runtime",
        "shell": "/bin/bash",
        "state": "present",
        "system": false,
        "uid": 4000
    }
    [root@localhost ~]# ansible all -a 'id runtime'
    web1 | CHANGED | rc=0 >>
    uid=4000(runtime) gid=4000(runtime) groups=4000(runtime)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    LangChain 摘要 和问答示例
    [论文精读|博士论文]面向文本数据的关系抽取关键技术研究
    C/C++教程 从入门到精通《第二十六章》——Linux开发服务器详解
    【深入理解java虚拟机】 - 类加载器与双亲委派模型
    wget参数使用说明
    微擎模块 超人跑腿 1.7.1 后台模块+前端小程序,后台新增代办,代驾,家政模板自定义
    Fundamentals of Electrostatic Discharge-SUMMARY
    基于ssm流浪动物救助管理系统
    【GUI】-- 10 贪吃蛇小游戏之静态面板绘制
    1536_AURIX_TriCore内核架构_Trap
  • 原文地址:https://blog.csdn.net/qq_65998623/article/details/127429265