linux 无法使用when子句优化ansible代码

k5hmc34c  于 2023-05-22  发布在  Linux
关注(0)|答案(1)|浏览(137)

我想优化下面的yaml成单块

- name: Download Amazon CloudWatch Agent for Debian
  when: ansible_facts['ansible_distribution'] == 'Debian'
  get_url:
    url: "https://s3.amazonaws.com/amazoncloudwatch-agent/debian/amd64/latest/amazon-cloudwatch-agent.deb"
    dest: "/tmp/temp_files/amazon-cloudwatch-agent.deb"

- name: Download Amazon CloudWatch Agent for RHEL
  when: ansible_facts['ansible_distribution'] == 'RedHat'
  get_url:
    url: "https://s3.amazonaws.com/amazoncloudwatch-agent/redhat/amd64/latest/amazon-cloudwatch-agent.rpm"
    dest: "/tmp/temp_files/amazon-cloudwatch-agent.rpm"```
m1m5dgzv

m1m5dgzv1#

下面的剧本展示了如何为各种发行版创建参数

- hosts: all

  vars:

    options:
      Debian:
        agent: debian
        ext: deb
      RedHat:
        agent: redhat
        ext: rpm

  tasks:

    - debug:
        var: ansible_distribution
    - assert:
        that: ansible_distribution in options
        fail_msg: "{{ ansible_distribution }} not supported."

    - debug:
        msg: |
          url: "https://s3.amazonaws.com/amazoncloudwatch-agent/{{ agent }}/amd64/latest/amazon-cloudwatch-agent.{{ ext }}"
          dest: "/tmp/temp_files/amazon-cloudwatch-agent.{{ ext }}"
      vars:
        agent: "{{ options[ansible_distribution].agent }}"
        ext: "{{ options[ansible_distribution].ext }}"

相关问题