简体   繁体   English

如何在 ansible 剧本中将目标服务器将运行任务的主机名字符串定义为变量?

[英]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有没有办法定义以下变量

  • value_A值_A
  • value_B值_B
  • server_target服务器_目标
  • csv_filename csv_文件名
  • csv_filename csv_文件名

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.

相关问题 Ansible-在Playbook执行期间更新环境变量 - Ansible - Update env variables during playbook execution 如何确保可笑的剧本完全运行 - How to make sure an ansible playbook runs completely 如何使用Ansible Playbook将变量放入Bash脚本 - How to put variable in bash script with ansible playbook 在 Ansible 中的任务之间重用环境变量 - Reusing environment variables between tasks in Ansible 如何将环境变量放入cloud-init运行的Ansible脚本中? - How to get environment variables into Ansible script run by cloud-init? 如何使用ansible运行需要中断操作的shell文件 - How to use ansible to run a shell file which needs an interrupt action 如何在 Ansible playbook 中为每个客户端设置不同的环境变量 - How to set different environment variable for each client in Ansible playbook 如何使用 Ansible-playbook 在磁盘的“/etc/fstab”中进行 append 条目? - How to append entries in `/etc/fstab` of disks using Ansible-playbook? 如何正确使用 ansible 主机名模块? - How do I correctly use the ansible hostname module? Ansible:与docker相关的命令在playbook中不起作用,但是一旦直接运行就起作用 - Ansible: docker related command doesn't work inside playbook, but does work once run directly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM