有条件地执行Ansible角色或根本不执行剧本

时间:2019-01-29 15:38:59

标签: ansible

我有一部剧本triad-ptf。它运行一个名为ptf_install的角色。主机vars文件的变量ptfCount初始化为0。

如果ptfCount大于0,我希望ptf_install运行,否则什么也不做。

我认为,如果剧本实际检查ptfCount的值并退出而没有错误,那会更好。但我似乎无法弄清楚其语法。因此,该剧本不会失败,因为它与其他剧本一起运行,并且0 PTF并非错误。

我尝试了此操作,但结果出乎意料。前三个任务已作为详细here跳过,但第四个任务已执行,我不知道为什么。

剧本:triad-ptf.yml

- hosts: triad
  become: yes
  become_method: sudo
  environment:
    INFORMIXDIR: /usr/informix
    PATH: "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/informix/bin:/usr/cbridge/bin:/usr/cbridge/jam/rh3:/root/bin"
  vars:
    playbook_name: triad-ptf.yml
  pre_tasks:
    - debug: var=ptfCount
  roles:
    - { role: notification_slack, color: "normal", message: "[Start] Run {{playbook_name}} playbook on {{ inventory_hostname }}, for {{cbMajor}}.{{cbMinor}}.{{cbBuild}} on Informix {{InformixServerVersion}}" }
    - role: common
    - { role: ptf_install,  when: ptfCount > 0}
    - role: motd
    - { role: notification_slack, color: "good", message: "[Finish] Run {{playbook_name}} playbook on {{ inventory_hostname }}" }   

- { role: ptf_install,  when: ptfCount > 0}

角色ptf_install

- name: Include version specific PTF variables file
  include_vars:  "v{{cbMajor}}.{{cbMinor}}.{{cbBuild}}.yml"
  tags: [ 'always' ]

- name: Check if working_directory exists for ptf download
  stat:
    path: "{{working_directory}}"
  register: workingDirectory
  tags: [ 'database' ,'downloadFiles']

- name: Create working_directory open to all
  file:
    path: "{{working_directory}}"
    state: directory
    mode: 0777
  when: workingDirectory.stat.exists == False
  tags: [ 'database' ,'downloadFiles']

- name: "Copy PTFs to the target"
  become: false
  maven_artifact: 
    group_id: com.companyname
    artifact_id: cbridge
    classifier: "cbptf{{cbMajor}}{{cbMinor}}{{cbBuild}}-{{item}}"
    extension: "{{ptf_extension}}"
    version: "{{cbMajor}}.{{cbMinor}}.{{cbBuild}}"
    repository_url: "{{nexus_repo_url}}"
    dest: "{{working_directory}}/cbptf{{cbMajor}}{{cbMinor}}{{cbBuild}}-{{item}}.{{ptf_extension}}"
    timeout: 60
  with_sequence: start=1 end="{{ptfCount}}"
  when: ("'primary' in group_names")
  tags: [ 'downloadFiles' ]

- name: Suspend the system before applying PTFs
  shell: "cbsuspend"
  when: ptfCount | int > 0

- name: "Apply the PTFs for Triad"
  shell: "./installptf -y -s -d /var/log/eti/installer 2>&1 >> /var/log/eti/installer/cbridge_{{cbMajor}}.{{cbMinor}}.{{cbBuild}}.{{item}}_install_log.txt"
  args:
    chdir: "/usr/cbridge/bin"
  with_sequence: start=1 end="{{ptfCount}}"
  when: ("'primary' in group_names")
  register: ptf_install_result

- name: Resume the system after applying PTFs
  shell: "cbresume"
  when: ptfCount | int > 0

调试输出显示该值为0

TASK [debug] *****************************************************************************************************************************************************************************************************$
ok: [cbt470.qa] => {
    "ptfCount": 0
}

Ansible输出

TASK [ptf_install : Include version specific PTF variables file] **************************************************************************************************************************************************
skipping: [cbt470.qa]

TASK [ptf_install : Check if working_directory exists for ptf download] *******************************************************************************************************************************************
skipping: [cbt470.qa]

TASK [ptf_install : Create working_directory open to all] *********************************************************************************************************************************************************
skipping: [cbt470.qa]

TASK [ptf_install : Copy PTFs to the target] **********************************************************************************************************************************************************************
fatal: [cbt470.qa]: FAILED! => {"msg": "to count backwards make stride negative"}

PLAY RECAP ********************************************************************************************************************************************************************************************************
cbt470.qa : ok=22   changed=3    unreachable=0    failed=1 

0 个答案:

没有答案