ansible-playbook yml 使用变量 打印变量结果

- name: 测试
  hosts: your_hosts
  tasks:
    - name: 使用变量
      command: ls -lh 
      register: command_result

    - name: 打印变量结果
      debug:
        # var: command_result.stdout
        # msg: "{
    
    { command_result.stdout }}"
        msg: "{
    
    { command_result.stdout.splitlines() }}"

ansible-playbook test.yml 
  • register 是一个关键字,用于将任务执行的结果保存到一个变量中
  • var 是一个关键字,用于在任务中引用变量,这个变量是一个字典(dictionary),其中包含了执行结果的不同属性,如stdout、stderr等

msg是debug模块的一个选项,用于指定要打印的消息内容,
splitlines()函数将command_result.stdout拆分,按照每行一个条目的方式显示。

猜你喜欢

转载自blog.csdn.net/u010953692/article/details/132452971