debugging 从Ansible中的交互式调试器打印项目

zf2sa74q  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(146)

我想以交互方式调试以下脚本中item s对象的内容。

- name: Deploy the apps from the apps directory to the splunk servers
  ansible.posix.synchronize:
    src: '{{ absolute_file_store_path }}/etc/{{ selected_apps_sourcedir }}/{{ item.source_app }}/'
    dest: '{{ splunk_path }}/etc/apps/{{ item.name }}/'
    copy_links: true
    recursive: true
    checksum: true
    times: false
    delete: true
    owner: false
    group: false
    archive: false
    use_ssh_args: "{{ synchronize_module_use_ssh_args | default(false) }}"
    rsync_path: 'sudo -u {{ splunk_user }} rsync'
    rsync_opts: '{{ selectable_apps_rsync_opts + item.rsync_opts if item.rsync_opts is defined else selectable_apps_rsync_opts }}'
  notify: notify standalone splunkd restart
  loop: '{{ selected_apps | default([]) if selected_apps | default([]) is iterable else [] }}'
  when:
    - item.state == 'present'
    - item.dest_dir is not defined or
      item.dest_dir == "etc"
    - item.target is defined and item.target == inventory_hostname or
      item.target is not defined

为此,我已经检查了使用debugger关键字启用调试器,但我找不到任何方法来打印item及其属性。
我已经添加了debugger: always,它就停在那里了。问题是我不知道打印item的语法,因为它不在task.argstask_vars中。
这甚至可以交互吗?是否表示不使用debug模块的msg

jhkqcmku

jhkqcmku1#

如何交互调试?

  • 我想以交互方式调试以下脚本中items对象的内容。我已经添加了debugger: always,它就停在那里。*

最小示例剧本

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - debug:
      msg: "{{ item }}"
    loop: [1, 2, 3]
    debugger: always

会有这种行为

TASK [debug] ******************************************
ok: [localhost] => (item=1) =>
  msg: 1
ok: [localhost] => (item=2) =>
  msg: 2
ok: [localhost] => (item=3) =>
  msg: 3
[localhost] TASK: debug (debug)> p task.args
{u'msg': u'{{ item }}'}
[localhost] TASK: debug (debug)> task_vars['msg'] = 4
[localhost] TASK: debug (debug)> p task_vars['msg']
4
[localhost] TASK: debug (debug)> c

PLAY RECAP ********************************************
localhost                  : ok=1

可以看到,使用debugger关键字启用调试器发生在围绕任务的循环已经完成之后。

  • 问题是我不知道打印item的语法,因为它不在task.argstask_vars中。*

即使使用文档中的正确语法

[localhost] TASK: debug (debug)> p task.args
{u'msg': u'{{ item }}'}
[localhost] TASK: debug (debug)> task_vars['msg'] = 4
[localhost] TASK: debug (debug)> p task_vars['msg']
4
[localhost] TASK: debug (debug)> c

它在那个阶段将不再有效果。

  • 我无法找到打印item及其属性的任何方法。*

要打印所有任务变量,只需在交互式调试器中使用p task_vars,但是,您要查找的内容在本示例中不会出现。

[localhost] TASK: debug (debug)> p task_vars
{'ansible_check_mode': False,
 'ansible_config_file': u'/home/ansibe_user/test/ansible.cfg',
 'ansible_connection': 'local',
 'ansible_current_hosts': [u'localhost'],
 'ansible_dependent_role_names': [],
 'ansible_diff_mode': False,
 'ansible_facts': {},
 'ansible_failed_hosts': [],
 'ansible_forks': 5,
 'ansible_inventory_sources': (u'/home/ansible_user/test/inventory.ini',),
 'ansible_play_batch': [u'localhost'],
 'ansible_play_hosts': [u'localhost'],
 'ansible_play_hosts_all': [u'localhost'],
 'ansible_play_name': u'localhost',
 'ansible_play_role_names': [],
 'ansible_playbook_python': '/usr/bin/python2',
 'ansible_python_interpreter': '/usr/bin/python2',
 'ansible_role_names': [],
 'ansible_run_tags': (u'all',),
 'ansible_skip_tags': (),
 'ansible_verbosity': 0,
 'ansible_version': {'full': '2.11.12',
                     'major': 2,
                     'minor': 11,
                     'revision': 12,
                     'string': '2.11.12'},
 'environment': [],
 'group_names': [],
 'groups': {'all': [], 'ungrouped': []},
 'hostvars': {},
 'inventory_hostname': u'localhost',
 'inventory_hostname_short': u'localhost',
 'omit': '__omit_place_holder__1234567890',
 'play_hosts': [u'localhost'],
 'playbook_dir': u'/home/ansible_user/test',
 'role_names': [],
 u'software': AnsibleMapping([(u'all', AnsibleMapping([(u'name', u'All')]))]),
 'vars': {'ansible_check_mode': False,
          'ansible_config_file': u'/home/ansible_user/test/ansible.cfg',
          'ansible_connection': 'local',
          'ansible_dependent_role_names': [],
          'ansible_diff_mode': False,
          'ansible_facts': {},
          'ansible_forks': 5,
          'ansible_inventory_sources': (u'/home/ansibe_user/test/inventory.ini',),
          'ansible_play_batch': [u'localhost'],
          'ansible_play_hosts': [u'localhost'],
          'ansible_play_hosts_all': [u'localhost'],
          'ansible_play_name': u'localhost',
          'ansible_play_role_names': [],
          'ansible_playbook_python': '/usr/bin/python2',
          'ansible_python_interpreter': '/usr/bin/python2',
          'ansible_role_names': [],
          'ansible_run_tags': (u'all',),
          'ansible_skip_tags': (),
          'ansible_verbosity': 0,
          'ansible_version': {'full': '2.11.12',
                              'major': 2,
                              'minor': 11,
                              'revision': 12,
                              'string': '2.11.12'},
          'environment': [],
          'group_names': [],
          'groups': {'all': [], 'ungrouped': []},
          'hostvars': {},
          'inventory_hostname': u'localhost',
          'inventory_hostname_short': u'localhost',
          'omit': '__omit_place_holder__1234567890',
          'play_hosts': [u'localhost'],
          'playbook_dir': u'/home/ansible_user/test',
          'role_names': [],
          u'software': AnsibleMapping([(u'all', AnsibleMapping([(u'name', u'All')]))])}}
[localhost] TASK: debug (debug)>

摘要

  • 这是可能的互动吗?*

不是完全按照要求的方式。你能做的是

  • 通过task_vars['item'] = 0重新定义变量item
  • update_task
  • redo运行

导致输出

TASK [debug] *****************************************
ok: [localhost] => (item=1) =>
  msg: 1
ok: [localhost] => (item=2) =>
  msg: 2
ok: [localhost] => (item=3) =>
  msg: 3
[localhost] TASK: debug (debug)> task_vars['item'] = 0
[localhost] TASK: debug (debug)> update_task
[localhost] TASK: debug (debug)> redo
[WARNING]: The loop variable 'item' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to
avoid variable collisions and unexpected behavior.
ok: [localhost] => (item=1) =>
  msg: 0
ok: [localhost] => (item=2) =>
  msg: 0
ok: [localhost] => (item=3) =>
  msg: 0
[localhost] TASK: debug (debug)>

请注意有关使用loop_var定义变量名的警告消息。

如何继续?

  • 我要调试items对象的内容...*

这也可以通过使用label限制循环输出来实现

loop_control:
    label: " {{ item.state }}, {{ item.dest_dir }}, {{ item.target }}"
    pause: 1

这取决于你想达到什么目的

相关问题