将来自yml内部shell的响应用于Ansible playbook

将来自yml内部shell的响应用于Ansible playbook,ansible,yaml,Ansible,Yaml,我想通过Ansible playbook安装MongoDB,我遵循中的说明: 有关“步骤2-创建源列表文件MongoDB”的步骤 我应该使用: echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list 当它通过以下命令获得ubuntu版本时: $(lsb

我想通过Ansible playbook安装MongoDB,我遵循中的说明:

有关“步骤2-创建源列表文件MongoDB”的步骤

我应该使用:

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
当它通过以下命令获得ubuntu版本时:

$(lsb_release -sc)
我怎样才能通过yml文件完成它,并通过ansible palybook运行它? 我使用了下面的yml命令,但它不起作用,并且给出了错误,因为我在脚本中使用了shell命令“$(lsb_release-sc)”

- name: Create source list file MongoDB
  sudo: yes
  lineinfile: >
    line="deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse"
    dest=/etc/apt/sources.list.d/mongodb-org-3.2.list
    state=present
    create=yes

您可以将一个任务的结果(包括其标准输出)注册为变量,然后在以后的任务中使用它:

- name: Work out the distribution
  command: lsb_release -sc
  register: result

- name: Create source list file MongoDB
  sudo: yes
  lineinfile: >
    line="deb http://repo.mongodb.org/apt/ubuntu {{ result.stdout }}/mongodb-org/3.2 multiverse"
    dest=/etc/apt/sources.list.d/mongodb-org-3.2.list
    state=present
    create=yes
Ansible中有一个模块:

- apt_repository:
    repo: deb http://repo.mongodb.org/apt/ubuntu {{ ansible_distribution_release | lower }}/mongodb-org/3.2 multiverse
    state: present

我是yml的新手,从来都不知道这个选项,非常感谢。没问题。您还将发现这是一个ansible功能,而不是yaml功能。使用
lineinfle
始终是一个危险信号,您可以而且应该轻松地使用
template
执行此操作。此外,您可以使用
{{ansible_发行版| lower}}
而不是lsb_发行版。