ansible register

register

register
用于注冊一個變量,保存命令的結(jié)果(shell或command模塊),這個變量可以在后面的task、when語句或模板文件中使用,該指令用在循環(huán)中會有不同,請看ansible學(xué)習(xí)之八:Loops中關(guān)于register的講解

- shell: /bin/pwd
  register: pwd_result

此時變量pwd_result的結(jié)果為:

{
    u'changed': True, 
    u'end': u'2014-02-23 12:02:51.982893', 
    u'cmd': [u'/bin/pwd'], 
    u'start': u'2014-02-23 12:02:51.980191', 
    u'delta': u'0:00:00.002702', 
    u'stderr': u'', 
    u'rc': 0,           #這個就是命令返回狀態(tài),非0表示執(zhí)行失敗
    'invocation': {'module_name': 'command', 'module_args': '/bin/pwd'}, 
    u'stdout': u'/home/sapser',    #以一個字符串保存命令結(jié)果
    'stdout_lines': [u'/home/sapser']     #以列表保存命令結(jié)果
}

在隨后的task中使用該變量:

- debug: msg="{{pwd_result}}"
  when: pwd_result.rc == 0

循環(huán)處理命令結(jié)果:

- name: registered variable usage as a with_items list
  hosts: all
  tasks:
      - name: retrieve the list of home directories
        command: ls /home
        register: home_dirs

      - name: add home dirs to the backup spooler
        file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
        with_items: home_dirs.stdout_lines       #等同于with_items: home_dirs.stdout.split()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容