Host_varsがAnsibleでは機能しません。

Host_varsがAnsibleでは機能しません。

私のansibleファイルを使用してリモートホストにパッケージをインストールする必要があります。ファイルがあるディレクトリから名前で変数を取得しようhost_varsとしています。これはリモートコンピュータのホスト名です。packagehost_varsansi2

---
 - hosts: all
   become: yes
   ignore_errors: yes
   gather_facts: no
   tasks:

   - name: install vim
     yum: name={{ package }} state=present

ディレクトリ構造:

[root@ansi1 ansible]# pwd
/etc/ansible
[root@ansi1 ansible]#
[root@ansi1 ansible]# ls -l ansi2/host_vars/
total 4
-rw-r--r-- 1 root root 19 Jun 13 08:15 ansi2
[root@ansi1 ansible]#


[root@ansi1 ansible]# cat ansi2/host_vars/ansi2
---
package: "vim"

間違い:

TASK [install vim] *************************************************************
task path: /etc/ansible/service.yml:7
fatal: [ansi2.example.com]: FAILED! => {
    "failed": true,
    "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'package' is undefined\n\nThe error appears to have been in '/etc/ansible/service.yml': line 7, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n   tasks:\n   - name: install vim\n     ^ here\n"
}
...ignoring

ベストアンサー1

あなたの場合、ディレクトリの最上位レベルには次のファイルとディレクトリを含める必要があるとします。

/etc
+-- ansible/
    +-- host_vars/
        +-- ansi2.example.com.yml
    +-- service.yml

おすすめ記事