The following task is running regardless of whatever i set as the ansible_distribution.
下面的任务是运行,不管我设置为ansible_distribution。
- name: install packages
yum: name={{item.name}} state=installed
when: item.when
with_items:
- { name: 'telnet', when: "ansible_distribution == 'CentOS'" }
- { name: 'net-tools', when: "ansible_distribution == 'Debian'" }
- { name: 'net-tools', when: "ansible_distribution == 'blahblahblah'" }
Result
结果
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [172.16.2.3]
TASK [test : install packages] *********************************************************************************************************************************************************************************
ok: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'CentOS'", u'name': u'telnet'})
ok: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'Debian'", u'name': u'net-tools'})
ok: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'blahblahblah'", u'name': u'httpd'})
Here is my ansible version
这是我的一个可以解释的版本。
ansible 2.3.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.520150623 (Red Hat 4.8.5-11)]
However when wrap the when condition in jinja2 format like so "{{item.when}}"
, i get following albeit with a warning.
然而,当用jinja2格式包装时,就像这样“{{item。“当}”时,我得到了警告。
TASK [test : install packages] *********************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{item.when}}
skipping: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'blahblahblah'", u'name': u'httpd'})
skipping: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'Debian'", u'name': u'net-tools'})
ok: [172.16.2.3] => (item={u'when': u"ansible_distribution == 'CentOS'", u'name': u'telnet'})
Any idea how i can rewrite this task without having to wrap the when condition in a jinja2 format?
我是否知道如何重写这个任务,而不必在jinja2格式中使用when条件?
1 个解决方案
#1
1
You can try this:
你可以试试这个:
- name: install packages
package:
name: "{{ pkgs[ansible_distribution] | default(pkgs.default) }}"
state: present
vars:
pkgs:
CentOS: telnet
Debian: net-tools
default: net-tools
I replaced yum
module with package
to make it distro independent, because yum
will definitely fail on Debian.
我用包替换了yum模块,使它独立,因为yum肯定会在Debian上失败。
I agree with @techraf's comment that if you have a lot of tasks that are different for each distro, consider using include
/include_vars
and with_first_found
to include distro-specific set of tasks/variables (see example).
我同意@techraf的评论,如果您有许多不同的任务,对于每个发行版,可以考虑使用include/include_vars和with_first_found来包含特定于distr的任务/变量集(参见示例)。
#1
1
You can try this:
你可以试试这个:
- name: install packages
package:
name: "{{ pkgs[ansible_distribution] | default(pkgs.default) }}"
state: present
vars:
pkgs:
CentOS: telnet
Debian: net-tools
default: net-tools
I replaced yum
module with package
to make it distro independent, because yum
will definitely fail on Debian.
我用包替换了yum模块,使它独立,因为yum肯定会在Debian上失败。
I agree with @techraf's comment that if you have a lot of tasks that are different for each distro, consider using include
/include_vars
and with_first_found
to include distro-specific set of tasks/variables (see example).
我同意@techraf的评论,如果您有许多不同的任务,对于每个发行版,可以考虑使用include/include_vars和with_first_found来包含特定于distr的任务/变量集(参见示例)。