Jah*_*iam 1 yaml jinja2 ansible ansible-playbook
我正在尝试为开发人员编写ansible playbooks并为django应用程序测试env配置.然而,在ansible任务中使用条件时似乎存在问题.
在下面的代码中,任务2在任务1被更改时执行.它不检查第二个条件.
- name: Task 1
  become: yes
  command: docker-compose run --rm web python manage.py migrate chdir="{{ server_code_path }}"
  when: perform_migration
  register: django_migration_result
  changed_when: "'No migrations to apply.' not in django_migration_result.stdout"
  tags:
    - start_service
    - django_manage
- name: Task 2 # Django Create Super user on 1st migration
  become: yes
  command: docker-compose run --rm web python manage.py loaddata create_super_user_data.yaml chdir="{{ server_code_path }}"
  when: django_migration_result|changed and ("'Applying auth.0001_initial... OK' in django_migration_result.stdout")
  ignore_errors: yes
  tags:
    - start_service
    - django_manage
每当更改Task1而不评估第二个条件时,就会运行任务2
"'Applying auth.0001_initial... OK' in django_migration_result.stdout"
当我尝试没有django_migration_result|changed它是按预期工作.
- name: Task 2 # Django Create Super user on 1st migration
  become: yes
  command: docker-compose run --rm web python manage.py loaddata create_super_user_data.yaml chdir="{{ server_code_path }}"
  when: "'Applying auth.0001_initial... OK' in django_migration_result.stdout"
以上是按预期工作的.我尝试用boolean var替换它,甚至还没有运气.
Ansible版本:2.0.0.1
任何想法,请帮忙.
你的第二个条件似乎是一个字符串.我的意思是整个条件.字符串始终为true.
"'Applying auth.0001_initial... OK' in django_migration_result.stdout"
在最后一个代码块中,整个条件都在引号中.这将是yaml级别的字符串以及它之所以有效的原因.
这个:
key: value
是相同的:
key: "value"
写这样的条件应该可以解决问题:
when: django_migration_result|changed and ('Applying auth.0001_initial... OK' in django_migration_result.stdout)
甚至更好:
when:
  - django_migration_result | changed
  - 'Applying auth.0001_initial... OK' in django_migration_result.stdout