03_ansible inventory、Hosts and Groups、Host Variables、Group Variables、Default groups、Pattern

4.ansible inventory
4.1.Hosts and Groups
4.1.1.案例
4.2.Host Variables
4.3.Group Variables
4.4.Groups of Groups, and Group Variables
4.5.Default groups
4.6.Splitting Out Host and Group Specific Data
4.7.List of Behavioral Inventory Parameters
4.7.1.一般所有连接
4.7.2.非SSH连接类型
5.Pattern

4.ansible inventory

转自:https://www.cnblogs.com/mhc-fly/p/7080974.html

同时可以在您的基础设施中对多个系统进行安全解决。 它可以通过选择Ansible的inventory中列出的系统部分,默认保存在/etc/ansible/hosts 。 您可以使用命令行上的-i 选项指定其他inventory文件。
此inventory不仅可以配置,还可以同时使用多个inventory文件,并从动态inventory中描述,从动态或云源中提取inventory。 在2.4版中引入,Ansible具有inventory插件,使其灵活可定制。

4.1.Hosts and Groups

inventory文件可以是许多格式之一,具体取决于您拥有的inventory插件。 对于这个例子, /etc/ansible/hosts的格式是一个INI(类似于Ansible的默认设置),如下所示:

# 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

# 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

括号中的标题是组名称,用于分类系统,并决定您在什么时间控制哪些系统以及用于什么目的。
将系统放在多个组中是可以的,例如服务器可以是一个webserver和一个dbserver。 如果你这样做,请注意,变量将来自他们所属的所有组。可变优先级在后面的章节中有详细介绍。

如果您拥有运行在非标准SSH端口上的主机,可以将端口号写在主机名的后边,用冒号隔开。 您的SSH配置文件中列出的端口不会与paramiko连接一起使用,但将与openssh连接一起使用。

要使事情明确,建议您在缺省端口上运行的情况下进行设置:

badwolf.example.com:5309

假设你只有静态IP,并想要设置一些存在于你的主机文件中的别名,或者你通过隧道进行连接。你也可以这样描述主机:

badwolf.example.com:5309

在上面的示例中,尝试对主机别名“jumper”(可能甚至不是真正的主机名)可以联系,将联系端口5555上的192.0.2.50。请注意,这是使用Inventory文件的功能来定义一些特殊变量。 一般来说,这不是定义描述系统策略的变量的最佳方法,但是稍后我们将分享有关这方面的建议。 我们刚刚开始
添加很多主机? 如果你有很多主机遵循类似的模式,你可以这样做,而不是列出每个主机名:

[webservers]
www[01:50].example.com

对于数字模式,可以根据需要包括或删除前导零。范围使包容性 您还可以定义字母范围:

[databases]
db-[a:f].example.com

还可以在每个主机的基础上选择连接类型和用户:

[targets]
localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan

如上所述,在Inventory文件中设置这些只是一个简写,我们将在稍后再讨论如何将它们存储在“host_vars”目录中的各个文件中。

4.1.1.案例

在/etc/ansible/hosts中配置如下:

[webserver]
172.17.0[3:6]

然后执行:

[root@node2 ansible]# ansible webserver -m ping
172.17.05 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
172.17.03 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
172.17.04 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
172.17.06 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@node2 ansible]#

4.2.Host Variables

如上所述,很容易将变量分配给稍后将在playbook中使用的主机:

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

4.3.Group Variables

变量也可以一次性应用于整个组:

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

请注意,这只是将变量一次应用于多个主机的便利方式。 即使您可以按组来定位主机,在执行play之前,变量总是被平坦化为主机级别。

4.4.Groups of Groups, and Group Variables

也可以使用:children后缀来创建组组,如上所述,您可以使用:vars应用:vars :

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

如果需要存储列表或散列数据,或者希望将主机和组的特定变量与Inventory文件分开,请参阅下一节。 子组有几个属性要注意:

  • 首先,作为子组成员的任何主机自动是父组的成员。
  • 第二,子组的变量将具有较高的优先级(覆盖)父组的变量。

4.5.Default groups

有两个默认组: all和ungrouped 。 all包含每个主机。 ungrouped包含除所有主机外没有其他组的所有主机。

4.6.Splitting Out Host and Group Specific Data

Ansible中的首选实践实际上不是在主Inventory文件中存储变量。
除了将变量直接存储在INI文件中,主机和组变量可以存储在相对于Inventory文件的单个文件中。
这些变量文件采用YAML格式。 有效的文件扩展名包括’.yml’,‘.yaml’,'.json’或没有文件扩展名。 如果您是YAML的新手,请参阅YAML语法 。
假设库存文件路径是:

