如何从ansible playbook中的文件中读取所有行
how to read all lines from a file in ansible playbook
我有一个名为 packages.txt 的文件,其中包含软件包列表。我必须通过我的 ansible 文件 rpm.yml 阅读 package.txt 的每一行。这是我的代码
- hosts: myhost
tasks:
- name: RPM Filter
theforeman.foreman.content_view_filter:
username: "admin"
password: "admin"
server_url: "myhost"
name: "rpm filter"
organization: "myorg"
content_view: "My content view"
filter_type: "rpm"
package_name: "{{ item }}"
inclusion: True
loop:
- packages.txt
我的packages.txt包含这些
open-ssh
nginx
grafana
vim
如有任何帮助,我们将不胜感激
您将需要一个额外的任务来读取文件。最简单的方法是将 cat
文件和 register
放入变量中。然后这个变量可以用于下一个任务。
类似于:
- name: get contents of package.txt
command: "cat packages.txt"
register: _packages
- name: RPM Filter
theforeman.foreman.content_view_filter:
username: "admin"
password: "admin"
server_url: "myhost"
name: "rpm filter"
organization: "myorg"
content_view: "My content view"
filter_type: "rpm"
package_name: "{{ item }}"
inclusion: True
loop: "{{ _packages.stdout_lines }}"
query 行 插件。例如
- debug:
var: item
loop: "{{ query('lines', 'cat packages.txt') }}"
给出(删节)
item: open-ssh
item: nginx
item: grafana
item: vim
查看插件详情
shell> ansible-doc -t lookup lines
我有一个名为 packages.txt 的文件,其中包含软件包列表。我必须通过我的 ansible 文件 rpm.yml 阅读 package.txt 的每一行。这是我的代码
- hosts: myhost
tasks:
- name: RPM Filter
theforeman.foreman.content_view_filter:
username: "admin"
password: "admin"
server_url: "myhost"
name: "rpm filter"
organization: "myorg"
content_view: "My content view"
filter_type: "rpm"
package_name: "{{ item }}"
inclusion: True
loop:
- packages.txt
我的packages.txt包含这些
open-ssh
nginx
grafana
vim
如有任何帮助,我们将不胜感激
您将需要一个额外的任务来读取文件。最简单的方法是将 cat
文件和 register
放入变量中。然后这个变量可以用于下一个任务。
类似于:
- name: get contents of package.txt
command: "cat packages.txt"
register: _packages
- name: RPM Filter
theforeman.foreman.content_view_filter:
username: "admin"
password: "admin"
server_url: "myhost"
name: "rpm filter"
organization: "myorg"
content_view: "My content view"
filter_type: "rpm"
package_name: "{{ item }}"
inclusion: True
loop: "{{ _packages.stdout_lines }}"
query 行 插件。例如
- debug:
var: item
loop: "{{ query('lines', 'cat packages.txt') }}"
给出(删节)
item: open-ssh
item: nginx
item: grafana
item: vim
查看插件详情
shell> ansible-doc -t lookup lines