Connection methods and details — Ansible Community Documentation
1 build your inventory
1.1
The default location for this file is /etc/ansible/hosts
. You can specify a different inventory file at the command line using the -i <path>
option or in configuration using inventory
.
mkdir -p /ansible_quickstart
# 建立一个ini格式的inventory
touch inventory.ini
cat inventory.ini
[myhosts]
172.31.18.222
# 或者创建一个yaml格式的inventory
touch inventory.yaml
cat inventory.yaml
myhosts:
hosts:
my_host_01:
ansible_host: 172.31.18.222
1.2 Passing multiple inventory sources
ansible-playbook get_logs.yml -i staging -i production
1.3 Inventory aliases
In ini format
jumper ansible_port=5555 ansible_host=192.0.2.50
In yaml format
# ...
hosts:
jumper:
ansible_port: 5555
ansible_host: 192.0.2.50
2 Patterns: targeting hosts and groups
2.1 Using patterns
ansible <pattern> -m <module_name> -a "<module options>"
例子
ansible webservers -m service -a "name=httpd state=restarted"
In a playbook, the pattern is the content of the hosts:
line for each play:
- name: <play_name>
hosts: <pattern>
例子
- name: restart webservers
hosts: webservers
2.2 常见模式
Description | Pattern(s) | Targets |
---|---|---|
All hosts | all (or *) | |
One host | host1 | |
Multiple hosts | host1:host2 (or host1,host2) | |
One group | webservers | |
Multiple groups | webservers:dbservers | all hosts in webservers plus all hosts in dbservers |
Excluding groups | webservers:!atlanta | all hosts in webservers except those in atlanta |
Intersection of groups | webservers:&staging | any hosts in webservers that are also in staging |
例子
webservers:dbservers:&staging:!phoenix
targets all machines in the groups ‘webservers’ and ‘dbservers’ that are also in the group ‘staging’, except for any machines in the group ‘phoenix’.
标签:staging,webservers,host,ansible,hosts,inventory,使用手册 From: https://www.cnblogs.com/goldtree358/p/18285659