I'm running a playbook which has the following step:
environment:
ORACLE_HOME: {{ ORACLE_HOME }}
tasks:
- name: run sqlplus
shell: |
sqlplus "{{ db_user }}/{{ db_password }}#{{ sid }}" #test.sql
The host server and the server which is running the playbook have Oracle installed correctly and also set ORACLE_HOME. The above sqlplus command workes on the host server, but via ansible getting:
"stderr": "/bin/sh: sqlplus: command not found"
How can I run the above?
Thank you
Related
I am trying to run a batch file with an argument on a remote windows machine from my local Cygwin console using Ansible. I have successfully configured ssh to a remote windows machine using OpenSSH. If I run this ansible command, I get this output.
ansible -i inventory.yml -m win_ping windows_server1 -vvv
windows_server1 | SUCCESS => {
"changed": false,
"invocation": {
"module_args": {
"data": "pong"
}
},
"ping": "pong"
}
My inventory file is as below.
---
all:
hosts:
windows_server1:
ansible_connection: ssh
ansible_shell_type: cmd
ansible_host: "host1"
ansible_user: "user1#DOMAIN"
Now when I add a playbook to execute a batch file with arguments, I get an error. Tried script, win_command, win_shell, win_psexec modules with no luck. Here is the code in my playbook.
---
- hosts: windows_server1
become: yes
become_method: runas
become_user: user1
tasks:
- name: Invoke App stop script
win_command:
cmd: '"D:\Temp\appstop.bat" "argument 1" -stop'
Here is the ansible command I used.
ansible-playbook -i inventory.yml playbook-01.yml -vvv
Here is the error I am getting now.
fatal: [windows_server1]: FAILED! => {
"changed": false,
"msg": "Get-AnsibleParam: Missing required argument: _raw_params" }
Appreciate any hints to proceed with this. Thanks
Try the following code snippet
- name: Run a bat script on remote windows system after successful ssh using ansible
hosts: windows
tasks:
- name: Run a bat script on remote windows system after successful ssh using ansible
win_shell: C:\Users\test\test.bat
register: result
ignore_errors: yes
- name: Display output
debug:
var: result.stdout_lines
I am writing a playbook for my Application tomcat node It will copy, deploy and stop/start tomcats.
I have a hop box serverA, another hop box serverB and tomcat node tomcatC. Manually using putty i use below steps to get on to the tomcat
Login to serverA using userId1
ssh to serverB using userId2
ssh to tomcatC using userId1
sudo to tomcat user.
Also I am able to directly ssh to tomcatC from serverA and my Ansible master is also serverA from where I am running the playbooks.
How do i run my playbook for this? Below is my playbook i am using as of now but it's not working.
ansible-playbook -i my-inventory my-V3.yml --tags=download,copy,deploy -e release_version=5.7 -e target_env=tomcatC -u userId1 --ask-pass. AND my-v3.yml looks like below -
hosts: '{{ target_env }}'
#serial: 1
remote_user: userId1
become: yes
become_user: tomcat
Getting this Error NOW -
GATHERING FACTS ***************************************************************
fatal: [tomcatC] => Missing become password
You can set the user a command is run as like so:
- cron:
name: "Clear Root Mail"
minute: "0"
hour: "22"
job: "sudo rm /var/spool/mail/root"
user: myuser
Or use become: true like so:
- name: Start Server
shell: "nohup /home/myuser/StartServer.sh &"
become: true
You can have shell scripts run the commands you need to run as well that ansible calls from the jump box you have. Your problem looks like you dont have the correct ssh key applied though.
Required some help on Ansible script
Login and executing some samples required using Ansible playbook.
No need to install PostgreSQL.
We tried with below command but it's not working.
- name: Login to DB and run command
command:PGPASSWORD='{{pgpass_filepath}}'; psql -U "{{ db_user }}" -d "{{ db_name }}" -h "{{ db_host }}" -p 5555-c "select count(*) from student";
You need to keep in mind that you have to escape the bracket characters, also the ; at the end of the sql statement needs to be inside the escaped brackets. I would also advise you to let ansible execute the shell command with a white space at the beginning so the command with the password won't be logged to the shell history.
- name: Login to DB and run command
shell: " PGPASSWORD='{{pgpass_filepath}}' psql -U {{ db_user }} -d {{ db_name }} -h {{ db_host }} -p 5555 -c \"select count(*) from student;\""
This should work. Give it a try.
In some ansible script I'm geting a
rsync: mkdir "/var/www/xxx" failed: Permission denied
I need to check what is the user my ansible is using in the target VM.
How can I print the user with a debug: line ?
I look for something like the $ id unix command to debug the permission Pb.
Ansible will always default to the current user(in the shell) and if you want to connect to a remote machine using a different user, you can use the remote_user in your ansible playbook.
See: http://docs.ansible.com/ansible/intro_configuration.html#remote-user for more details.
If you want to run a shell command and capture the output:
- name: "Run a shell command"
shell: /usr/bin/id
register: result
- name: Print the value of result
debug: var=result
or
- name: Print the user id using the ansible_user_id fact
debug: msg="{{ansible_user_id}}"
I have tried connecting to a remote "custom" Linux vm, and copied my ssh public ssh it, yet, I'm able to get Ansible to ping / connect it, as I keep getting remote host unreachable. "I think its because its running custom version of linux and ssh auth might be behaving differently"
The following remote ssh command works
# ssh -t user#server_ip "show vd"
password: mypassword
I'm trying to convert this above command to an Ansible playbook
---
- hosts: server_ip
gather_facts: no
remote_user: root
tasks:
- name: check devise status
shell: ssh -i server_ip "show vd"
register: output
- expect:
command: sh "{{ output.stdout }}"
responses:
(?i)password: user
I'm unable to get it to work, and I'm not sure if this is the write way of doing it, your input is highly appreciated it.
Whenever you have ssh connection problems you should add the -vvvv parameter to your ansible-playbook command line. That way it will give detailed information about the ssh connection and errors.