Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible 角色内环境变量的可用资源清册_Ansible_Ansible Inventory_Ansible Role - Fatal编程技术网

Ansible 角色内环境变量的可用资源清册

Ansible 角色内环境变量的可用资源清册,ansible,ansible-inventory,ansible-role,Ansible,Ansible Inventory,Ansible Role,我得到了一个我想要执行psql脚本的Ansible角色。该角色的名称为“db_scripts”,我将在下面列出各个目录/文件的内容 db_脚本/任务/mail.yml - name: "Run the SQLS" command: "psql -f \"{{ script }}\"/files/my_sql_script.sql" environment: PGDATABASE: "{{ db_name }}" PGUSER: "{{ db_user }}"

我得到了一个我想要执行psql脚本的Ansible角色。该角色的名称为“db_scripts”,我将在下面列出各个目录/文件的内容

db_脚本/任务/mail.yml

-
  name: "Run the SQLS"
  command: "psql -f \"{{ script }}\"/files/my_sql_script.sql"
  environment:
    PGDATABASE: "{{ db_name }}"
    PGUSER: "{{ db_user }}"
    PGHOST: "{{ db_host }}"
    PGPORT: "{{ db_port }}"
  delegate_to: localhost
insert into employees(name, contact) values (Jack, 5555555);
script: "{{ role_path }}"
- name: Run sql
  hosts: dbservers
  roles:
    - db_scripts
db\u scripts/files/my\u sql\u script.yml

-
  name: "Run the SQLS"
  command: "psql -f \"{{ script }}\"/files/my_sql_script.sql"
  environment:
    PGDATABASE: "{{ db_name }}"
    PGUSER: "{{ db_user }}"
    PGHOST: "{{ db_host }}"
    PGPORT: "{{ db_port }}"
  delegate_to: localhost
insert into employees(name, contact) values (Jack, 5555555);
script: "{{ role_path }}"
- name: Run sql
  hosts: dbservers
  roles:
    - db_scripts
db\u脚本/default/main.yml

-
  name: "Run the SQLS"
  command: "psql -f \"{{ script }}\"/files/my_sql_script.sql"
  environment:
    PGDATABASE: "{{ db_name }}"
    PGUSER: "{{ db_user }}"
    PGHOST: "{{ db_host }}"
    PGPORT: "{{ db_port }}"
  delegate_to: localhost
insert into employees(name, contact) values (Jack, 5555555);
script: "{{ role_path }}"
- name: Run sql
  hosts: dbservers
  roles:
    - db_scripts
/app/production/inventory

[dbservers]
db_host=192.168.1.100 db_name=mycompany db_user=abc db_port=3308
[dbservers]
192.168.1.100 db_name=mycompany db_user=abc db_port=3308
/app/playbooks/run_-sqls.yml

-
  name: "Run the SQLS"
  command: "psql -f \"{{ script }}\"/files/my_sql_script.sql"
  environment:
    PGDATABASE: "{{ db_name }}"
    PGUSER: "{{ db_user }}"
    PGHOST: "{{ db_host }}"
    PGPORT: "{{ db_port }}"
  delegate_to: localhost
insert into employees(name, contact) values (Jack, 5555555);
script: "{{ role_path }}"
- name: Run sql
  hosts: dbservers
  roles:
    - db_scripts
我运行剧本“run_sqls”,如下所示

ansible-playbook run_sqls.yml -i inventories/app/production/inventory
我想了解的是,我应该如何为以下环境变量指定值,以便它们可以是动态的,并且可以被其他环境(例如:开发等)的主机使用

PGDATABASE: "{{ db_name }}"
PGUSER: "{{ db_user }}"
PGHOST: "{{ db_host }}"
PGPORT: "{{ db_port }}"
在各自的清单文件中指定它们似乎不起作用,对于“未定义的变量”,playbook错误是有意义的。以下是我的尝试。 /app/production/inventory

[dbservers]
db_host=192.168.1.100 db_name=mycompany db_user=abc db_port=3308
[dbservers]
192.168.1.100 db_name=mycompany db_user=abc db_port=3308
运行剧本时的错误

TASK [db_scripts : Run the SQLS] ******************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The field 'environment' has an invalid value, which includes an undefined variable. The error was: 'db_host' is undefined\n\nThe error appears to be in '/home/db_scripts/tasks/mail.yml': line 37, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n-\n  name: \"Run the SQLS\"\n  ^ here\n"}
请注意,当在“db_scripts/default/main.yml”下指定上述环境变量的值时 ,playbook运行良好。非常感谢你的一些意见。我刚开始学习ansible,所以请尽可能简单地使用示例。提前感谢:)

问:“在各自的清单文件中指定它们(变量)似乎不起作用,因为“未定义变量”的剧本错误是有意义的。”

答:该清单中的变量应按预期工作。简单地测试一下

- hosts: dbservers
  tasks:
    - debug:
        msg:
          - '{{ db_name }}'
          - '{{ db_user }}'
          - '{{ db_host }}'
          - '{{ db_port }}'
有两个问题

1)
db\u主机
在资源清册中缺失

2) 命令
inventory/app/production/inventory
中的路径与描述的路径不同
/app/production/inventory

ansible-playbook run_sqls.yml -i inventories/app/production/inventory

你说:
“…剧本中的“未定义变量”错误是有道理的。”
有没有可能解释一下到底什么是有道理的?谢谢@Vladimir Yeah db_在发布问题时错误地遗漏了库存中的主机。然而,仍然没有运气。我已经用剧本错误更新了我的原始问题。