【问题标题】:How to execute different shell commands dependent on different values from the same variable parameter on ansible?如何根据ansible上相同变量参数的不同值执行不同的shell命令?
【发布时间】:2019-08-11 00:37:00
【问题描述】:

这里是 Ansible 新手:

我目前正在创建一个旨在启动、停止和重新启动 vm 的 ansible playbook。我需要根据来自同一输入参数变量的特定值执行不同的 shell 命令。当我尝试停止 VM 时,我假设 playbook 在运行 Start VM 任务时已停止。我将如何解决这个问题?

#Start the VM
- name: Start the VM
  shell: virsh start "{{ vmname }}"
  register: vmstate_start
  when: state == "start"

- debug:
    msg: "{{ vmstate_start.stdout  }}"

#Stop the VM
- name: Stop the VM
  shell: virsh shutdown "{{ vmname }}"
  register: vmstate_stop
  when: state == "stop"

- debug:
    msg: "{{ vmstate_stop.stdout }}"

#Restart the VM
- name: Restart the VM
  shell: virsh reboot "{{ vmname }}"
  register: vmstate_reboot
  when: state == "reboot"

- debug:
    msg: "{{ vmstate_reboot.stdout }}"

致命:[111.111.1.1]:失败! => {"msg": "该任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute 'stdout'\n\n错误似乎在 '/root//ansible/kvm- vm-start-stop/kvm-vm-start-top.yaml':第 65 行,第 7 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\n \n\n - 调试:\n ^ 此处\n"}

【问题讨论】:

  • 这与 Bash 有什么关系?这是您的 YAML 中的问题,不是吗?

标签: bash shell ansible


【解决方案1】:

当一个任务被跳过时,它不会像没有被跳过时那样改变register:-ed 变量。值得庆幸的是,ansible 总是在已注册任务的 skipped 字段中包含任务是否被跳过,或者可以使用 is skipped 过滤器。因此,您可以使用 if 保护表达式,或使用 default 过滤器,或者同样跳过 debug: 任务以避免错误:

- name: run the debug, but conditionally display the stdout
  debug:
    msg: '{{ "NO OUTPUT DUE TO SKIP" if vmstate_start.skipped else vmstate_start.stdout }}'

- name: or you can use the "default" filter
  debug:
    msg: '{{ (vmstate_start.stdout)|default("") }}'

- name: or, skip the debug task
  debug:
    msg: vmstate_start.stdout
  when: vmstate_start is not skipped

【讨论】:

    【解决方案2】:

    可以在命令中包含变量

      - name: Manage the VM
        shell: "virsh {{ state }} {{ vmname }}"
        register: vmstate_result
      - debug:
          msg: "{{ vmstate_result.stdout }}"
    

    如果state 变量的值来自不可变列表 [start, stop, reboot],则可以创建转换表

    vars:
      vm_command:
        start: start
        stop: shutdown
        reboot: reboot
    tasks:
      - name: Manage the VM
        shell: "virsh {{ vm_command[state] }} {{ vmname }}"
        register: vmstate_result
      - debug:
          msg: "{{ vmstate_result.stdout }}"
    

    除了上述任务之外,还可以让游戏更加健壮并测试state 变量

    vars:
      vm_commands: [start, stop, reboot]
      vm_command:
        start: start
        stop: shutdown
        reboot: reboot
    tasks:
      - block:
          - name: Manage the VM
            shell: "virsh {{ vm_command[state] }} {{ vmname }}"
            register: vmstate_result
          - debug:
              msg: "{{ vmstate_result.stdout }}"
        when: state in vm_commands
      - debug:
          msg: "[ERR] unkown state: {{ state }} Valid states: {{ vm_commands }}"
        when: state not in vm_commands
    

    (未测试)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多