我必须从 mysql 查询中获取两个数组,并且我想进行主机比较。例如 mysql 结果的结果类似于 list1 & list2。
- name:
set_fact:
list1:
- host: '[\"X.XXX.XX.XXX\"]'
username: username1
- host: '[\"X.XXX.XX.XXX\"]'
username: username2
- host: '[\"X.XXX.XX.XXX\"]'
username: username3
- host:
- '[\"XX.XXX.XX.XXX\"]'
- '[\"XX.X.X.XXX\"]'
username: username4
- host: '[\"XX.X.X.XXX\"]'
username: username5
- host: '[\"localhost\"]'
username: username6
- host: '[\"XX.XXX.XX.XXX\"]'
username: username7
list2:
- host: 'X.XXX.XX.XXX'
username: username1
- host: 'localhost'
username: username2
- host: 'XX.XXX.XX.XXX'
username: username3
- host: 'localhost'
username: username4
- host: 'XX.X.X.XXX'
username: username5
tags: [diff]
如您所见,来自 mysql 查询的 list2 发回了带有一些不必要字符的主机,我想删除它们,因为在进行比较时,条件为假。
- name: Comparison of two lists
debug:
msg: "{{ item.0.username }}({{ item.0.host }}) == {{ item.1.username }}({{ item.1.host }}) {{ item.0.username == item.1.username }}"
loop: "{{ list1 | product(list2) | list }}"
when:
- "{{ item.0.host == item.1.host }} == True"
register: output
tags: [diff]
这是 Ansible 的输出:
skipping: [staging1-sql0] => (item=[{'host': '[\\"X.XXX.XX.XXX\\"]', 'username': 'username1'}, {'host': 'X.XXX.XX.XXX', 'username': 'username1'}])
skipping: [staging1-sql0] => (item=[{'host': '[\\"X.XXX.XX.XXX\\"]', 'username': 'username2'}, {'host': 'localhost', 'username': 'username2'}])
skipping: [staging1-sql0] => (item=[{'host': '[\\"X.XXX.XX.XXX\\"]', 'username': 'username3'}, {'host': 'XX.XXX.XX.XXX', 'username': 'username3'}])
我怎样才能解决这个问题 ?