ansible - "when" 条件不适用于逻辑 "AND" 运算符
ansible - "when" condition not working with logical "AND" operator
我正在尝试为 Django 应用程序的开发和测试环境配置编写 ansible 剧本。然而,在ansible任务中使用when条件似乎有问题。
在下面的代码中,只要任务 1 发生更改,就会执行任务 2。它不检查第二个条件。
- 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
任务 2 是 运行 每当任务 1 被更改而不评估第二个条件
"'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
任何想法,请帮助。
你的第二个条件似乎是一个字符串。我的意思是整个情况。字符串始终为真。
"'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
我正在尝试为 Django 应用程序的开发和测试环境配置编写 ansible 剧本。然而,在ansible任务中使用when条件似乎有问题。
在下面的代码中,只要任务 1 发生更改,就会执行任务 2。它不检查第二个条件。
- 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
任务 2 是 运行 每当任务 1 被更改而不评估第二个条件
"'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
任何想法,请帮助。
你的第二个条件似乎是一个字符串。我的意思是整个情况。字符串始终为真。
"'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