I have an ansible script I created that backs up a Databases, Destroys, Creates, and Points the back up to the newly created DB:
- name: Backing up database
command: pg_dump --schema-only --file=image.dump.20100217 image
- name: Destroying Current Database
command: time dropdb image
- name: Creating Database
postgresql_db: name=image
owner='ryan'
- name: Pointing newly created DB to Backed up DB
command: psql image < image.dump.20100217
Everything runs fine until it tries to point to the backed up DB. Ansible doesn't process the < symbol and ultimately crashes:
failed: [192.168.x.x] => {"changed": true, "cmd": ["psql", "image", "<", "image.dump.20100217"], "delta": "0:00:00.003229", "end": "2015-08-19 11:26:21.796224", "rc": 2, "start": "2015-08-19 11:26:21.792995", "warnings": []}
stderr: psql: warning: extra command-line argument "image.dump.20100217" ignored
psql: FATAL: role "<" does not exist
FATAL: all hosts have already failed -- aborting
Does anyone have any suggestions on how to work around this?
Have not tested it but the ultimate trick with problematic characters is to put it into a Jinja expression:
command: psql image {{ "<" }} image.dump.20100217
I can't reproduce this in Ansible 1.9.2. Here is what I tested:
- command: echo psql image < image.dump.20100217
register: x
- debug: var=x
Use the module 'shell' instead of 'command'
---
name: Pointing newly created DB to Backed up DB
shell: psql image < image.dump.20100217
Related
trying to understand expect functionality using below piece of code... I know I can do using becom_user, but I would like to check expect module... since there is requirement where I need to use expect module for automating client app CLI
tasks:
- name: testing expect
expect:
command: sudo su
responses:
<?i>password<?i>: "password\n"
<?i>: ls
timeout: 30
echo: yes
register: exp
- debug: var=exp
fatal: [192.168.153.31]: FAILED! => {"changed": true, "cmd": "sudo su", "delta": "0:00:30.133859", "end": "2021-09-30 00:51:45.538114", "msg": "non-zero return code", "rc": 1, "start": "2021-09-30 00:51:15.404255", "stdout": "[sudo] password for XXXXXX: ", "stdout_lines": ["[sudo] password for XXXXX: "]}
Issue is that <?i>password<?i> regex does not match sudo's prompt. Ansible uses python regex and angle brackets have no special meaning there. If you want case insensetive match use (?i)password instead.
Few sidenotes:
Newline at the end of password string is superfluous. Code works without it.
command: sudo su will hang execution on success as it runs shell. It is more convenient to run something that exits, command: sudo id for example.
I have an ansible task:
- name: Get vault's binary path
shell: type -p vault
register: vault_binary_path
returns
TASK [update_vault : Get vault's binary path] **********************************************************************************************************************************************************************
fatal: [xxxxx]: FAILED! => {"changed": true, "cmd": "type -p vault", "delta": "0:00:00.003303", "end": "2020-04-08 11:37:19.636528", "msg": "non-zero return code", "rc": 1, "start": "2020-04-08 11:37:19.633225", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
but when I run it in shell it returns just fine
[root#ip-xxxxx]# type -p vault
/usr/local/bin/vault
I run ansible as root with become: true. All previous steps are fine up until this one. Any advice appreciated.
Define an update to your PATH in your playbook:
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
...so that /usr/local/bin is guaranteed to be included.
(Also, while when writing bash-specific code type is almost always preferable to which, this isn't such a case, as your shell may be /bin/sh, which isn't guaranteed to support any features that aren't given in the POSIX sh specification. Consider changing to shell: command -v vault, which is guaranteed to work as-intended on all POSIX-compliant shells).
I'm using Ansible 2.8.4-1.el7 in order to automate some tasks. Since I'm starting learning the tool, I'm going through the official documentation most of the times (more specifically, I'm following these examples).
I'm just trying to execute a script that manages the Tomcat instance(s):
- name: Stop service
command: "{{ script_manager }} stop"
args:
chdir: "{{ path_base }}/bin/"
I'm setting those variables beforehand:
path_base: /some/path/tomcat9_dev/
script_manager: tomcat9_dev
...but I'm still getting this error message:
TASK [tomcat : Stop service] ***************************************************
fatal: [tld.server.name]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "cmd": "tomcat9_dev stop", "msg": "[Errno 2] No such file or directory", "rc": 2}
It looks like it's not changing into that directory first to execute the command. Any clues?
I know the location exists, I can SSH into the server(s) and cd into it.
UPDATE: If I do "{{ path_base }}/bin/{{ script_manager }} stop" it works fine. In any case, what's a better approach?
The current directory is not part of the PATH in *nix OS. Changing dir to the correct location is not enough.
If you really want to call your script like this, you have to add {{ path_base }}/bin/ to your user's PATH.
Meanwhile, you can take advantage of chdir by calling your script relative to the current dir:
- name: Stop service
command: "./{{ script_manager }} stop"
args:
chdir: "{{ path_base }}/bin/"
Furtermore, you are IMO on the wrong track: tomcat should be a registered service on the machine and you should manipulate it with the relevant modules: either the generic service or one of the specific systemd, runit,sysvinit...
Example with service:
- name: Make sure tomcat_dev is stopped
service:
name: tomcat_dev
state: stopped
The end goal is for me to copy file.txt from Host2 over to Host1. However, I keep getting the same error whenever I perform the function. I have triple checked my spacing and made sure I spelled everything correctly, but nothing seems to work.
Command to start the playbook:
ansible-playbook playbook_name.yml -i inventory/inventory_name -u username -k
My Code:
- hosts: Host1
tasks:
- name: Synchronization using rsync protocol on delegate host (pull)
synchronize:
mode: pull
src: rsync://Host2.linux.us.com/tmp/file.txt
dest: /tmp
delegate_to: Host2.linux.us.com
Expected Result:
Successfully working
Actual Result:
fatal: [Host1.linux.us.com]: FAILED! => {"changed": false, "cmd": "sshpass", "msg": "[Errno 2] No such file or directory", "rc": 2}
I have the same problem as you,Installing sshpass on the target host can work normally
yum install -y sshpass
I have a ansible playbook which calls 2 roles. role 1 runs on local, which has a script with arg as file path /tmp/inputfile/input.csv. The playbook looks:
- hosts: "{{my_extra_var_IP}}"
connection: local
roles:
- prereq
Roles task:
- name: Copy script to local
copy:
src: files/csv_to_files.sh
dest: /tmp/input_dir/
mode: 0777
- command: ls -ltr /tmp/input_dir
- command: cat /tmp/input_dir/inputFile.csv
#- name: run csv to yml script
# script: /tmp/input_dir/csv_to_files.sh /tmp/input_dir/inputFile.csv
# become_user: niceha
The output of first 2 tasks is success and is as expected but on 3rd & 4th step I get error:
FAILED! => {"changed": true, "cmd": ["cat", "/tmp/input_dir/inputFile.csv"], "delta": "0:00:00.007141", "end": "2017-06-09 15:53:58.673450", "failed": true, "rc": 1, "start": "2017-06-09 15:53:58.666309", "stderr": "cat: /tmp/input_dir/inputFile.csv: No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
I am running this job from tower which uses userA I also tried to change the users but no luck.
The indenting does not look right:
- name: Copy script to local
copy:
src: files/csv_to_files.sh
dest: /tmp/input_dir/
mode: 0777
Ok. So after much reading I got to know the code is fine as it runs from the console but not from the Ansible tower, and just to cross check it worked from other dir paths.
Ansible tower actually uses /tmp/ dir as staging area so any changes/tasks mentioned in the playbook to be run in tmp dir wont take affect.
Changing my input file path from /tmp to /home/user did the work for me.