用于检查 Linux 发行版的 Ansible 手册

问题描述 投票:0回答:4

如果系统安装了 Oracle Linux,如何检查已安装的操作系统并继续下载文件?

这是我到目前为止所拥有的:

- hosts: all
  become: true
  gather_facts: true
  tasks:
    - name: Check if oracle linux is installed
      shell: |
        cat: /etc/system-release
      register: os_name
      ignore_errors: yes
    - debug:
      msg: "{{os_name.stdout}}"```
ansible
4个回答
8
投票
- hosts: all
  become: yes
  gather_facts: true
  tasks:     

     - name: downloading file if Oracle Linux is there
       get_url:
              url: #url of the file to download 
              dest: #path where you want to store it eg. /etc/downloaded-file
              mode: '0600' #permissions to be given to the file
       when: ansible_facts['distribution'] == "OracleLinux" 

您可能想阅读此参考资料。
1. 从系统中发现变量:事实
2. 从 HTTP、HTTPS 或 FTP 下载文件到节点


4
投票

您应该使用 ansible

gather_facts: yes
输出,以便您的 ansible 剧本在所有平台上通用,否则阅读
/etc/system-release
将无法在所有平台上工作。 参考

- hosts: all
  become: true
  gather_facts: yes

  tasks:
    - name: Distribution
      debug: msg="{{ ansible_distribution }}"

2
投票

当:ansible_os_family ==“RedHat”

  ---
    - name: install apache
      hosts: all
      become: yes
    
      vars:
        source_file: ./html-samples/site20/
        destin_file: /var/www/html
    
      tasks:
      - name: check OS version
        debug: var=ansible_os_family
    
    
      - block:     # for redhat
    
         - name: install apache web server for RedHat
           yum: name=httpd state=latest
    
         - name: copy web-site files to Apache web server
           copy: src={{ source_file }} dest={{ destin_file }} mode=0555
           notify: Restart Apache Restart Apache RedHat
    
         - name: start Apache and enable it on the boot for RedHat
           service: name=httpd state=started enabled=yes
    
        when: ansible_os_family == "RedHat"

0
投票

所有其他答案都是正确的。 如果由于某些原因(例如,Python 在您的目标主机上不可用),您无法/不想收集事实,这是丑陋的方法:

- hosts: whatever
  gather_facts: no
  tasks:
    - name: Register the system release
      raw: cat /etc/system-release
      register: system_release
      changed_when: no
      check_mode: no

    - name: Do something for Oracle systems
      ...
      when: "{{ system_release.stdout|trim is match('.*Oracle Linux.*') }}"
© www.soinside.com 2019 - 2024. All rights reserved.