我不能完全理解如何告诉Ansible做以下事情:
当我运行playbook时,我可以在输出中看到字典中的每个项目都在使用。完成后,该文件包含最后一项。
此时我有两个选择:
选项1看起来像这样,但由于任务中的额外命令
,它失败了var l = document.getElementById('l');
var log = function (m) {
var i = document.createElement('li');
i.innerText = new Date().toISOString()+' '+m;
l.appendChild(i);
}
log('opening socket.io connection');
var s = io();
s.on('connect_error', function (m) { log("error"); });
s.on('connect', function (m) { log("socket.io connection open"); });
s.on('message', function (m) { log(m); });
我不知道如何解决选项2。
这些是我目前拥有的将每次迭代写入目标服务器上的文件的文件。不是我需要的,但它很接近。
任务/ main.yml
private void verifyLocationServices() {
final LocationManager manager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This app requires that location services be enabled. Please enable location in settings.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
乏/ main.yml
---
- name: Insert web app users
template:
src: sql_template.j2
dest: /tmp/sql_template
with_dict: users
command: cat /tmp/sql_template
模板/ sql_template.j2
---
- name: Insert web app users
template:
src: sql_template.j2
dest: /tmp/sql_template
with_dict: users
答案 0 :(得分:0)
解决方案是在模板内部进行循环。文件vars / main.yml中定义的变量在模板中自动可用。文件现在看起来像这样,它生成整个文件:
任务/ main.yml
---
- name: Insert web app users
template:
src: sql_template.j2
dest: /tmp/sql_template
模板/ sql_template.j2
{% for item in users %}
INSERT INTO cbcommon:users (user_nbr, user_name, user_desc, password, user_timeout, suspended, change_password, no_password_expiry, display_name, email) VALUES ({{users[item].user_nbr}}, '{{users[item].user_name}}', '{{users[item].user_desc}}', '{{ users[item].password}}', 60, 0, 0, 1, '{{ users[item].display_name}}', '{{ users[item].email}}' );
INSERT INTO cbcommon:users_orgs (user_nbr, org_nbr, class_nbr, primary_org) VALUES({{users[item].user_nbr}}, 1000, -1, 1);
INSERT INTO cbcommon:users_orgs (user_nbr, org_nbr, class_nbr, primary_org) SELECT {{ users[item].user_nbr}}, org_nbr, -1, 0 FROM orgs WHERE org_nbr != 1000;
{% endfor %}