ansible简单使用之inventory文件(主机清单)

发布时间 2023-07-12 18:02:54作者: 南风丶轻语

ansible简单使用之inventory文件(主机清单)

ansible inventory文件是干什么的

  • 一般称ansible inventory文件为ansible主机清单

  • ansible inventory文件定义了ansible需要操作的远程机器的信息,默认路径是/etc/ansible/hosts,也可以在命令行使用 -i <path> 指定路径。

  • inventory文件支持多种格式,常用的是INI格式和YAML格式

主机清单定义

我比较喜欢INI格式,下面就只记录INI格式的笔记了

  • 可以直接写在文件中,表示不属于任一组

  • 也可以通过[]指定所属的组

  • 一个被控机器可以属于多个组

  • 默认组

    默认有两个分组: all and ungroupedall 组顾名思义包括所有主机。 ungrouped 则是 all 组之外所有主机。所有的主机要不属于 all 组,要不就属于 ungrouped 组。

    尽管 allungrouped 始终存在,但它们以隐式的方式出现,而不出现在诸如 group_names 的组列表中。

例子:

#直接写IP或者hostname 默认属于Ungrouped组 但Ungrouped不会显式的写在文件中,只是隐式存在
green.example.com
blue.example.com
192.168.100.1
192.168.100.10

;属于webservers组的机器可以这么写
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110

# 有规律的主机名可以这么写
[webservers2]
www[001:006].example.com

;属于dbservers组的机器可以这么写
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
                       
# 有规律的主机名可以这么写
[dbservers2]
db-[99:101]-node.example.com
  • 可以把组作为另一个组的子成员

例子

;atlanta组包含host1和host2
[atlanta]
host1
host2

;raleigh组包含host2和host3
[raleigh]
host2
host3

;southeast组包括atlanta组和raleigh组,即host1和host2和host3
[southeast:children]
atlanta
raleigh

;usa组包括southeast组和northeast组和southwest组和northwest组
[usa:children]
southeast
northeast
southwest
northwest

主机变量

每个主机可以通过传递变量,在编写playbooks时使用

  • 可以直接写在主机IP或者hostname后面,仅供主机使用
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
  • 可以通过:var定义组的变量,组的变量可以供组内主机使用
[southeast]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
  • 把所有的变量都写在inventory中,看起来很臃肿,可以通过文件指定变量

假设 inventory 文件的路径为:

/etc/ansible/hosts

假设有一个主机名为 ‘foosball’, 主机同时属于两个组,一个是 ‘raleigh’, 另一个是 ‘webservers’. 那么以下配置文件(YAML 格式)中的变量可以为 ‘foosball’ 主机所用.依次为 ‘raleigh’ 的组变量,’webservers’ 的组变量,’foosball’ 的主机变量:

/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

group_vars目录下对应的就是组的变量

host_vars目录下对应就是host的变量

文件可以以’.yml’, ‘.yaml’, ‘.json’ 结尾,甚至没有扩展结尾。

inventory的内置参数

  • ansible_connection

    连接受控主机的方式. 填写 ansible 连接的插件名字,如 smart, ssh or paramiko. 默认: smart.

  • ansible_host

    要连接的主机名,如果与您要给它提供的别名不同。

  • ansible_port

    连接端口,默认22

  • ansible_user

    连接远程主机的用户

  • ansible_password

SSH 连接选项:

  • ansible_ssh_private_key_file

    指定 ssh 私钥。

  • ansible_ssh_common_args

    这个设置通常添加在默认命令行 sftp, scp and ssh 之后。 当为一台主机或组配置 ProxyCommand 时有用。

  • ansible_sftp_extra_args

    此设置始终附加在默认的 sftp 命令行中。

  • ansible_scp_extra_args

    此设置始终附加在默认的 scp 命令行中。

  • ansible_ssh_extra_args

    此设置始终附加在默认的 ssh 命令行中。

  • ansible_ssh_pipelining

    设置是否使用 SSH 管道,可以在 ansible.cfg 设置

  • ansible_ssh_executable (added in version 2.2)

    此设置将覆盖默认行为以使用系统 ssh。 这样会覆盖 ansible.cfg 文件中的 ssh_executable 设置

提权设置 ( 更多请参考 Ansible Privilege Escalation ):

  • ansible_become

    等同 ansible_sudo or ansible_su, 允许强制特权升级

  • ansible_become_method

    允许设置权限提升方法

  • ansible_become_user

    等同 ansible_sudo_user or ansible_su_user, 允许设置通过特权升级成为的用户

  • ansible_become_password

    等同 ansible_sudo_password or ansible_su_password, 允许您设置特权升级密码。 ( 切勿存储明文密码,请务必使用加密的方式 。详见 Keep vaulted variables safely visible)

  • ansible_become_exe

    等同 to ansible_sudo_exe or ansible_su_exe, 允许您为所选的升级方法设置可执行文件

  • ansible_become_flags

    等同 ansible_sudo_flags or ansible_su_flags, 允许您设置传递给所选升级方法的标志。 也可以在中 ansible.cfg 全局设置 sudo_flags

远程主机环境变量选项:

  • ansible_shell_type

    指定远程主机使用的 Shell。 在使用该选项前一定要先将 ansible_shell_executable 设置为 non-Bourne (sh) 。 默认命令使用 sh. 设置 csh or fish 将会在远程主机上使用 csh fish,而非默认的 sh

  • ansible_python_interpreter

    目标主机 python 目录。 对于一台主机上有多个 Python 环境或者默认路径不是 /usr/bin/python 的 *BSD 环境,或者 where /usr/bin/python 的不是 2.X 系统的 Python 情况有用。我们不使用:command:/usr/bin/env 命令机制,因为这需要设置远程用户的路径,并且假定 python 可执行文件名为 python ,其中可执行文件可能命名为像 python2.6 一样的程序。

  • ansible_*_interpreter

    适用于 ruby or perl 等类型 ansible_python_interpreter 环境。这将替换运行模块在远程主机上的 shabang.

  • ansible_shell_executable

    设置远程主机使用何种 shell,默认 /bin/sh,会覆盖 executable in ansible.cfg。 如果远程主机没有安装 /bin/sh ,则需要修改下了。 ( 比如: /bin/sh 在远程主机没有安装或者无法 sudo 运行 )

链接

Inventory文件 — 国内最专业的Ansible中文官方学习手册

Inventory 使用进阶 — Ansible Documentation (cn-ansibledoc.readthedocs.io)