Ansible: How to add variables to "command" or "shell"

Is it possible to use variables on command or shell modules? I have the following code, and I would like to use variable file to provide some configurations:

I would like to read the Hadoop version from my variables file. On other modules of ansible I could use {{ansible_version}}, but with command or shell it doesn't works.

- name: start ZooKeeper HA
  command: hadoop-2.7.1/bin/hdfs zkfc -formatZK -nonInteractive

- name: start zkfc
  shell: hadoop-2.7.1/sbin/hadoop-daemon.sh start zkfc

I would like to convert to the following:

- name: Iniciar zkfc
  command: {{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc

Because if I run the with this syntax it throws this error:

- name: inicializar estado ZooKeeper HA
  command: {{hadoop_version}}/bin/hdfs zkfc -formatZK -nonInteractive
                             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

I have try using, but same problem:

- name: Iniciar zkfc
  command: "{{ hadoop_version }}"/sbin/hadoop-daemon.sh start zkfc

What is the correct syntax?

Quote the full string in the command argument:

- name: Iniciar zkfc
  command: "{{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc"

Variables — Ansible Documentation, Variable Precedence: Where Should I Put A Variable? Variable Scopes; Variable Examples; Advanced Syntax. While automation exists to make it easier to make� Is it possible to use variables on command or shell modules? I have the following code, and I would like to use variable file to provide some configurations: I would like to read the Hadoop version from my variables file. On other modules of ansible I could use {{ansible_version}}, but with command or shell it doesn't works.

The syntax error is because you started a value with {. If you begin a value with a variable like so:

command: {{ my_var }}

then you must quote the whole line:

command: "{{ my_var }}"

This is due to the parser not being able to distinguish between YAML's dictionary syntax and variable interpolation otherwise.

include_vars – Load variables from files, dynamically within a task , Variable Precedence: Where Should I Put A Variable? Variable Scopes; Variable Examples; Advanced Syntax. While automation exists to make it� These variables are set on a host-by-host basis which is just like ansible facts, discovered by setup module. These variables are available to subsequent plays in a playbook. These variables are set with precedence but as per the standard ansible precedence, many other types of variables have a higher priority. So this value may be overridden.

I have done below steps to replacing the inventory hostname in ansible shell module

- name: 'Task-10 Modif the splunk input.conf file with Actual hostname'

  become: yes
  become_method: sudo
  become_user: root
  shell: |
    /splunk/splunkforwarder/bin/splunk enable boot-start --accept-license --answer-yes --no-prompt -user splunk
    declare wfqdn 
    wfqdn=$(uname -n)
    sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf
  tags:
    - always

The Main play is :

sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf

How to Use Variables in Ansible Playbook, name: Include vars of stuff.yaml into the 'stuff' variable (2.2). include_vars: file: stuff.yaml name: stuff - name: Conditionally decide to load in variables into 'plans' � The main variable types are Ansible Facts variables and User-defined variables. Ansible Facts variables: Depending on the host Ansible is working on, Ansible generates Ansible facts variables. Ansible facts variables contains information about the host, such as IP addresses, hostname, domain name, date, time, shell environment variables, and more.

How to Include Variables in Ansible + Examples, Examples of acceptable variable names include: turntable; turn_table; turntable01; turntable_01. The names below do not qualify as valid� Ansible variables help to determine how the tasks execute on different systems based on the values assigned to these variables. Ansible variable names should be letters, numbers, underscores and they should always start with a letter. Basic Ansible Variable example. In the playbook, we can give a variable in ‘variable_name: variable_value

How can I pass variable to ansible playbook in the command line , I'm aware of two ways to include variables from another file in Ansible: The include_vars module (works in any list of tasks, such as a playbook or role); The� Well, we could parse some configuration files and extract it that way like in a shell script, but that’s tedious and prone to errors. Ansible conveniently provides a lot of predefined variables of the machine and it makes our lives easier. To view all of the variables, add this task: - name: Display Host Variables debug: var=hostvars

How to Work with Ansible Variables and Facts - Part 8, Reading the docs I find the section Passing Variables On The Command Line, that give this sample: ansible-playbook release.yml --extra-vars� Variable "devices" holds just one value: "AristaSwitch03". To make Ansible append to the list, instead of ovewriting the last value, we'll make a few modification. We will initiate "devices" var, assigning an empty list to it. We will also change the assignment expression to make Ansible append to this variable.

Comments
  • Would be nice if it threw some sort of error or warning when the whole line not being quoted causes it to not interpret the variable format, instead of just failing silently.