这可以通过applyMove [(1,2),(2,3)] gameState
和loop
(替换旧的zip
循环)来实现。
with_together
这将产生相当冗长的输出,因此,如果您希望将其最小化,请添加以下内容:
- name: Deploy config file
copy: src=files/wars/{{ item.0.war_name }} dest=/opt/{{ item.0.jboss_folder }}/standalone/deployments/{{ item.0.war_name }}
when: item.1.stat.exists
loop: "{{ config_vars | zip(war_file.results) | list }}"
,
实际上,您确实拥有duplicate loop in task
:loop
和with_items
都是循环结构。
这是一本简单的剧本,以演示如何实现这一目标。您甚至不需要使用Matt P提供的with_together
构造,但是您可能需要这样做以提高可读性。
- hosts: localhost
connection: local
gather_facts: false
vars:
config_vars:
- filepath: ./f1.txt
other: 'here'
- filepath: ./f2.txt
other: 'also here'
tasks:
- name: check if war exists
stat:
path: "{{ item.filepath }}"
register: war_file
loop: "{{ config_vars }}"
- debug:
msg: "Copying {{ item.item }}"
when: item.stat.exists
loop: "{{ war_file.results }}"
结果(仅存在f2.txt):
PLAY [localhost] *****************************************************************************************************************************************************************
TASK [check if war exists] *******************************************************************************************************************************************************
ok: [localhost] => (item={'filepath': './f1.txt','other': 'here'})
ok: [localhost] => (item={'filepath': './f2.txt','other': 'also here'})
TASK [debug] *********************************************************************************************************************************************************************
skipping: [localhost] => (item={'changed': False,'stat': {'exists': False},'invocation': {'module_args': {'path': './f1.txt','follow': False,'get_checksum': True,'get_mime': True,'get_attributes': True,'checksum_algorithm': 'sha1','get_md5': None}},'failed': False,'item': {'filepath': './f1.txt','other': 'here'},'ansible_loop_var': 'item'})
ok: [localhost] => (item={'changed': False,'stat': {'exists': True,'path': './f2.txt','mode': '0644','isdir': False,'ischr': False,'isblk': False,'isreg': True,'isfifo': False,'islnk': False,'issock': False,'uid': 501,'gid': 20,'size': 0,'inode': 8626896681,'dev': 16777220,'nlink': 1,'atime': 1572930945.6961422,'mtime': 1572930850.452235,'ctime': 1572930850.452235,'wusr': True,'rusr': True,'xusr': False,'wgrp': False,'rgrp': True,'xgrp': False,'woth': False,'roth': True,'xoth': False,'isuid': False,'isgid': False,'blocks': 0,'block_size': 4096,'device_type': 0,'flags': 0,'generation': 0,'birthtime': 1572930850.452235,'readable': True,'writeable': True,'executable': False,'pw_name': 'pwd','gr_name': 'staff','checksum': 'da39a3ee5e6b4b0d3255bfef95601890afd80709','mimetype': 'unknown','charset': 'unknown','version': None,'attributes': [],'attr_flags': ''},'invocation': {'module_args': {'path': './f2.txt','item': {'filepath': './f2.txt','other': 'also here'},'ansible_loop_var': 'item'}) => {
"msg": "Copying {'filepath': './f2.txt','other': 'also here'}"
}
PLAY RECAP ***********************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
请注意,item
的每个字典元素中都提供了war_file.results
字典元素。 item
包含stat
循环中使用的项目。
以下是有关循环的更多信息:https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
,
感谢你们俩。下面的代码现在运行良好:
tasks:
- name: check if war exists
local_action: stat path=files/wars/{{ item.war_name }}
register: war_file
loop: "{{ config_vars }}"
- name: deploy war file
become: yes
become_user: jboss-as
copy: |
src=files/wars/{{ item.item.war_name }}
dest=/opt/{{ item.item.jboss_folder }}/standalone/deployments/{{ item.item.war_name }}
when: item.stat.exists
loop: "{{ war_file.results }}"
本文链接:https://www.f2er.com/3162260.html