如何在ansible的嵌套json中编写变量/硬编码值?

问题描述 投票:0回答:1

我正在尝试创建一个带有硬代码值的json文件作为嵌套json中的输出。但是第二个游戏正在覆盖第一个播放值。那么我们有什么最佳选择吗?

我已尝试使用to_nice_json模板将变量复制到json文件。但是无法在imported_var中保留多个变量值以复制到json文件

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
  - name: load var from file
    include_vars:
      file: /tmp/var.json
      name: imported_var

  - name: Checking mysqld status
    shell: service mysqld status
    register: mysqld_stat
    ignore_errors: true

  - name: Checking mysqld status
    shell: service httpd status
    register: httpd_stat
    ignore_errors: true

  - name: append mysqld status to output json
    set_fact:
     imported_var: "{{ imported_var | combine({ 'status_checks':[{'mysqld_status': (mysqld_stat.rc == 0)|ternary('good', 'bad') }]})}}"

# - name: write var to file
 #   copy:
  #    content: "{{ imported_var | to_nice_json }}"
   #   dest: /tmp/final.json

  - name: append httpd status to output json
    set_fact:
      imported_var: "{{ imported_var| combine({ 'status_checks':[{'httpd_status': (httpd_stat.rc == 0)|ternary('good', 'bad') }]})}}"

 # - debug:
  #    var: imported_var

  - name: write var to file
    copy:
      content: "{{ imported_var | to_nice_json }}"
      dest: /tmp/final.json

预期结果:

{
    "status_checks": [
        {
            "mysqld_status": "good"
            "httpd_status": "good"
        }
    ]
}

实际结果:

{
    "status_checks": [
        {
            "httpd_status": "good"
        }
    ]
}
ansible jinja2
1个回答
1
投票

你正在尝试执行Ansible真正不是那么擅长的那种数据操作。每当您尝试修改现有变量时 - 尤其是当您尝试设置嵌套值时 - 您的生活变得复杂。话虽如此,有可能做你想要的。例如:

---
- hosts: localhost
  gather_facts: false
  vars:
    imported_var: {}

  tasks:
    - name: Checking sshd status
      command: systemctl is-active sshd
      register: sshd_stat
      ignore_errors: true

    - name: Checking httpd status
      command: systemctl is-active httpd
      register: httpd_stat
      ignore_errors: true

    - set_fact:
        imported_var: "{{ imported_var|combine({'status_checks': []}) }}"

    - set_fact:
        imported_var: >-
          {{ imported_var|combine({'status_checks':
          imported_var.status_checks + [{'sshd_status': (sshd_stat.rc == 0)|ternary('good', 'bad')}]}) }}

    - set_fact:
        imported_var: >-
          {{ imported_var|combine({'status_checks':
          imported_var.status_checks + [{'httpd_status': (httpd_stat.rc == 0)|ternary('good', 'bad')}]}) }}

    - debug:
        var: imported_var

在我的系统上(运行sshd但没有运行httpd,这将输出:

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "imported_var": {
        "status_checks": [
            {
                "sshd_status": "good"
            }, 
            {
                "httpd_status": "bad"
            }
        ]
    }
}

您可以通过重组数据来大大简化剧本。使status_checks成为顶级变量,而不是让它成为列表,让它成为将服务名称映射到相应状态的字典。将它与一些循环相结合,最终会得到一些非常简单的东西:

---
- hosts: localhost
  gather_facts: false

  tasks:

    # We can use a loop here instead of writing a separate task
    # for each service.
    - name: Checking service status
      command: systemctl is-active {{ item }}
      register: services
      ignore_errors: true
      loop:
        - sshd
        - httpd

    # Using a loop in the previous task means we can use a loop
    # when creating the status_checks variable, which again removes
    # a bunch of duplicate code.
    - name: set status_checks variable
      set_fact:
        status_checks: "{{ status_checks|default({})|combine({item.item: (item.rc == 0)|ternary('good', 'bad')}) }}"
      loop: "{{ services.results }}"

    - debug:
        var: status_checks

以上将输出:

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "status_checks": {
        "httpd": "bad", 
        "sshd": "good"
    }
}

如果您真的想将此信息添加到imported_var,则可以在单个任务中执行此操作:

- set_fact:
    imported_var: "{{ imported_var|combine({'status_checks': status_checks}) }}"
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.