条件与变量的比较

huangapple go评论69阅读模式
英文:

Conditionals against variables

问题

我正在构建一个快照管理的操作手册,用于在创建快照之前检查是否存在快照。我在测试条件语句时遇到了问题,当操作手册找到现有的快照时。以下是示例代码:

---
- name: Test Snapshot
  hosts: axwayT
  gather_facts: false

  vars_files:
    - vault/creds.yml

  vars:
    mail_body_file: "/tmp/ansible_mail"

  pre_tasks:

    - name: Delete mail body file
      file:
        state: absent
        path: "{{ mail_body_file }}"
      delegate_to: localhost
      run_once: true

    - name: Create mail body file
      file:
        state: touch
        path: "{{ mail_body_file }}"
      delegate_to: localhost
      run_once: true

  tasks:

    - name: find guest's folder using name
      vmware_guest_find:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        name: "{{ vcenter_hostgroup_name }}"
        datacenter: "{{ datacenter_name }}"
      register: vm_folder
      delegate_to: localhost

    - name: Check for existing snapshots
      vmware_guest:
         hostname: "{{ vcenter_hostname }}"
         username: "{{ vcenter_username }}"
         password: "{{ vcenter_password }}"
         validate_certs: no
         name: "{{ vcenter_hostgroup_name }}"
         datacenter: "{{ datacenter_name }}"
         folder: "{{vm_folder.folders[0]}}"
         name: "{{ vcenter_hostgroup_name }}"
         state: present
      register: vm_state
      delegate_to: localhost

    - name: debug
      debug:
        var: vm_state.instance.current_snapshot

    - name: Test
      shell: echo "Found!"
      when: vm_state.instance.current_snapshot != ""

我正在使用两台服务器的清单进行测试,没有特殊要求。以下是我想要根据条件语句使用的输出:

TASK [debug] ************************************************************************************************
ok: [swipe901.test.com] => {
    "vm_state.instance.current_snapshot": ""
}
ok: [swipe902.test.com] => {
    "vm_state.instance.current_snapshot": {
        "creation_time": "2023-02-07T21:14:06.812901+00:00",
        "description": "",
        "id": 13,
        "name": "VM Snapshot 2%2f7%2f2023, 3:14:05 PM",
        "state": "poweredOn"
    }
}

我尝试了两个条件语句:

when: vm_state.instance.current_snapshot != ""
when: vm_state.instance.current_snapshot is defined

如果vm_state.instance.current_snapshot是空的话,我的逻辑是完全错的,我意识到了我对编程逻辑的限制,但我计划很快修复。我的逻辑计划是,如果vm_state.instance.current_snapshot是空的,跳过操作手册中的一个步骤。应该如何处理这种情况?

如果你想在vm_state.instance.current_snapshot为空时跳过操作手册的一个步骤,你可以使用when语句来实现。你已经尝试了条件when: vm_state.instance.current_snapshot != "",但这只会在vm_state.instance.current_snapshot不为空时才执行操作。如果你希望在vm_state.instance.current_snapshot为空时跳过操作,可以使用以下条件:

when: vm_state.instance.current_snapshot != ""

这将确保操作只在vm_state.instance.current_snapshot不为空时执行。如果vm_state.instance.current_snapshot为空,操作将被跳过。

英文:

I am building a snapshot management playbook and that checks for an existing snap before taking a snap. I am having trouble testing a conditional when the playbook finds an existing snap. Here is a sample code:

---
- name: Test Snapshot
  hosts: axwayT
  gather_facts: false
  
  vars_files:
    - vault/creds.yml

  vars:
    mail_body_file: "/tmp/ansible_mail"

  pre_tasks:

    - name: Delete mail body file
      file:
        state: absent
        path: "{{ mail_body_file }}"
      delegate_to: localhost
      run_once: true

    - name: Create mail body file
      file:
        state: touch
        path: "{{ mail_body_file }}"
      delegate_to: localhost
      run_once: true
    
  tasks:
    
    - name: find guest's folder using name
      vmware_guest_find:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        name: "{{ vcenter_hostgroup_name }}"
        datacenter: "{{ datacenter_name }}"
      register: vm_folder
      delegate_to: localhost
    
    - name: Check for existing snapshots
      vmware_guest:
         hostname: "{{ vcenter_hostname }}"
         username: "{{ vcenter_username }}"
         password: "{{ vcenter_password }}"
         validate_certs: no
         name: "{{ vcenter_hostgroup_name }}"
         datacenter: "{{ datacenter_name }}"
         folder: "{{vm_folder.folders[0]}}"
         name: "{{ vcenter_hostgroup_name }}"
         state: present
      register: vm_state
      delegate_to: localhost

    - name: debug
      debug:
        var: vm_state.instance.current_snapshot

    - name: Test
      shell: echo "Found!"
      when: vm_state.instance.current_snapshot !=""