/etc/ansible/hosts

如果主机被命名为“foosball”,并且在“raleigh”和“webservers”组中,以下位置的YAML文件中的变量将提供给主机:

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

例如,假设您拥有按数据中心的分组的主机,并且每个数据中心使用一些不同的服务器。”releign”组的文件”/etc /ansible / group_vars / raleigh”中的数据可能如下所示:

ntp_server: acme.example.org
database_server: storage.example.org

这些文件不存在就可以了,因为这是一个可选的功能。
作为一个高级用例,您可以创建以组或主机命名的目录,而Ansible将读取这些目录中的所有文件。”raleigh”组的一个例子:

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings

“raleigh”组中的所有主机都将具有这些文件中定义的变量。 当单个文件开始太大时,或者当您想在组变量的一部分使用可安全保管库时,这可能非常有用。 请注意,这只适用于Ansible 1.4或更高版本。

提示:在Ansible 1.2或更高版本中, group_vars/和host_vars/目录可以存在于playbook目录或Inventory目录中。 如果两个路径都存在,则playbook目录中的变量将覆盖在Inventory目录中设置的变量。

提示:将Inventory文件和变量保存在git repo(或其他版本控制)中时跟踪Inventory变量和Inventory目录中。如果两个路径都存在,则playbook目录中的变量将覆盖在Inventory目录中设置的变量。

提示:将Inventory文件和变量保存在git repo(或其他版本控制)中是跟踪Inventory变量和主机变量的极好方法。

4.7.List of Behavioral Inventory Parameters

设置以下变量控制如何可以与远程主机进行交互。

ansible_connection:
连接类型到主机。这可以是任何ansible的连接插件的名称。SSH协议类型是smart, ssh或paramiko。默认是smart。下一节将介绍基于非SSH的类型。

4.7.1.一般所有连接

ansible_host
要连接到的主机的名称,如果与要提供的别名不同。
ansible_port
ssh端口号,如果不是22
ansible_user
要使用的默认ssh用户名。
特定于SSH连接:

ansible_ssh_pass
要使用的ssh密码(不要以纯文本形式存储此变量;请始终使用保管库。请参阅变量和保管库 )
ansible_ssh_private_key_file
ssh使用的私钥文件。 如果使用多个键,并且您不想使用SSH代理程序,则很有用。
ansible_ssh_common_args
此设置始终附加到sftp , scp和ssh的默认命令行。 有用于配置特定主机(或组)的ProxyCommand 。
ansible_sftp_extra_args
此设置始终附加到默认sftp命令行。
ansible_scp_extra_args
此设置始终附加到默认的scp命令行。
ansible_ssh_extra_args
此设置始终附加到默认的ssh命令行。
ansible_ssh_pipelining
确定是否使用SSH流水线。 这可以覆盖ansible.cfg的pipelining设置。
ansible_ssh_executable(在版本2.2中添加)
此设置将覆盖使用系统ssh的默认行为。 这可以覆盖ssh_executable中的ssh_executable设置。
特权升级(有关详细信息,请参阅可选权限升级 ):

ansible_become
相当于ansible_sudo或ansible_su ,允许强制特权升级
ansible_become_method
允许设置权限升级方法
ansible_become_user
相当于ansible_sudo_user或ansible_su_user ,允许设置您成为通过特权升级的用户
ansible_become_pass
相当于ansible_sudo_pass或ansible_su_pass ,您可以设置权限升级密码(永远不要以纯文本形式存储此变量;请始终使用保管库 ),请参见变量和保管库 )
远程主机环境参数:

ansible_shell_type
目标系统的shell类型。 除非您将ansible_shell_executable设置为非Bourne(sh)兼容shell,否则不应使用此设置。 默认情况下,使用sh -style语法格式化命令。 将此设置为csh或fish将导致在目标系统上执行的命令来跟随这些shell的语法。
ansible_python_interpreter
目标主机python路径。 这对于具有多个Python或不在/ usr / bin / python (如* BSD)的系统或/ usr / bin / python不是2.X系列Python的系统非常有用。 我们不使用/ usr / bin / env机制,因为这要求远程用户的路径设置正确,并且还假定python可执行文件被命名为python,其中可执行文件可能被命名为python2.6 。
ansible _ * _解释
适用于任何类似ruby或perl的工作,就像ansible_python_interpreter一样。 这将替代将在该主机上运行的模块的shebang。
2.1版新功能

