【发布时间】:2016-04-10 01:35:07
【问题描述】:
我一直试图在 Ansible 中引用一个变量,但每次都收到错误消息。
yaml var 文件有:
nodes:
barni:
- { name: 'LoginGraceTime', value: '2m'}
- { name: 'MaxSessions', value: '6'}
- { name: 'ChallengeResponseAuthentication', value: 'yes'}
其中 barni 是“主机名 -s”。我需要 sshd_config.j2 模板中的动态内容来匹配变量中的主机名。如果我指定 node.barni.sshdextra,模板可以正常工作,但我需要将“barni”动态替换为服务器名称的短名称。
{% for item in node.barni.sshdextra %}
{{ item.name }} {{ item.value }}
{% endfor %}
在任务中,我可以使用
hostname_mounts: "{{hostname_shortname.replace('-','')}
hostname_shortname: "{{inventory_hostname.split('.')[0]}}在变量def文件中
- name: nfs - edit - remove nfs lines from /etc/fstab
command: sed -i '/nfs/d' /etc/fstab
ignore_errors: yes
when: "'{{inventory_hostname}}'.startswith('{{hostname_mounts}}')"
tags: [ nfs ]
【问题讨论】: