如何使用操作系统特定的变量运行任务?

时间:2019-05-30 14:09:42

标签: ansible

如何使用文件中特定于操作系统的变量运行以下任务?

  • Debian-6-certificates.yaml
  • Debian-7-certificates.yaml
  • Debian-8-certificates.yaml
  • Debian-9-certificates.yaml

   - name: Copy the proxy CA to /etc/ssl/certs/
      copy:
        src: library/{{ item }}.crt
        dest: /usr/local/share/ca-certificates/{{ item }}.crt
      with_items:
      - "{{ proxy_ca_certificate }}"
    - name: Update system CA certificates
      command: update-ca-certificates --fresh

当前手册:

- hosts: debian_hosts
  gather_facts: yes  # collect info about host incl. ansible_distribution_*
  tasks:
    - include_vars:
        file: "vars/{{ ansible_distribution: Debian }}-{{ ansible_distribution_version: 6 }}-certificates.yml"
    - name: Copy the proxy CA to /etc/ssl/certs/
      copy:
        src: library/{{ item }}.crt
        dest: /usr/local/share/ca-certificates/{{ item }}.crt
      with_items:
        - "{{ proxy_ca_certificate }}"
    - name: Update system CA certificates
      command: update-ca-certificates --fresh

Output:

PLAY [debian_hosts] ********************************************************************************
skipping: no hosts matched

1 个答案:

答案 0 :(得分:0)

找到Debian-6、7、8、9的 ansible_distribution ansible_distribution_version

$ ansible -m setup host | grep ansible_distribution

假设(选择适合您需求的变量)

"ansible_distribution": "Debian"
"ansible_distribution_version": "6"

创建目录 var 并在其中放置带有变量的文件。文件名结合了变量。请参见下面的示例。使名称和变量适合您的需求。

"vars/{{ ansible_distribution }}-{{ ansible_distribution_version }}-certificates.yml"

创建广告资源组 debian_hosts 并运行下面的剧本

- hosts: debian_hosts
  gather_facts: yes  # collect info about host incl. ansible_distribution_*
  tasks:
    - include_vars:
        file: "vars/{{ ansible_distribution }}-{{ ansible_distribution_version }}-certificates.yml"
    - name: Copy the proxy CA to /etc/ssl/certs/
      copy:
        src: library/{{ item }}.crt
        dest: /usr/local/share/ca-certificates/{{ item }}.crt
      with_items:
        - "{{ proxy_ca_certificate }}"
    - name: Update system CA certificates
      command: update-ca-certificates --fresh

(未经测试)