Ansible从多个Json文件访问相同的变量

时间:2019-01-28 18:14:30

标签: ansible

我在放置主机的本地主机上有多个.json文件:

json-file-path/{{ testName }}.json

{{testName}}。json是:testA.json,testB.json,testC.json ...等。

所有.json文件具有相同的键,但具有不同的值,例如:

json-file-path / testA.json:

{
   “a_key”: “a_value1”
   “b_key”: “b_value1”
 }

json-file-path / testB.json:

{
   “a_key”: “a_value2”
   “b_key”: “b_value2”
 }

json-file-path / testC.json:

{
   “a_key”: “a_value3”
   “b_key”: “b_value3”
 }

.....

我需要从所有.json文件访问键值变量,如果值满足某些条件,我将在目标主机中执行某些任务。例如,我有:

a_value1=3
a_value2=4
a_value3=1

我会逐个浏览.json文件,如果a_key [value]> 3,我会将这个.json文件复制到目标主机,否则跳过该任务。在这种情况下,我只会将testC.json复制到目标主机。

我将如何实现?我正在考虑使用{{testName}}作为dict的动态键来重新构建.json文件:

 {
    “testName”: “testA”
        {
           “a_key”: “a_value1”
           “b_key”: “b_value1”
  }

因此,我可以使用{{testName}}。a_key来访问我的变量。到目前为止,我还无法实现这一目标。

我在剧本中尝试了以下内容:

—-
- host: localhost 
  tasks:
    - name: construct json files
      vars:
        my_vars:
          a_key: “{{ a_value }}”
          b_key: “{{ b_value }}”
        with_dict: “{{ testName }}”
      copy:
        content: “{{ my_vars | to_nice_json }}”
        dest: /json-file-path/{{ testName }}.json

我更新的剧本是:

/mypath/tmp/include.yaml:

—-
- hosts: remote_hostName
  tasks:
  - name: load json files
    set_fact:
      json_data: “{{ lookup(‘file’, item) | from_json }}”
  - name: copy json file if condition meets
    copy:
      src: “{{ item }}”
      dest: “{{ /remote_host_path/tmp}}/{{item | basename }}”
    delegate_to: “{{ remote_hostName }}”
    when: json_data.a_key|int>5

/mypath/test.yml:
—-
- hosts: localhost
  vars:
    local_src_ dir: /mypath/tmp
    remote_host: remote_hostName
    remote_dest_dir: /remote_host_path/tmp
  tasks:
    - name: looping
      include: include.yaml
      with_fileglob:
         - “{{ local_src_dir }}/*json”

/ mypath / tmp /下localhost上的所有json文件。

最新版本的剧本。现在正在工作:

/mypath/tmp/include.yaml:
—-
- name: loafing json flies
  include_vars:
    file: “{{ item }}”
    name: json_data
- name: copy json file to remote if condition meets
  copy: 
    src: “{{ item }}”
    dest: ‘/remote_host_path/tmp/{{item | basename}}’
  delegate_to: “{{ remote_host }}”
  when: json_data.a_key > 5

/mypath/test.yml:

—-
- hosts: localhost
  vars: 
    local_src_dir: /mypath/tmp
    remote_host: remote_hostName
    remote_dest_dir: /remote_host_path/tmp
  tasks:
    - name: looping json files
      include: include.yaml
      with_fileglob:
         - “{{ local_src_dir }}”/*json”

1 个答案:

答案 0 :(得分:1)

我希望我正确地理解了您的要求,这将帮助您前进。

从根本上讲,您可以加载每个JSON文件,以便可以将值作为本地Ansible变量进行查询。因此,您可以循环浏览所有文件,读取每个文件,比较您感兴趣的值,然后通过委派任务有条件地复制到远程主机。因此,尝试一下:

创建一个包含文件include.yaml

---
# 'item' contains a path to a local JSON file on each pass of the loop

- name: Load the json file
  set_fact:
    json_data: "{{ lookup('file', item) | from_json }}"
- name: Delegate a copy task to the remote host conditionally
  copy:
    src: "{{ item }}"
    dest: "{{ remote_dest_dir }}/{{ item | basename }}"
  delegate_to: "{{ remote_host }}"
  when: json_data.a_key > value_threshold

然后在您的剧本中

---

- hosts: localhost
  connection: local
  # Set some example vars, tho these could be placed in a variety of places
  vars:
    local_src_dir: /some/local/path
    remote_host: <some_inventory_hostname>
    remote_dest_dir: /some/remote/path
    value_threshold: 3
  tasks:
    - name: Loop through all *json files, passing matches to include.yaml
      include: include.yaml
      loop: "{{ lookup('fileglob', local_src_dir + '/*json').split(',') }}"

注意:在您运行旧版本的Ansible时,可能需要较旧的备用语法才能使所有这些正常工作:

在您的包含文件中:

- name: Load the json file
  set_fact:
    include_vars: "{{ item }}"
- name: Delegate a copy task to the remote host conditionally
  copy:
    src: "{{ item }}"
    dest: "{{ remote_dest_dir }}/{{ item | basename }}"
  delegate_to: "{{ remote_host }}"
  when: a_key > value_threshold

和您的剧本中:

- name: Loop through all *json files, passing matches to include.yaml
  include: include.yaml
  with_fileglob:
    - "{{ local_src_dir }}/*json"