【问题标题】:Ansible sum register valueAnsible 和寄存器值
【发布时间】:2020-09-11 14:22:36
【问题描述】:

如何使用 Jinja2 过滤 ansible 获得两台主机的总和 主机 1 和主机 2

---
- name: Count Check
  hosts: MYGROUP
  gather_facts: true
  user: sv_admin
  tasks:
    - name: count check
      shell: cat /etc/hosts | wc -l
      register: command_result
    - debug:
        var: command_result.stdout
    - set_fact:
        total_result: "{{ command_result.stdout | map('int') | sum(start=0) }}"
    - debug:
        msg: "Total count: {{ total_result }}" 

剧本输出

TASK [debug] *****************************************************************
ok: [Host-01] => {
  "msg": "Total count:      134"
}
ok: [Host-02] => {
  "msg": "Total count:      133"
}

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    使用extractsum。比如下面的剧本

    shell> cat playbook.yml
    - hosts: test_01:test_03
      gather_facts: false
      tasks:
        - shell: cat /etc/hosts | wc -l
          register: command_result
        - debug:
            var: command_result.stdout
        - set_fact:
            total_result: "{{ ansible_play_hosts_all|
                              map('extract', hostvars, ['command_result', 'stdout'])|
                              map('int')|
                              sum }}"
          run_once: true
        - debug:
            var: total_result
    

    给予(删节)

    shell> ansible-playbook playbook.yml 
    
    PLAY [test_01:test_03] ****
    
    TASK [shell] ****
    changed: [test_01]
    changed: [test_03]
    
    TASK [debug] ****
    ok: [test_01] => {
        "command_result.stdout": "      62"
    }
    ok: [test_03] => {
        "command_result.stdout": "      31"
    }
    
    TASK [set_fact] ****
    ok: [test_01]
    
    TASK [debug] ****
    ok: [test_03] => {
        "total_result": "93"
    }
    ok: [test_01] => {
        "total_result": "93"
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用自定义统计信息来做到这一点:https://docs.ansible.com/ansible/latest/modules/set_stats_module.html

      所以对于你的情况,它看起来像

      ---
      - name: Count Check
        hosts: MYGROUP
        gather_facts: true
        user: sv_admin
        tasks:
          - name: count check
            shell: cat /etc/hosts | wc -l
            register: command_result
          - debug:
              var: command_result.stdout
          - set_fact:
              host_result: "{{ command_result.stdout }}"
          - debug:
              msg: "Count for this host: {{ host_result }}"
          - set_stats:
              data: "{{ { 'total_count': host_result | int } }}"
      

      如果你用ANSIBLE_SHOW_CUSTOM_STATS=yes 运行它,它会在最后显示结果:

      $ ANSIBLE_SHOW_CUSTOM_STATS=yes ansible-playbook -i inventory pb.yml
      ... (usual output)
      
      CUSTOM STATS: *************************************************************
      
          RUN: { "total_count": 267}
      
      

      set_stats 任务默认将所有主机的结果加在一起,这就是您要查找的内容。不过,您需要确保这些值是整数,因为如果它们是字符串,它只会将它们连接起来,您最终会得到类似RUN: { "total_count": "134133"} 的东西。这就是为什么我把data: 位放在我的位置上 - 如果你尝试在常规 yaml 中创建字典,比如

        data:
          total_count: "{{ host_result | int }}"
      

      你会看到该值仍然是一个字符串(由于 yaml/jinja 的工作方式),它不会正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        相关资源
        最近更新 更多