“Ansible配置”的版本间的差异
(→inventory文件) |
(→主机清单(inventory)) |
||
第47行: | 第47行: | ||
默认inventory文件路径为/etc/ansible/hosts,我们也可以通过Ansible的配置文件来指定inventory文件位置。除默认文件外,可以同时使用多个inventory文件,也可以从动态源或云上拉取inventory配置信息。 | 默认inventory文件路径为/etc/ansible/hosts,我们也可以通过Ansible的配置文件来指定inventory文件位置。除默认文件外,可以同时使用多个inventory文件,也可以从动态源或云上拉取inventory配置信息。 | ||
− | + | ====简单设置==== | |
一个简单的inventory文件示例如下。 | 一个简单的inventory文件示例如下。 | ||
<nowiki>192.168.0.1</nowiki> | <nowiki>192.168.0.1</nowiki> | ||
+ | ====分组设置==== | ||
一个对主机进行分组的inventory文件: | 一个对主机进行分组的inventory文件: | ||
第82行: | 第83行: | ||
proxy=proxy.atlanta.example.com</nowiki> | proxy=proxy.atlanta.example.com</nowiki> | ||
+ | |||
+ | ====指定连接和用户名==== | ||
对于每一个host,还可以选择连接类型和连接用户名。 | 对于每一个host,还可以选择连接类型和连接用户名。 | ||
<nowiki>localhost ansible_connection=local | <nowiki>localhost ansible_connection=local | ||
other1.example.com ansible_connection=ssh ansible_ssh_user=pdchaan</nowiki> | other1.example.com ansible_connection=ssh ansible_ssh_user=pdchaan</nowiki> | ||
+ | |||
+ | ===主机清单示例=== | ||
+ | |||
+ | |||
+ | [root@localhost ~]# mkdir /etc/ansible | ||
+ | |||
+ | [root@localhost ~]# cd /etc/ansible | ||
+ | |||
+ | [root@localhost ansible]# vi hosts | ||
+ | |||
+ | <nowiki> | ||
+ | [master] | ||
+ | localhost ansible_connection=local ansible_ssh_user=root | ||
+ | 10.0.0.30 ansible_ssh_user=root | ||
+ | [slave] | ||
+ | 10.0.0.32 ansible_ssh_user=root | ||
+ | |||
+ | </nowiki> | ||
+ | |||
+ | 有可能每台机器登录的用户名都不一样,这里指定每台机器连接的SSH登录用户名,在执行Ansible命令时就不需要再指定用户名。如果不指定用户名,Ansible就会尝试使用本机已登录的用户名登录远程主机。 | ||
==配置实例== | ==配置实例== |
2021年2月12日 (五) 09:16的版本
目录
配置文件
ansible主要有两个配置文件:
ansible.cfg 和 inventory文件
Ansible.cfg是ansible自动化任务所用的一个核心配置文件,
Ansible可同时操作属于一个组的多台主机,是通过inventory文件配置来实现的,
ansible.cfg
文件位置
1. 环境变量ANSIBLE_CONFIG所指向的位置。
2. 当前目录下的ansible.cfg。
3. HOME目录下的配置文件~/.ansible.cfg。
4. /etc/ansible/ansible.cfg。
在大多数场景下默认的配置就能满足大多数用户的需求。在一些特殊场景下,用户还需要自行修改这些配置文件,安装后如果没有在上述三个位置找到配置文件,那么在HOME目录新建一个.ansible.cfg文件即可。
常见的配置参数
Ansible常见的配置参数如下。
- inventory = ~/ansible_hosts:表示主机清单inventory文件的位置。
- forks = 5:并发连接数,默认为5。
- sudo_user = root:设置默认执行命令的用户。
- remote_port = 22:指定连接被管节点的管理端口,默认为22端口。建议修改,能够更加安全。
- host_key_checking = False:设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例。
- timeout = 60:设置SSH连接的超时时间,单位为秒。
- log_path = /var/log/ansible.log:指定一个存储Ansible日志的文件(默认不记录日志)。
其中参数的取值范围及更加详细的配置请参考官方文档:https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg。
主机清单(inventory)
Ansible可同时操作属于一个组的多台主机,是通过inventory文件配置来实现的,
组与主机的关系也是由inventory来定义的。
主机可以从属于组,后者通常用于标识主机的角色。一个主机可以属于多个组
编写主机清单
可以通过两种方式定义主机清单:通过文本文件定义静态主机清单,或从外部 provider 生成动态主机清单。
默认inventory文件路径为/etc/ansible/hosts,我们也可以通过Ansible的配置文件来指定inventory文件位置。除默认文件外,可以同时使用多个inventory文件,也可以从动态源或云上拉取inventory配置信息。
简单设置
一个简单的inventory文件示例如下。
192.168.0.1
分组设置
一个对主机进行分组的inventory文件:
mail.example.com [webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com
其中方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理。一个系统可以属于不同的组,比如一台服务器可以同时属于webserver组和dbserver组。这时属于两个组的变量都可以为这台主机所用。
分配变量给主机很容易做到,这些变量定义后可在playbooks中使用。
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
组的变量也可以定义属于整个组的变量。
[atlanta] host1 host2 [atlanta:vars] ntp_server =ntp.atlanta.example.com proxy=proxy.atlanta.example.com
指定连接和用户名
对于每一个host,还可以选择连接类型和连接用户名。
localhost ansible_connection=local other1.example.com ansible_connection=ssh ansible_ssh_user=pdchaan
主机清单示例
[root@localhost ~]# mkdir /etc/ansible
[root@localhost ~]# cd /etc/ansible
[root@localhost ansible]# vi hosts
[master] localhost ansible_connection=local ansible_ssh_user=root 10.0.0.30 ansible_ssh_user=root [slave] 10.0.0.32 ansible_ssh_user=root
有可能每台机器登录的用户名都不一样,这里指定每台机器连接的SSH登录用户名,在执行Ansible命令时就不需要再指定用户名。如果不指定用户名,Ansible就会尝试使用本机已登录的用户名登录远程主机。
配置实例
ansible.cfg
vi ~/ansible.cfg
[defaults] inventory = ~/ansible_hosts host_key_checking = False
inventory文件
[root@localhost ~]# mkdir /etc/ansible
[root@localhost ~]# cd /etc/ansible
[root@localhost ansible]# vi hosts
[master] localhost ansible_connection=local ansible_ssh_user=root 10.0.0.30 ansible_ssh_user=root [slave] 10.0.0.32 ansible_ssh_user=root
有可能每台机器登录的用户名都不一样,这里指定每台机器连接的SSH登录用户名,在执行Ansible命令时就不需要再指定用户名。如果不指定用户名,Ansible就会尝试使用本机已登录的用户名登录远程主机。