尝试编写一个剧本来检查一组服务器(Linux 和 Windows 主机)上是否存在一组文件,如果存在,则替换这些文件。

这是我到目前为止所拥有的:

--- 
- hosts: "{{hosts}}" 
  vars: 
    scripts: 
      - check_blackout.pl 
      - check_empty.pl 
  tasks: 
    - name: windows stat 
      with_items: "{{scripts}}" 
      win_stat: path=D:\scripts\{{item}} 
      register: windows_stat 
      when: "'windows' in group_names" 
    - name: other stat 
      with_items: "{{scripts}}" 
      stat: path=/usr/local/bin/{{item}} 
      register: other_stat 
      remote_user: "{{script_owner | default(ansible_user)}}" 
      when: "'windows' not in group_names" 
    - name: windows debug 
      with_items: "{{windows_stat.results}}" 
      debug: var={{item.item}} 
      when: "{{item.stat.exists}}" 
    - name: other debug 
      with_items: "{{other_stat.results}}" 
      debug: var={{item.item}} 
      when: "{{item.stat.exists}}" 
... 

当我在 Windows 和 Linux 主机上运行它进行测试时,我得到以下信息:
[ansible@vmhklftpscdv1 ~]$ ansible-playbook test.yml -e "hosts=vmhkge1jasdev01,jdeesbkup" --ask-vault-pass 
Vault password:  
 
PLAY [vmhkge1jasdev01,jdeesbkup] ************************************************************************************************************************************************ 
 
TASK [Gathering Facts] ********************************************************************************************************************************************************** 
ok: [vmhkge1jasdev01] 
ok: [jdeesbkup] 
 
TASK [windows stat] ************************************************************************************************************************************************************* 
skipping: [jdeesbkup] => (item=check_blackout.pl)  
skipping: [jdeesbkup] => (item=check_empty.pl)  
ok: [vmhkge1jasdev01] => (item=check_blackout.pl) 
ok: [vmhkge1jasdev01] => (item=check_empty.pl) 
 
TASK [other stat] *************************************************************************************************************************************************************** 
skipping: [vmhkge1jasdev01] => (item=check_empty.pl)  
skipping: [vmhkge1jasdev01] => (item=check_blackout.pl)  
ok: [jdeesbkup] => (item=check_blackout.pl) 
ok: [jdeesbkup] => (item=check_empty.pl) 
 
TASK [windows debug] ************************************************************************************************************************************************************ 
skipping: [vmhkge1jasdev01] => (item={'_ansible_parsed': True, u'stat': {u'exists': False}, '_ansible_item_result': True, '_ansible_no_log': False, u'changed': False, 'item': u'check_empty.pl', 'invocation': {'module_name': u'win_stat'}})  
skipping: [vmhkge1jasdev01] => (item={'_ansible_parsed': True, u'stat': {u'exists': False}, '_ansible_item_result': True, '_ansible_no_log': False, u'changed': False, 'item': u'check_blackout.pl', 'invocation': {'module_name': u'win_stat'}})  
fatal: [jdeesbkup]: FAILED! => {"failed": true, "msg": "The conditional check '{{item.stat.exists}}' failed. The error was: error while evaluating conditional ({{item.stat.exists}}): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/home/ansible/test.yml': line 19, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      when: \"'windows' not in group_names\"\n    - name: windows debug\n      ^ here\n"} 
 
TASK [other debug] ************************************************************************************************************************************************************** 
fatal: [vmhkge1jasdev01]: FAILED! => {"failed": true, "msg": "The conditional check '{{item.stat.exists}}' failed. The error was: error while evaluating conditional ({{item.stat.exists}}): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/home/ansible/test.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      when: \"{{item.stat.exists}}\"\n    - name: other debug\n      ^ here\n"} 
        to retry, use: --limit @/home/ansible/test.retry 
 
PLAY RECAP ********************************************************************************************************************************************************************** 
jdeesbkup                  : ok=2    changed=0    unreachable=0    failed=1    
vmhkge1jasdev01            : ok=2    changed=0    unreachable=0    failed=1    
 
[ansible@vmhklftpscdv1 ~]$  

任何想法如何让这个工作?
我尝试了各种组合,包括在检查结果之前检查组成员身份:
  when: "'windows' in group_names and {{item.stat.exists}}" 

然而 Ansible 似乎仍然检查 item.stat字段,即使条件的第一部分为假。

或者我的基本方法是错误的,我应该将这些任务分成不同的剧本吗?

请您参考如下方法:

你是对的。 with_itemswhen 之前评估.您可以添加一个默认的空列表来避免错误:

- name: other debug 
  with_items: "{{other_stat.results | default([]) }}" 
  debug: var={{item}} 
  when: "{{item.stat.exists | default(false)}}" 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!