ansible - 在ansible中,我可以为模块使用变量而不是使用模块名称吗?
问题描述
我正在使用 ansible 2.9,我想知道是否可以使用变量而不是实际的模块名称。例如:
---
- name: A Network play
hosts: routers
vars:
router_module: ios_command
tasks:
- name: a network task
{{router_module}}:
commands:
- show ip int brief
解决方案
不可以。您不能为模块使用变量,但您可以从模板动态创建包含任务的文件,并将这些文件包含在剧本中。例如,请参阅下面的剧本和模板
shell> cat test.yml
- hosts: localhost
vars:
pb_tasks:
- module: debug
params:
- {key: var, val: inventory_hostname}
- module: debug
params:
- {key: msg, val: End of dynamic play}
tasks:
- file:
state: directory
path: tasks
- template:
src: task.yml.j2
dest: tasks/task-{{ ansible_loop.index }}.yml
loop: "{{ pb_tasks }}"
loop_control:
extended: true
- include_tasks: tasks/task-{{ ansible_loop.index }}.yml
loop: "{{ pb_tasks }}"
loop_control:
extended: true
shell> cat task.yml.j2
- {{ item.module }}:
{% for param in item.params %}
{{ param.key }}: {{ param.val }}
{% endfor %}
在循环中,任务“模板”创建带有任务的文件
shell> tree tasks
tasks
├── task-1.yml
└── task-2.yml
0 directories, 2 files
shell> cat tasks/task-1.yml
- debug:
var: inventory_hostname
shell> cat tasks/task-2.yml
- debug:
msg: End of dynamic play
并且剧本中的下一个任务将这些文件包含在循环中。剧本给
shell> ansible-playbook test.yml
PLAY [localhost] ****
TASK [file] ****
ok: [localhost]
TASK [template] ****
changed: [localhost] => (item={'module': 'debug', 'params': [{'key': 'var', 'val': 'inventory_hostname'}]})
changed: [localhost] => (item={'module': 'debug', 'params': [{'key': 'msg', 'val': 'End of dynamic play'}]})
TASK [include_tasks] ****
included: /scratch/tasks/task-1.yml for localhost
included: /scratch/tasks/task-2.yml for localhost
TASK [debug] ****
ok: [localhost] => {
"inventory_hostname": "localhost"
}
TASK [debug] ****
ok: [localhost] => {
"msg": "End of dynamic play"
}
PLAY RECAP ****
localhost: ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
同样可以通过动态创建的剧本来简化。例如,请参阅下面的剧本和模板
shell> cat test.yml
- name: Create playbook from template
hosts: localhost
gather_facts: false
vars:
pb_tasks:
- module: debug
params:
- {key: var, val: inventory_hostname}
- module: debug
params:
- {key: msg, val: End of play}
tasks:
- template:
src: playbook.yml.j2
dest: playbook.yml
shell> cat playbook.yml.j2
- hosts: localhost
gather_facts: false
tasks:
{% for item in pb_tasks %}
- {{ item.module }}:
{% for param in item.params %}
{{ param.key }}: {{ param.val }}
{% endfor %}
{% endfor %}
任务“模板”创建剧本
shell> cat playbook.yml
- hosts: localhost
gather_facts: false
tasks:
- debug:
var: inventory_hostname
- debug:
msg: End of play
然后剧本给出
shell> ansible-playbook playbook.yml
PLAY [localhost] ****
TASK [debug] ****
ok: [localhost] => {
"inventory_hostname": "localhost"
}
TASK [debug] ****
ok: [localhost] => {
"msg": "End of play"
}
PLAY RECAP ****
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
推荐阅读
- c# - HierarchicalDataTemplate 中的 WPF TreeView 过滤器项
- python - 如何在没有错误的情况下标记推文列表?
- wordpress - 如何导出带有帖子信息的 WordPress 评论
- java - 如何使用 apache spark 流和 Java API 从所有人中获取英文推文?
- security - 是否可以通过 XSS 漏洞执行 SQL 注入?
- maven - geb 将测试用例作为参数从 maven 命令行传递
- c++ - 为什么我的 SFML 在将 sf::Texture 作为参数传递给函数时不断崩溃
- multithreading - Java Security API 同步方法导致应用程序线程在高负载时挂起
- r - 使用来自另一个数据表的值更新数据表列
- javascript - 单击按钮时显示 DIV 容器的 Javascript 函数