ansible-playbook yml view process

- name: 查看 sshd 进程
  hosts: your_hosts
  tasks:
    - name: 运行 pgrep 命令查找 sshd 进程
      shell: pgrep sshd
      register: command_result

    - name: 打印进程输出
      debug:
        var: command_result.stdout_lines

ansible-playbook process.yml 
  • stdout_lines is an attribute of the variable, and the variable result is printed line by line
- name: 查看并统计 sshd 进程
  hosts: your_hosts
  tasks:
    - name: 运行 pgrep 命令查找 sshd 进程
      shell: pgrep sshd
      register: command_result

    - name: 打印进程输出
      debug:
        var: command_result.stdout_lines

    - name: 统计进程数量
      set_fact:
        process_count: "{
    
    { command_result.stdout_lines | length }}"

    - name: 打印进程数量
      debug:
        var: process_count
  • The set_fact module is a built-in module in Ansible, used to set a fact in the playbook. Facts are variables in Ansible that can be used in other tasks in the playbook
- name: 查看并统计 sshd 进程
  hosts: your_hosts
  tasks:
    - name: 运行 pgrep 命令查找 sshd 进程
      shell: pgrep sshd
      register: command_result

    - name: 打印进程输出
      debug:
        var: command_result.stdout_lines

    - name: 统计进程数量
      set_fact:
        process_count: "{
    
    { command_result.stdout_lines | length }}"

    - name: 打印进程数量
      debug:
        var: process_count

- name: 统计所有主机的进程数量总和
  hosts: localhost
  gather_facts: false
  tasks:
    - name: 收集进程数量
      command: echo "{
    
    { hostvars[item].process_count }}"
      register: process_counts
      loop: "{
    
    { groups['jiedan11_c2'] }}"

    - name: 计算进程数量总和
      set_fact:
        total_process_count: "{
    
    { process_counts.results | map(attribute='stdout') | map('int') | sum }}"

    - name: 打印进程数量总和
      debug:
        var: total_process_count

Guess you like

Origin blog.csdn.net/u010953692/article/details/132454322