我创建一个包含内容的文件,我需要将其传递给我的任务参数:
原来的任务是这样的:
- name: Configure Web filter profiles.
fortinet.fortios.fortios_webfilter_profile:
vdom: "{{ vdom }}"
state: "present"
access_token: "x4jcqmpwhrgdjw0p4qnHcw35m5GH63"
webfilter_profile:
feature_set: flow
name: "{{ wf_name }}"
ftgd_wf:
options: "error-allow"
filters:
-
action: "block"
category: "41"
id: "41"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"
max_quota_timeout: "300"
rate_crl_urls: "enable"
rate_css_urls: "enable"
rate_javascript_urls: "enable"
当我使用这个剧本时工作正常,但我需要为多个 ID 创建多行,如下所示:
- name: Configure Web filter profiles.
fortinet.fortios.fortios_webfilter_profile:
vdom: "{{ vdom }}"
state: "present"
access_token: "x4jcqmpwhrgdjw0p4qnHcw35m5GH63"
webfilter_profile:
feature_set: flow
name: "{{ wf_name }}"
ftgd_wf:
options: "error-allow"
filters:
-
action: "block"
category: "41"
id: "41"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"
-
action: "block"
category: "42"
id: "42"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"
-
action: "block"
category: "42"
id: "42"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"
(--- etc ids)
max_quota_timeout: "300"
rate_crl_urls: "enable"
rate_css_urls: "enable"
rate_javascript_urls: "enable"
因此,我使用 lineinfile 模块创建一个文件来添加存在 ID 号的行:
- name: Create file
file:
path: "/tmp/wf_content.txt"
state: touch
mode: '0775'
- name: Add lines in file
lineinfile:
path: "/tmp/wf_content.txt"
line: |
'-
action: "block"
category: "{{ item }}"
id: "{{ item }}"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"'
loop: "{{ id_number }}"
- name: remove blank lines in file
shell: "sed -e '/^[[:blank:]]*$/d' wf_content.txt > wf_content2.txt"
args:
chdir: "/tmp/"
但是当我从文件中替换任务内容时,我收到“请求字典,无法解析 JSON 或 key=value”
我的完整剧本是下一个:
- name: Create file with context to fortinet
hosts: localhost
become: false
gather_facts: false
vars_files:
vars_fortinet.yml
tasks:
- name: Create file
file:
path: "/tmp/wf_content.txt"
state: touch
mode: '0775'
- name: Add lines in file
lineinfile:
path: "/tmp/wf_content.txt"
line: |
'-
action: "block"
category: "{{ item }}"
id: "{{ item }}"
log: "enable"
warn_duration: "5m"
warning_duration_type: "timeout"
warning_prompt: "per-category"'
loop: "{{ id_number }}"
- name: remove blank lines in file
shell: "sed -e '/^[[:blank:]]*$/d' wf_content.txt > wf_content2.txt"
args:
chdir: "/tmp/"
- name: Test connection to fortinet
hosts: fortinet
connection: httpapi
become: false
vars:
ansible_httpapi_use_ssl: yes
ansible_httpapi_validate_certs: false
ansible_network_os: fortinet.fortios.fortios
vdom: "VDOM_BOT"
wf_name: wf_10_0_0_3
# Esto debe pasarlo el chatbot -- Convertir la IP de punto a subguión
vars_files:
vars_fortinet.yml
tasks:
- name: Get values from file
shell: "cat /tmp/wf_content2.txt"
register: output
- name: Configure Web filter profiles.
fortinet.fortios.fortios_webfilter_profile:
vdom: "{{ vdom }}"
state: "present"
access_token: "x4jcqmpwhrgdjw0p4qnHcw35m5GH63"
webfilter_profile:
feature_set: flow
name: "{{ wf_name }}"
ftgd_wf:
options: "error-allow"
filters:
"{{ output.stdout_lines }}"
max_quota_timeout: "300"
rate_crl_urls: "enable"
rate_css_urls: "enable"
rate_javascript_urls: "enable"
我希望你能帮忙解决这个问题,抱歉我的英语不好。
给定列表ids
ids: [41, 42, 43]
创建模板而不是文本文件。例如,
shell> cat wf_content.j2
{% filter from_yaml %}
{% for id in ids %}
- action: block
category: {{ id }}
id: {{ id }}
log: enable
warn_duration: 5m
warning_duration_type: timeout
warning_prompt: per-category
{% endfor %}
{% endfilter %}
创建列表过滤器
filters: "{{ lookup('template', 'wf_content.j2') }}"
给予
filters:
- action: block
category: 41
id: 41
log: enable
warn_duration: 5m
warning_duration_type: timeout
warning_prompt: per-category
- action: block
category: 42
id: 42
log: enable
warn_duration: 5m
warning_duration_type: timeout
warning_prompt: per-category
- action: block
category: 43
id: 43
log: enable
warn_duration: 5m
warning_duration_type: timeout
warning_prompt: per-category
在模块中使用它。将声明放入任务的变量中
- name: Configure Web filter profiles.
fortinet.fortios.fortios_webfilter_profile:
vdom: "{{ vdom }}"
state: present
access_token: "x4jcqmpwhrgdjw0p4qnHcw35m5GH63"
webfilter_profile:
feature_set: flow
name: "{{ wf_name }}"
ftgd_wf:
options: error-allow
filters: "{{ filters }}"
...
vars:
filters: "{{ lookup('template', 'wf_content.j2') }}"