简体   繁体   中英

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,
it is named main.yml , placed at path /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

/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.
The playbook is meant to run a query on a database on the addressed host, to generate a csv file with the query results.

Here below is my playbook.

Is there a way to define the following variables

  • value_A
  • value_B
  • server_target
  • csv_filename
  • csv_filename

before the task that requires them is run?

I have put them under the key vars: , but it is wrong

How can I do it?

Note: the db access credentials are saved in another ansible file

---


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 )

---
  - 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM