我正在尝试使用Ansible与数据库一起设置Apache虚拟主机,但并非所有的Vhost都具有数据库(1个或更多)。
我已经尝试了很多,也尝试从这里进行适应,但是没有任何效果。我花了好几个小时反复尝试并得出以下结论:
我的host_vars
(节选):
web_vhosts:
- vhost:
name: domain1.tld
enabled: true
serveradmin_email: [email protected]
https: true
redirect_to_https: true
dns_a_record: 1.2.3.4
update_dns: false
- vhost:
name: domain2.tld
enabled: true
serveradmin_email: [email protected]
https: true
redirect_to_https: true
dns_a_record: 1.2.3.4
update_dns: false
mysql:
- name: wordpress1
user: myuser
password: secret
这是我有多远:
- name: Ensure databases
mysql_db:
name: "{{ item.1.name }}"
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
when: item.0.mysql is defined
with_subelements:
- "{{ web_vhosts }}"
- "mysql"
哪些错误:
fatal: [examplehost]: FAILED! => {"msg": "could not find 'mysql' key in iterated item '{'vhost': {'name': 'domain1.tld', 'enabled': True, 'serveradmin_email': '[email protected]', 'https': True, 'redirect_to_https': True, 'dns_a_record': '1.2.3.4', 'update_dns': False}}'"}
谁能帮助我了解问题所在?
您的变量web_vhosts
是一个由字典组成的列表,其中第一个元素是vhost
。您需要找到vhost.mysql
而不是的子元素mysql
。这应该可以解决您的问题:
- name: Ensure databases
mysql_db:
name: "{{ item.1.name }}"
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
loop: "{{ web_vhosts | subelements('vhost.mysql', skip_missing=True) }}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句