[英]How to define as variables the strings of hostname of target-server-which-will-run-the-tasks in ansible playbook?
I have written an ansible playbook,我写了一个 ansible 剧本,
it is named main.yml
, placed at path /my_path/ansible-venv/bin/ansible-playbook playbooks/generate_results/
.它被命名为
main.yml
,放置在路径/my_path/ansible-venv/bin/ansible-playbook playbooks/generate_results/
。
I want to activate it from a bash shell by passing it some arguments, like the following我想通过传递一些参数从 bash shell 激活它,如下所示
/my_path/ansible-venv/bin/ansible-playbook playbooks/generate_results/main.yml --extra-vars="field_A=$value_A field_B=$value_B"
These arguments shall define which is the host that will run the playbook tasks.这些参数应定义运行 playbook 任务的主机。
The playbook is meant to run a query on a database on the addressed host, to generate a csv file with the query results.该剧本旨在对寻址主机上的数据库运行查询,以生成包含查询结果的 csv 文件。
Here below is my playbook.下面是我的剧本。
Is there a way to define the following variables有没有办法定义以下变量
before the task that requires them is run?在需要它们的任务运行之前?
I have put them under the key vars:
, but it is wrong我把它们放在关键的
vars:
下,但这是错误的
How can I do it?我该怎么做?
Note: the db access credentials are saved in another ansible file注意:数据库访问凭证保存在另一个 ansible 文件中
---
vars:
# inputs
#--------
# these variables will be overwritten as extra variables by the bash calling this ansible playbook
value_A: 0 # cannot be left void
value_B: 0 # cannot be left void
# fixed variables
# ----------------------
remote_machine_filepath: '/tmp/'
# input processing
#----------------------
server_target: "{{ 'server-' + value_A.zfill(3) + '_B'+value_B }}"
# generated variables
# ----------------------
csv_filename: "results_{{ value_A }}{{ value_B }}.csv"
copy_query: "\copy (select * from my_table where field_A={{ value_A }} and field_B={{ value_B }}) to '{{ remote_machine_filepath }}{{ csv_filename }}' with delimiter ';' CSV HEADER;"
- hosts:
- {{ server_target }}
order: sorted
gather_facts: False
tasks:
- name: generate csv
ansible.builtin.shell:
cmd: psql -h '{{ db_host }}' -U '{{ db_username }}' -d '{{ db_database }}' -p '{{ db_database_port }}' -tAc "{{ copy_query }}"
register: psql_output # this will be like: COPY X
ignore_errors: true
no_log: true
SOLVED解决了
It was sufficient to give a +1 indent to the whole block of vars
and to add a - where the block starts (I have added a name
keyword to the block) to make it work (same indent level of hosts
)给整个
vars
块一个 +1 缩进并在块开始的地方添加一个 - 就足够了(我在块中添加了一个name
关键字)以使其工作(与hosts
相同的缩进级别)
---
- name: my_playbook
vars:
# inputs
#--------
# these variables will be overwritten as extra variables by the bash calling this ansible playbook
value_A: 0 # cannot be left void
value_B: 0 # cannot be left void
# fixed variables
# ----------------------
remote_machine_filepath: '/tmp/'
# input processing
#----------------------
server_target: "{{ 'server-' + value_A.zfill(3) + '_B'+value_B }}"
# generated variables
# ----------------------
csv_filename: "results_{{ value_A }}{{ value_B }}.csv"
copy_query: "\copy (select * from my_table where field_A={{ value_A }} and field_B={{ value_B }}) to '{{ remote_machine_filepath }}{{ csv_filename }}' with delimiter ';' CSV HEADER;"
- hosts:
- {{ server_target }}
order: sorted
gather_facts: False
tasks:
- name: generate csv
ansible.builtin.shell:
cmd: psql -h '{{ db_host }}' -U '{{ db_username }}' -d '{{ db_database }}' -p '{{ db_database_port }}' -tAc "{{ copy_query }}"
register: psql_output # this will be like: COPY X
ignore_errors: true
no_log: true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.