在yaml文件中定义的主机上运行任务,而不是使用ansible在资源清册中运行任务
我是Ansible新手,尝试从下面的config.yaml文件而不是清单中获取主机,以便在这些主机上运行任务。我如何在主剧本中做到这一点在yaml文件中定义的主机上运行任务,而不是使用ansible在资源清册中运行任务,ansible,yaml,Ansible,Yaml,我是Ansible新手,尝试从下面的config.yaml文件而不是清单中获取主机,以便在这些主机上运行任务。我如何在主剧本中做到这一点 service: web-app: common: tomcat: port: 80 hosts: all: - abc.com - pqr.com 如果我必须在这些服务器上运行某些任务,是否有办法访问我的剧本中的abc.com和pqr.com?基础:加载数据 以下示例所需
service:
web-app:
common:
tomcat:
port: 80
hosts:
all:
- abc.com
- pqr.com
如果我必须在这些服务器上运行某些任务,是否有办法访问我的剧本中的abc.com和pqr.com?基础:加载数据
以下示例所需的基本ansible函数为:
文件/service_config.yml
中添加了上面的yaml示例(在解决了缩进问题之后)。如果文件位于files
子目录中,只需更改文件名,如果文件位于项目之外,则使用文件的完整路径
结合以上内容,您可以使用以下jinja2表达式获得主机列表
{{ (lookup('file', 'service_config.yml') | from_yaml).service.hosts.all }}
注意:如果控制器上没有自定义yaml文件,则首先需要使用或模块在本地获取数据
使用内存资源清册
在本例中,我创建了一个动态组custom\u group
,在一个剧本上运行一个任务,目标是localhost
,然后在下一个剧本中以该自定义组为目标。如果要在这些主机上运行大量任务,这可能是最好的选择
---
- name: Prepare environment
hosts: localhost
gather_facts: false
vars:
# Replace with full path to actual file
# if this one is not in your 'files' subdir
my_config_file: service_config.yml
my_custom_hosts: "{{ (lookup('file', my_config_file) | from_yaml).service.hosts.all }}"
tasks:
- name: Create dynamic group from custom yaml file
add_host:
name: "{{ item }}"
group: custom_group
loop: "{{ my_custom_hosts }}"
- name: Play on new custom group
hosts: custom_group
gather_facts: false
tasks:
- name: Show we can actually contact the group
debug:
var: inventory_hostname
其中:
PLAY [Prepare environment] **********************************************************************************************************************************************************************************************************************************************
TASK [Create dynamic group from custom yaml file] ***********************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=abc.com)
changed: [localhost] => (item=pqr.com)
PLAY [Play on new custom group] *****************************************************************************************************************************************************************************************************************************************
TASK [Show we can actually contact the group] ***************************************************************************************************************************************************************************************************************************
ok: [abc.com] => {
"inventory_hostname": "abc.com"
}
ok: [pqr.com] => {
"inventory_hostname": "pqr.com"
}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
abc.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
pqr.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
PLAY [Delegation example] ***********************************************************************************************************************************************************************************************************************************************
TASK [Task played on our current target host list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"inventory_hostname": "localhost"
}
TASK [Fake task delegated to our list of custom host] *******************************************************************************************************************************************************************************************************************
ok: [localhost -> abc.com] => (item=abc.com) => {
"msg": "I would run on abc.com with facts from localhost"
}
ok: [localhost -> pqr.com] => (item=pqr.com) => {
"msg": "I would run on pqr.com with facts from localhost"
}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
使用授权
在下面的示例中,我使用更改针对其他主机的播放中的目标主机
如果要在自定义主机上运行的任务很少,并且/或者需要来自当前播放主机的事实来运行这些任务,则更适合使用此选项。请参阅上面文档中的负载平衡器示例以获得更深入的解释
---
- name: Delegation example
hosts: localhost
gather_facts: false
vars:
# Replace with full path to actual file
# if this one is not in your 'files' subdir
my_config_file: service_config.yml
my_custom_hosts: "{{ (lookup('file', my_config_file) | from_yaml).service.hosts.all }}"
tasks:
- name: Task played on our current target host list
debug:
var: inventory_hostname
- name: Fake task delegated to our list of custom host
# Note: we play it only once so it does not repeat
# if the play `hosts` param is a group of several targets
# This is for example only and is not really delegating
# anything in this case. Replace with your real life task
debug:
msg: "I would run on {{ item }} with facts from {{ inventory_hostname }}"
delegate_to: "{{ item }}"
run_once: true
loop: "{{ my_custom_hosts }}"
其中:
PLAY [Prepare environment] **********************************************************************************************************************************************************************************************************************************************
TASK [Create dynamic group from custom yaml file] ***********************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=abc.com)
changed: [localhost] => (item=pqr.com)
PLAY [Play on new custom group] *****************************************************************************************************************************************************************************************************************************************
TASK [Show we can actually contact the group] ***************************************************************************************************************************************************************************************************************************
ok: [abc.com] => {
"inventory_hostname": "abc.com"
}
ok: [pqr.com] => {
"inventory_hostname": "pqr.com"
}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
abc.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
pqr.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
PLAY [Delegation example] ***********************************************************************************************************************************************************************************************************************************************
TASK [Task played on our current target host list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"inventory_hostname": "localhost"
}
TASK [Fake task delegated to our list of custom host] *******************************************************************************************************************************************************************************************************************
ok: [localhost -> abc.com] => (item=abc.com) => {
"msg": "I would run on abc.com with facts from localhost"
}
ok: [localhost -> pqr.com] => (item=pqr.com) => {
"msg": "I would run on pqr.com with facts from localhost"
}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
这是美丽的,我尝试了你的第一个选择,它无缝工作。非常感谢。如果这解决了您的问题,您应该接受答案,以便其他人知道有一个可接受的解决方案,并最终投票表决(这是说谢谢而不是只说谢谢的评论的方式)。很高兴我能帮忙。