具有 when 条件的 Ansible Stat 函数

Ansible Stat function with when condition

我有这样的子剧本

{{

- vars:
    appHome: /opt/app
    aliasName: webbuild

- stat:
    path: "{{ appHome }}/{{ aliasName }}"
  register: folder_stat

- name: Print vars
  debug:
    msg: >
      {{ appHome }}/{{ aliasName }}
      {{ folder_stat }}

- set_fact:
    oldVersion: "{{ folder_stat.stat.lnk_source }}"
  when: folder_stat.stat.islnk is defined and folder_stat.stat.islnk == true

- set_fact:
    oldVersion: "{{ appHome }}/{{ aliasName }}"
  when: folder_stat.stat.islnk is defined and folder_stat.stat.islnk == false

- name: Print oldVersion
  debug:
    msg: "{{ oldVersion }}"

}}

当我运行这本书玩的时候,它return:

"TASK [backupService : set_fact] **********************************************************************************************************************
task path: /opt/ansible/bullion/ansible/playbooks/roles/backupService/tasks/app01.yml:16
skipping: [STG-app-1] => {
    "changed": false, 
    "skip_reason": "Conditional result was False"
}

"

谁能帮我解释一下:

非常感谢你们!

您可以用一个任务替换 2 个任务:

- set_fact:
    oldVersion: "{{ folder_stat.stat.lnk_source }}"
  when: folder_stat.stat.islnk is defined and folder_stat.stat.islnk == true

- set_fact:
    oldVersion: "{{ appHome }}/{{ aliasName }}"
  when: folder_stat.stat.islnk is defined and folder_stat.stat.islnk == false

替换为:

- set_fact:
    oldVersion: "{{ ptrue if folder_stat.stat.islnk else pfalse }}"
  when: folder_stat.stat.islnk is defined
  vars:
    ptrue: "{{ folder_stat.stat.lnk_source }}"
    pfalse: "{{ appHome }}/{{ aliasName }}"