logo
0
0
WeChat Login

Inventory

inventory 库存;清单。 Ansible 的目标们,就是清单。清单上列举了所有需要管理的机器。

示例:

ansible-target-1 ansible_host=124.220.168.49 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/Ansible_tc1.pem
  • ansible-target-1:机器名称。
  • ansible_host:目标机器的 IP 地址。
  • ansible_connection:连接类型。ssh 表示使用 SSH 连接。
  • ansible_user:登录用户名。
  • ansible_ssh_private_key_file:私钥文件路径。(如果不在这里设置私钥,则需要通过 ssh-agent 来管理私钥)

这里的 SSH 相关参数可以通过 ~/.ssh/config 来设置

云服务商提供了方式用来给云服务器绑定私钥。


Getting Started with Ansible 是一个很好的学习例子

https://docs.ansible.com/projects/ansible/latest/getting_started/get_started_inventory.html

初学 Ansible 不知道如何指定主机,我们需要创建一个清单文件,清单文件可以是两种格式:INI 格式和 YAML 格式。

ansible-inventory 查看 inventory 的 hosts 信息。

$ ansible-inventory -i empty.ini --list { "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped" ] } } $ ansible-inventory --list -i /dev/null { "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped" ] } }

inventory 默认就有两个组:all, ungrouped。所有的主机都在这两个组下。一个 inventory 就是组织 hosts or nodes

下面是 INI 格式的例子:

myserver.ini:

[myservers]

可以看到,我们有了一个自己定义的组。

$ ansible-inventory --list -i myserver.ini { "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped", "myserver" ] } }

但是目前我们还没有任何的 hosts。

下面指定一个 hosts:

[servers] Ark ansible_host=115.191.24.187 ansible_connection=ssh ansible_user=root ansible_ssh_private_key_file=~/.ssh/volc1.pem

[servers] 创建组。每一行是一个 host,host 首先指定一个 IP 或者域名,也可以是一个名字而已。后面可以指定各种参数, 参见:

https://docs.ansible.com/projects/ansible/latest/inventory_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters


INI 格式简单,但是 YAML 格式更常见

下面是 yaml 格式的例子。yaml 格式看起来要复杂一些,但是看起来更加规范。

myservers: hosts: host_01: ansible_host: 192.168.1.100

开头是 Group,下面指定 Group 的 hosts。然后是每个 host 的参数。

localhost.yml:

all: hosts: localhost:

我们可查看这个 inventory 文件到底是什么样的:

$ ansible-inventory -i localhost.yml --list { "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped" ] }, "ungrouped": { "hosts": [ "localhost" ] } }

localhost 不指定任何的参数,就相当于我们把 localhost 当作域名指定了主机。

all 是默认的 group,包含了所有的主机。

playbook

这是最简单的一个 Playbook。

playbook.yml:

--- # Ansible Playbook - A declarative configuration management file # Playbooks are written in YAML and define the desired state of your systems - name: My first playbook # Play name (optional but recommended for readability) hosts: localhost # Target hosts: 'localhost' can be implicit or defined in inventory file # Tasks: A list of actions to perform on the target hosts tasks: # Task 1: Simple connectivity test using Ansible's ping module # The ping module checks if Ansible can connect to the target node - name: Hello World # Task name (optional, used in output/logs) ping: # Ansible built-in module - tests connectivity

Run this playbook,不给 inventory 文件,默认就有 localhost 可用:

ansible-playbook -i /dev/null playbook.yml

也可以在 inventory 文件中指定一个名为 localhost 的主机,这样可以覆盖默认的 localhost。

all: hosts: localhost:
[00:03] fbbd8015d9f7:workspace (main %) | ansible all -i /dev/null -m ping [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' [00:03] fbbd8015d9f7:workspace (main %) | ansible localhost -i /dev/null -m ping localhost | SUCCESS => { "changed": false, "ping": "pong" } [00:03] fbbd8015d9f7:workspace (main %) | ansible localhost -i localhost.yml -m ping localhost | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused", "unreachable": true }

/dev/null 作为我们的 inventory. localhost 才是本地。否则就会尝试 ssh 链接,因为默认的主机的连接方式是 ssh。

我们可以在 inventory 中明确指定一个连接方式。让 localhost 为本地,默认 inventory 的 hosts 是远程 ssh。

all: hosts: localhost: ansible_connection: local

dotfiles with ansible

dotfile.yml:

--- - name: Dotfile config hosts: localhost connection: local tasks: - name: hello world ping:

用 ansible 管理 dotfiles ,hosts 直接就应该是 localhost,inventory 默认可以是 /dev/null. connection 指定连接方式,但是注意优先级的影响。

https://docs.ansible.com/projects/ansible/latest/reference_appendices/general_precedence.html#general-precedence-rules

如果在 inventory 中的 hosts list 指定了 ansible_connection 会比 playbook 中的有更高的优先级。

常用的命令

ansible Run single task playbook ansible-playbook Runs Ansible playbooks, executing the defined tasks on the targeted hosts. ansible-inventory Show Ansible inventory information, by default it uses the inventory script JSON format.