如何使用Ansible有条件地复制文件?

请参考下面的配置,其想法是,如果找到配置文件,请对配置进行部署

我尝试了很多表达这种想法的方法,尤其是在最后三行中,但是我无法以有意义的方式将将stat循环的结果与我的部署循环结合在一起

我有以下结构列表:

config_vars:
  - war_name: TestNode_app1.war
    standalone_name: jboss_standalone.xml
    jboss_service_name: jboss_app1
    jboss_folder: jboss_app1
  - war_name: TestNode_app2.war
    standalone_name: jboss_app2_standalone.xml
    jboss_service_name: jboss_app2
    jboss_folder: jboss_app2

在我拥有的剧本中

...
...

tasks:
  - name: check if war exists
    stat:
      path: files/wars/{{ item.war_name }}
    register: war_file
    loop: "{{ config_vars }}"

  - name: Deploy config file
    copy: src=files/wars/{{ item.war_name }} dest=/opt/{{ item.jboss_folder }}/standalone/deployments/{{ item.war_name }}
    when: item.stat.exists
    loop: "{{ config_vars }}"
    with_items: war_file.results

预期结果:对于存在的war文件,deploy循环应将其拾取并进行复制。如果不存在,则不执行任何操作 实际结果:各种。对于上面的配置,我有:

ERROR! duplicate loop in task: items
The offending line appears to be:


    - name: deploy WAR file
      ^ here
why111333 回答:如何使用Ansible有条件地复制文件?

这可以通过applyMove [(1,2),(2,3)] gameStateloop(替换旧的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 taskloopwith_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

大家都在问