Ansible

include_vars 它的實際含義

  • July 18, 2017

我基本上想從我的遠端主機獲取動態資訊,無論它是 redhat 還是 debian,並相應地執行特定文件,該文件將安裝基於 os 風格的 http 包。

[root@ansi1 ansible]# cat include.yml
---
- hosts: all
  tasks:
  - name: Getting os info
    include_vars: "{{ ansible_os_family }}.yml"

  - include: setup-RedHat.yml
    when: ansible_os_family == 'RedHat'
[root@ansi1 ansible]#
[root@ansi1 ansible]#
[root@ansi1 ansible]#
[root@ansi1 ansible]# cat setup-RedHat.yml
---
- hosts: all
  tasks:
  - name: htttp install
    yum: name=httpd state=present

ansible-playbook include.yml

錯誤:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/etc/ansible/setup-RedHat.yml': line 2, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: all
  ^ here


The error appears to have been in '/etc/ansible/setup-RedHat.yml': line 2, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: all
  ^ here

當你include在一個部分中使用模組時tasks,你不能包含一個劇本,你只能包含一個任務列表。這意味著您的文件setup-RedHat.yml應僅包含以下內容:

- name: htttp install
 yum: name=httpd state=present

- name: more tasks...

include_vars指令用於從作為值的文件中獲取目前劇本的 Ansible 變數。在這種情況下,變數(可能聲明在和)從文件繼承"{{ ansible_os_family }}.yml"

ansible_os_family是 Ansible 從遠端系統自動收集的事實,它解析為上下文中分佈的父分佈(如果有),如果沒有父分佈,則解析為它本身。因此,例如,如果您在 Debian 衍生產品上執行它,則要查找的文件名將變為Debian.yml.

引用自:https://unix.stackexchange.com/questions/370108