我正在尝试删除一个与另一个文件不匹配的文件。删除文件后,我想将变量从“ 1”更改为“ 0”,以执行其他处理。但是,当我尝试使用vars
进行更改并运行它时。它不会跳过旧文件并安装新文件,而是会跳过这两个文件。
代码:
# Runs if the file on the target host don't match another remote host file
- name: Remove old tar file
file:
path: /tmp/{{ target_tar_name }}
state: absent
vars:
target_version_matched: "0"
# Below should be hostname with tarfile on it
when:
- target_version_matched == "1"
- tar_version != target_tar_name
# Copies the file from the host to the target machine if nothing exist
- name: Copy Remote-To-Remote (host to target)
synchronize: src=/tmp/{{ tar_version }} dest=/tmp
delegate_to: 10.x.x.x
when: target_version_matched == "0"
错误:
TASK [Remove old tar file] *****************************************************************************************************************************************************************************
skipping: [10.x.x.x]
TASK [Copy Remote-To-Remote (host to target)] **********************************************************************************************************************************************************
skipping: [10.x.x.x]
答案 0 :(得分:0)
在下面使用
# Runs if the file on the target host don't match another remote host file
- name: Remove old tar file
file:
path: /tmp/{{ target_tar_name }}
state: absent
# Below should be hostname with tarfile on it
when:
- target_version_matched == "1"
- tar_version != target_tar_name
register: result
- name: Check if result changes
set_fact:
target_version_matched: "0"
when: result.changed
# Copies the file from the host to the target machine if nothing exist
- name: Copy Remote-To-Remote (host to target)
synchronize: src=/tmp/{{ tar_version }} dest=/tmp
delegate_to: 10.x.x.x
when: target_version_matched == "0"
#when: result.changed # or you can use result directly here, if target_version_matched is not used anywhere
答案 1 :(得分:0)
问题的说明
以下任务将始终被跳过,因为 vars 在何时
之前求值 - name: Remove old tar file
file:
path: /tmp/{{ target_tar_name }}
state: absent
vars:
target_version_matched: "0"
when:
- target_version_matched == "1"
示例
- hosts: localhost
vars:
match: '999'
tasks:
# Always skipped because 'match' is assigned '0' by 'vars' first
- debug:
var: match
vars:
match: '0'
when: match == '1'
# TASK [debug] *********************************************
# skipping: [localhost]
# Always pass because 'match' is assigned '1' by 'vars' first
- debug:
var: match
vars:
match: '1'
when: match == '1'
# TASK [debug] *********************************************
# ok: [localhost] => {
# "match": "1"
# }
# Scope of variables declared inside a task is this task
- debug:
var: match
# TASK [debug] *********************************************
# ok: [localhost] => {
# "match": "999"
# }
# All skipped because 'when' is applied to all tasks first
- block:
- debug:
var: match
- set_fact:
match: '1'
- debug:
var: match
when: match == '1'
# TASK [debug] *********************************************
# skipping: [localhost]
# TASK [set_fact] ******************************************
# skipping: [localhost]
# TASK [debug] *********************************************
# skipping: [localhost]