ansible从json文件中读取变量

标签 ansible

我是 ansible 世界的新手。尝试提供 json 文件中的所有变量,但它不接受。它显示错误变量未定义

下面是我的 json 文件

{  
   "Tomcat":{  
       "SHPN":"8905",
       "HTPN":"8980",
       "SSPN":"8943",
       "AJPN":"8909",
       "Server":"test.example.com",
       "JENKINS_HOME":"/apps/tech/jenkins/jenkins-configuration",
       "PName":"Tomcat-Installation",
       "IName":"ansible_test",
       "IUID":"jbosscfg",
       "IGID":"staff",
       "IDEPT":"tech",
       "IRECPT":"test@example.com"
   }
}

下面是我的剧本

--- 
- 
  gather_facts: false
  hosts: "{{Server}}"
  tasks: 
    - 
      ignore_errors: true
      name: "find no of Tomcat Instance available on the Server"
      copy: 
        src: "{{ JENKINS_HOME }}/workspace/{{ PName }}/instance/"
        dest: /apps/tech/{{IName}}
        group: "{{IGID}}"
        owner: "{{IUID}}"
        mode: 0755 
    - 
      replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<DEPARTMENT NAME>' replace='{{IDEPT}}'"
    - 
      replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<RECIPIENT>' replace='{{IRECPT}}'"
    - 
      replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<TOMCAT INSTANCE NAME>' replace='{{IName}}'"
    - 
      replace: "dest='/apps/tech/{{IName}}/scripts/set_env.sh' regexp='<USER ID>' replace='{{IUID}}'"
    - 
      name: "Ansible Template Example"
      template: 
        src: tomcat_server.j2
        dest: /apps/tech/{{IName}}/conf/server.xml
        mode: 0777

我在执行ansible playbook时在下面 - 错误说变量未定义

test.example.com:~/final:$ ansible-playbook configure-tomcat-instance.yml --extra-vars "@test.json"
ERROR! the field 'hosts' has an invalid value, which appears to include a variable that is undefined. The error was: 'Server' is undefined

The error appears to have been in '/home/jbosscfg/final/configure-tomcat-instance.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

-
  gather_facts: false
  ^ here

最佳答案

您引用变量就好像它们位于顶层,而它们位于 Tomcat 键下。

使用 hosts: "{{Tomcat.Server}}" 或重新格式化您的 json 文件以消除 Tomcat 级别。

关于ansible从json文件中读取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365670/

相关文章:

php - Laravel 无法连接到 dockerise 数据库

azure - 当 Kubespray 中其他节点丢失时,Kubernetes 集群节点不会自动创建

filter - 在 ansible with_items 循环中根据条件跳过某些项目

ansible - [Ansible][Fedora 24] DNF 模块需要 python2-dnf 但它已经安装

ansible - 使用 Ansible 中的authorized_key模块将多个公共(public)ssh key 分配给用户定义

linux - 使用 Ansible 移动/复制目录中的所有文件

Ansible 与堡垒主机/跳箱?

amazon-web-services - ansible.vars.unsafe_proxy.AnsibleUnsafeText 对象'没有属性 'public_ip'

ansible:如何在提示中进行字符串插值?

linux - 迭代 Ansible 设置命令