知识点1:构建Ansible清单

  • 两种方式定义主机清单:

             静态主机清单:通过文本文件来定义,包括INI样式或YAML

             动态主机清单:根据需要使用外部信息提供程序通过脚本或其他程序完成。

  • INI样式

                servera.example.com   #主机名或IP地址模式

                192.168.0.113

                [web]  #web组模式定义多台主机或IP

                [db:children]   #:children定义嵌套组

                192.168.0.[0:55]  #通过范围简化主机规格,如例所示,192.168.0.0到192.168.0.55所有地址

默认有all和ungrouped组

  • 验证清单

[root@RHEL8 ~]# ansible web --list-hosts

[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

  hosts (1):

    192.168.0.106

  • 清单文件的位置

                默认清单位置:/etc/ansible/hosts     通常不使用该 文件,而是在Ansible配置文件中为清单文件定义一个不同位置,使用--inventory PATHNAME或-i PATHNAME指定

[student@workstation ansible]$ ansible all --list-hosts

  hosts (2):

    servera.lab.example.com

    serverb.lab.example.com

[student@workstation ansible]$ ansible ungrouped --list-hosts

  hosts (1):

    servera.lab.example.com

[student@workstation ansible]$ ansible webservers --list-hosts

  hosts (1):

    serverb.lab.example.com

  • 自定义清单文件位置(如需要访问,必须指定路径或者进入所在目录相看)

[student@workstation ansible]$ mkdir /home/student/deploy-inventory

[student@workstation ansible]$ vim /home/student/deploy-inventory/inventory

[student@workstation ansible]$ cd /home/student/deploy-inventory                             #方法1

[student@workstation deploy-inventory]$ ansible all -i inventory --list-hosts

  hosts (4):

    servera.lab.example.com

    serverb.lab.example.com

    serverd.lab.example.com

    serverc.lab.example.com

[student@workstation deploy-inventory]$

[student@workstation /]$ ansible all -i /home/student/deploy-inventory/inventory --list-host   #方法2

  hosts (4):

    servera.lab.example.com

    serverb.lab.example.com

    serverd.lab.example.com

    serverc.lab.example.com

知识点2:管理Ansible配置文件

  • 可以通过修改Ansible配置文件中的设置来自定义Ansible安装的行为,可从控制节点上多个可能的位置之一选择其配置文件。

            1.使用/etc/ansible/ansible.cfg     #全局,不常用

            2.使用~/.ansible.cfg     #用户的主目录,不常用

            3.使用./ansible.cfg       #执行命令所在目录

  • 使用ANSIBLE_CONFIG定义配置文件位置

            配置文件优先级: ANSIBLE_CONFIG-->命令所在目录ansible-->用户主目录.ansible-->/etc/ansible/ansible.cfg-->默认值

  • 显示ansible配置文件方法

[student@workstation /]$ ansible --version    #方法1

student@workstation ~]$ ansible web --list-hosts -v              #方法2

Using /home/student/ansible.cfg as config file

  • ansible配置文件一般由2部分组成

            [default] 部分设置Ansible操作的默认值

            [privilege_escalation] 配置Ansilbe如何在受管主机上执行特权升级

[student@workstation ~]$ cat /home/student/ansible.cfg

[defaults]

inventory=inventory

remote_user=root

sudo_user=root

ask_sudo_pass=True

[privilege_escalation]

#become=True

become_method=sudo

become_user=root

become_ask_pass=false

知识点3:运行临时命令

  • ansible-doc -l 列出ansible已经安装模块

  • 验证是否安装某个软件包命令

[student@workstation /]$ yum list installed ansible

Installed Packages

ansible.noarch                                             2.8.0-1.el8ae                                             @rhel-8-server-ansible-2.8-rpms

  • wget Linux系统下载文件工具

[student@workstation deploy-review]$ wget -O inventory/inventory http://materials.example.com/labs/deploy-review/inventory

总结:

  1. 任何其上装有Ansible且能够访问所需的配置文件和playbook以管理远程系统(受管主机)的系统都称为控制节点。

  2. 受管主机在清单中定义,主机模式用于引用清单 中定义的受管主机。

  3. 清单可以是静态文件,或者收程序从外部来源(如目录服务或云管理系统)动态生成。

  4. Ansible以一定的优先顺序在多个位置上寻找其配置文件。它将使用找到的第一个配置文件,并忽略所有其他文件。

  5. Ansible命令用于在受管主机上执行临时命令。

  6. 临时命令通过使用模块及其参数确定要执行的操作,也可先用Ansible的特权升级功能 。