ansible_shell_executable
这将设置可控制器将在目标计算机上使用的shell,覆盖默认为/ bin / sh的 ansible.cfg中的executable 。 如果不可能使用/ bin / sh (即/ bin / sh未安装在目标机器上或不能从sudo运行),那么您应该只能改变它。
Ansible-INI主机文件中的示例:

some_host ansible_port = 2222 ansible_user = manager
aws_host ansible_ssh_private_key_file = / home / example / .ssh / aws.pem
freebsd_host ansible_python_interpreter = / usr / local / bin / python
ruby_module_host ansible_ruby_interpreter = / usr / bin / ruby​​.1.9.3

4.7.2.非SSH连接类型

使用主机特定参数ansible_connection= ,可以更改连接类型。 以下基于非SSH的连接器可用:

local
该连接器可以用于将该playbook部署到控制机器本身。

docker
该连接器使用本地Docker客户机将该playbook直接插入Docker容器。 以下参数由此连接器处理:

ansible_host
要连接的Docker容器的名称。
ansible_user
在容器内操作的用户名。 用户必须存在于容器内。
ansible_become
如果设置为true ,则将使用become_user在容器内操作。
ansible_docker_extra_args
可以是由Docker理解的任何其他参数的字符串,它们不是特定于命令的。 此参数主要用于配置远程Docker守护程序以使用。
以下是如何立即部署到创建的容器的示例:

- name: create jenkins container
  docker_container:
    docker_host: myserver.net:4243
    name: my_jenkins
    image: jenkins

- name: add container to inventory
  add_host:
    name: my_jenkins
    ansible_connection: docker
    ansible_docker_extra_args: "--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/client-cert.pem --tlskey=/path/to/client-key.pem -H=tcp://myserver.net:4243"
    ansible_user: jenkins
  changed_when: false

- name: create directory for ssh keys
  delegate_to: my_jenkins
  file:
    path: "/var/jenkins_home/.ssh/jupiter"
    state: directory

5.Pattern

在Ansible中,Patterns 是指我们怎样确定由哪一台主机来管理. 意思就是与哪台主机进行交互. 但是在:doc:playbooks 中它指的是对应主机应用特定的配置或执行特定进程.

我们再来复习下:doc:intro_adhoc 章节中介绍的命令用法,命令格式如下:

ansible <pattern_goes_here> -m <module_name> -a <arguments>

示例如下:

ansible  webservers  -m service -a “name=httpd state=restarted” 

一个pattern通常关联到一系列组(主机的集合) –如上示例中,所有的主机均在 “webservers” 组中.

不管怎么样,在使用Ansible前,我们需事先告诉Ansible哪台机器将被执行. 能这样做的前提是需要预先定义唯一的 host names 或者 主机组.

如下的patterns等同于目标为仓库(inventory)中的所有机器:

all
*

也可以写IP地址或系列主机名:

one.example.com
one.example.com: two.example.com
192.168.1.50
192.168.1.*

如下patterns分别表示一个或多个groups.多组之间以冒号分隔表示或的关系.这意味着一个主机可以同时存在多个组:

webservers
webservers: dbservers

你也可以排队一个特定组,如下实例中,所有执行命令的机器必须隶属 webservers 组但同时不在 phoenix组:

webservers:!phoenix

你也可以指定两个组的交集,如下实例表示,执行命令有机器需要同时隶属于 webservers 和 staging 组.

webservers:&staging

你也可以组合更复杂的条件

webservers:dbservers:&staging:!phoenix

上面这个例子表示“‘webservers’ 和 ‘dbservers’ 两个组中隶属于 ‘staging’ 组并且不属于 ‘phoenix’ 组的机器才执行命令”

你也可以使用变量如果你希望通过传参指定group,ansible-playbook通过 “-e” 参数可以实现,但这种用法不常用:

webservers:!{{excluded}}:&{{required}}

你也可以不必严格定义groups,单个的host names, IPs , groups都支持通配符:

*.example.com
*.com

Ansible同时也支持通配和groups的混合使用:

one*.com:dbservers

在高级语法中,你也可以在group中选择对应编号的server:

webservers[0]

或者一个group中的一部分servers:

webservers[0-25]

大部分人都在patterns应用正则表达式,但你可以.只需要以 ‘~’ 开头即可:

~(web|db).*\.example\.com

同时让我们提前了解一些技能,除了如上,你也可以通过–limit标记来添加排除条件,/usr/bin/ansible or /usr/bin/ansible-playbook都支持:

ansible-playbook site.xml --limit datacenter2

如果你想从文件读取hosts,文件名以@为前缀即可.从Ansible 1.2开始支持该功能:

ansible-playbook site.yml --limit @retry_hosts.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值