The below syntax works fine.
---
- hosts: all
tasks:
- name: run this command and ignore the result
shell: echo "The server is UP since " `uptime`
However when i change shell module to command module I was expecting this to still work.
command: echo "The server is UP since " `uptime`
But it does not print the uptime value with the command module.
Can I not have the same syntax to work with both the shell as well as the command module ?
Can I not have the same syntax to work with both the shell as well as the command module ?
Yes, of course, by just manually doing the job that shell: is going to do and wrapping the string in sh -c
- set_fact:
the_command: sh -c 'echo "The server is UP since `uptime`"'
- command: '{{ the_command }}'
- shell: '{{ the_command }}'
Related
I am trying to emulate this behavior with Ansible raw command but I could not find any feature that achieve this
ssh user#host.com <<EOF
command
exit
EOF
You are simply sending the script:
command
exit
To the remote host. The <<EOF and EOF parts are parsed by your local shell and aren't part of the command. The equivalent ansible task would be:
- raw: |
command
exit
In most cases (if the remote target is running a semi-standard shell), you won't need the exit either; the script will exit after the last command completes.
You don't need to send a multiline commands via ssh, perhaps you have connected with ssh already with ansible when you set ansible_connection variable, e.g. in your inventory file:
[my_host_group]
host.com
[my_host_group:vars]
ansible_connection=ssh
ansible_become_user=root
ansible_ssh_user=user
Then execute a tasks with bash:
- name: Executing multiline command on host.com under user
ansible.builtin.shell: command
delegate_to: "{{ groups['my_host_group'][0] }}"
become: False
Or just use ansible.builtin.command module instead of ansible.builtin.shell if your command is simple and not multi line.
You don't need an exit at the end of your script either (until you want to change an exit code and return them to ansible). 'Failed when' conditions is your firend:
- name: Executing multiline command on host.com under user
ansible.builtin.shell: command
delegate_to: "{{ groups['my_host_group'][0] }}"
register: your_script_results
ignore_errors: True
become: False
- name: Print an exit code on script error
ansible.builtin.debug:
msg: "Script was failed with {{ your_script_results.rc }} exit code"
when: your_script_results.failed
I can run the script with the command line argument on the linux server it works fine.
for e.g.: ./install.sh -n -I <IP address of the server>
The above command is able to install the script on the server.
When I am trying to do via ansible (version 2.5) playbook using the shell module it gives me an argument error.
- name: Running the script
shell: yes | ./fullinstall
Expect modules has been tried.
--my-arg1=IP address
- shell: "./install.sh -n -I"
args:
chdir: somedir/
creates: somelog.txt
You can look here for examples.
You can also place the install.sh file on the server as a template. Then you can set the variables as desired in Jinja2.
- name: Template install.sh
template:
src: /install.sh.j2
dest: /tmp/install.sh
- shell: "cd /tmp/ ; ./install.sh
Your install.sh.j2 contains:
IP adres: {{ my_ip }}
And set the variable on the command line with:
ansible-playbook -e my_ip="192.168.0.1"
Use command module
- name: run script
command: /path/to/install.sh -n -I {{ ip_addrress }}
playbook
ansible-playbook -e ip_address="192.168.3.9" play.yml
If you want to interactively wanted to enter the IP address, use prompt module.
I'm quite new to Ansible so I'm still in learning curve. I'm looking for a way to retrieve a value from script's variable and use it further along with ansible-playbook command.
Saying I have a script I would like to retrieve $hostname info in target node. The script is run in a playbook. When a $HOSTNAME value returns, how can I pass it to my wrapper script so I can reference it with other list?
The script is as simple as follows:
HOSTNAME=$(hostname)
ECHO "$HOSTNAME"
Assuming you are running a script with ansible in one task, you would register the output:
tasks:
- name: Echo value
command: "echo Hello"
register: command_output
Then in your next task, maybe you want to create a file for the hostname:
- shell: "touch {{ item }} "
with_items:
- "{{ command_output.stdout_lines }}"
That's the basic structure - you don't say what command you want to run, but this should get you started.
Here's a nice tutorial: http://www.mydailytutorials.com/ansible-register-variables/
I would like to set output of a shell command as an environment variable in Ansible.
I did the following to achieve it:
- name: Copy content of config.json into variable
shell: /bin/bash -l -c "cat /storage/config.json"
register: copy_config
tags: something
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config}}"
tags: something
But somehow after the ansible run, when I do run the following command:
echo ${TEMP_CONFIG}
in my terminal it gives an empty result.
Any help would be appreciated.
There are at least two problems:
You should pass copy_config.stdout as a variable
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config.stdout}}"
tags: something
You need to register the results of the above task and then again print the stdout, so:
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config.stdout}}"
tags: something
register: shell_echo
- debug:
var: shell_echo.stdout
You never will be able to pass the variable to a non-related process this way. So unless you registered the results in an rc-file (like ~/.bash_profile which is sourced on interactive login if you use Bash) no other shell process would be able to see the value of TEMP_CONFIG. This is how system works.
I have an issue with a shell command in my Ansible script. This shell command doesn't work properly on the script, while it works great directly on the server.
Here is the problematic code:
- name: export keys
shell: "export NR_INSTALL_PATH=$(which php | sed 's/\/php$//g') ; export NR_INSTALL_KEY='somekey'; export NR_INSTALL_SILENT=1; newrelic-install install"
After I investigated the issue, I have noticed that the problematic part is:
which php | sed 's/\/php$//g'
This code suppose to get the executable path of PHP, and remove the last directory from the path. Instead of the expected result, i get only the first directory of the path (/bin).
Is that issue related to escape characters? Can someone tell me how to fix it?
Do I have more characters from the original command that I need to escape?
Is there a way to print the bash command that actually has been received by the server? (That's how I will be able to know what has been changed from the original command).
This syntax should work:
- name: export keys
shell: 'export NR_INSTALL_PATH=$(which php | sed "s/\/php$//g") ; export NR_INSTALL_KEY="somekey"; export NR_INSTALL_SILENT=1; newrelic-install install'
For example:
- set_fact:
php_path: /usr/bin/php
- shell: 'export NR_INSTALL_PATH=$(echo {{ php_path }} | sed "s/\/php$//g") ; echo $NR_INSTALL_PATH'
register: result
- debug: var=result.stdout
returns:
TASK [debug] *******************************************************************
ok: [localhost] => {
"result.stdout": "/usr/bin"
}
Is there a way to print the bash command that actually has been received by the server?
Not an Ansible way (because it is oblivious to how shell interprets the command), but you can run the shell with -x option enabled:
- command: /bin/bash -x -c 'export NR_INSTALL_PATH=$(echo {{ php_path }} | sed "s/\/php$//g") ; echo $NR_INSTALL_PATH'
register: result
- debug: var=result.err
And you will get a full trace in the log.
Notice: you asked about "bash", but it doesn't necessarily have to be bash - Ansible might run /bin/sh by default.