q986 发表于 2018-7-30 11:23:58

自动化运维工具Ansible之Playbooks循环语句

# cat /etc/ansible/reloop.yml  
---
  
- hosts: webservers
  
remote_user: root
  
tasks:
  
   - shell: echo "{{ item }}"
  
   with_items:
  
      - one
  
      - two
  
   register: echo
  
   - debug: msg="{{ echo }}"
  
   - name: Fail if return code is not 0
  
   fail: msg=" The command {{ item.cmd }} has a 0 return code"
  
   when: item.rc != 0
  
   with_items: "{{ echo.results }}"
  

  

  
# ansible-playbook /etc/ansible/reloop.yml
  

  
PLAY *************************************************************
  

  
GATHERING FACTS ***************************************************************
  
ok:
  

  
TASK: ***********************************************
  
changed: => (item=one)
  
changed: => (item=two)
  

  
TASK: ************************************************
  
ok: => {
  
    "msg": "{'msg': 'All items completed', 'changed': True, 'results': [{u'stdout': u'one', u'changed': True, u'end': u'2015-08-05 11:30:42.560652', u'start': u'2015-08-05 11:30:42.549056', u'cmd': u'echo \"one\"', u'rc': 0, 'item': 'one', u'stderr': u'', u'delta': u'0:00:00.011596', 'invocation': {'module_name': u'shell', 'module_args': u'echo \"one\"'}, 'stdout_lines': , u'warnings': []}, {u'stdout': u'two', u'changed': True, u'end': u'2015-08-05 11:30:44.105387', u'start': u'2015-08-05 11:30:44.093500', u'cmd': u'echo \"two\"', u'rc': 0, 'item': 'two', u'stderr': u'', u'delta': u'0:00:00.011887', 'invocation': {'module_name': u'shell', 'module_args': u'echo \"two\"'}, 'stdout_lines': , u'warnings': []}]}"
  
}
  

  
TASK: ******************************************
  
skipping: => (item={u'cmd': u'echo "one"', u'end': u'2015-08-05 11:30:42.560652', u'stderr': u'', u'stdout': u'one', u'changed': True, u'rc': 0, 'item': 'one', u'warnings': [], u'delta': u'0:00:00.011596', 'invocation': {'module_name': u'shell', 'module_args': u'echo "one"'}, 'stdout_lines': , u'start': u'2015-08-05 11:30:42.549056'})
  
skipping: => (item={u'cmd': u'echo "two"', u'end': u'2015-08-05 11:30:44.105387', u'stderr': u'', u'stdout': u'two', u'changed': True, u'rc': 0, 'item': 'two', u'warnings': [], u'delta': u'0:00:00.011887', 'invocation': {'module_name': u'shell', 'module_args': u'echo "two"'}, 'stdout_lines': , u'start': u'2015-08-05 11:30:44.093500'})
  

  
PLAY RECAP ********************************************************************
  
192.168.1.65               : ok=4    changed=1    unreachable=0    failed=0
页: [1]
查看完整版本: 自动化运维工具Ansible之Playbooks循环语句