I am working with an inventory of two servers for testing, nothing special. Here is the ouput I want to use a conditional against:

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [swipe901.test.com] => {
    "vm_state.instance.current_snapshot": ""
}
ok: [swipe902.test.com] => {
    "vm_state.instance.current_snapshot": {
        "creation_time": "2023-02-07T21:14:06.812901+00:00",
        "description": "",
        "id": 13,
        "name": "VM Snapshot 2%2f7%2f2023, 3:14:05 PM",
        "state": "poweredOn"
    }
}

I tried two when statements:

when: vm_state.instance.current_snapshot =""
when: vm_state.instance.current_snapshot is defined

My logic is way off and I am seeing my own limitations with programing logic, which I plan to fix soon.

My plan for the logic is to, skip a step, in my playbook if vm_state.instance.current_snapshot is "". How would this be handled?

答案1

得分: 1

A: 例如,下面的Playbook:

shell> cat pb.yml
- name: 如果current_snapshot中有任何内容,则评估为false
  hosts: localhost
  tasks:
    - debug:
        msg: 变量current_snapshot要么为空,要么不存在。
      when: current_snapshot|d('', true)|length == 0

给出了以下结果:

shell> ansible-playbook pb.yml

PLAY [如果current_snapshot中有任何内容,则评估为false] *************************************************************

TASK [debug] *************************************************************************************************************************
ok: [localhost] => 
  msg: 变量current_snapshot要么为空,要么不存在。

PLAY RECAP ***************************************************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell> ansible-playbook pb.yml -e current_snapshot=''

PLAY [如果current_snapshot中有任何内容,则评估为false] *************************************************************

TASK [debug] *************************************************************************************************************************
ok: [localhost] => 
  msg: 变量current_snapshot要么为空,要么不存在。

PLAY RECAP ***************************************************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell> ansible-playbook pb.yml -e current_snapshot='anything'

PLAY [如果current_snapshot中有任何内容,则评估为false] *************************************************************

TASK [debug] *************************************************************************************************************************
skipping: [localhost]

PLAY RECAP ***************************************************************************************************************************
localhost: ok=0    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

您还可以使用assert。例如,

shell> cat pb.yml
- name: 如果current_snapshot中有任何内容,则评估为false
  hosts: localhost
  tasks:
    - assert:
        that: current_snapshot|d('', true)|length == 0
        success_msg: 变量current_snapshot要么为空,要么不存在。
        fail_msg: 变量current_snapshot已被填充。
英文:

Q: "I would like the condition to evaluate false if there is anything populated in current_snapshot."

A: For example, the playbook

shell> cat pb.yml
- name: Evaluate false if there is anything populated in current_snapshot
  hosts: localhost
  tasks:
    - debug:
        msg: The variable current_snapshot is either empty or does not exist.
      when: current_snapshot|d('', true)|length == 0

gives

<sup>

shell&gt; ansible-playbook pb.yml

PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************

TASK [debug] *************************************************************************************************************************
ok: [localhost] =&gt; 
  msg: The variable current_snapshot is either empty or does not exist.

PLAY RECAP ***************************************************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell&gt; ansible-playbook pb.yml -e current_snapshot=&#39;&#39;

PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************

TASK [debug] *************************************************************************************************************************
ok: [localhost] =&gt; 
  msg: The variable current_snapshot is either empty or does not exist.

PLAY RECAP ***************************************************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell&gt; ansible-playbook pb.yml -e current_snapshot=&#39;anything&#39;

PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************

TASK [debug] *************************************************************************************************************************
skipping: [localhost]

PLAY RECAP ***************************************************************************************************************************
localhost: ok=0    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

</sup>

<hr>

You can also use assert. For example,

shell&gt; cat pb.yml
- name: Evaluate false if there is anything populated in current_snapshot
  hosts: localhost
  tasks:
    - assert:
        that: current_snapshot|d(&#39;&#39;, true)|length == 0
        success_msg: The variable current_snapshot is either empty or does not exist.
        fail_msg: The variable current_snapshot is populated.

huangapple
  • 本文由 发表于 2023年2月10日 05:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定