i try to download from s3 and when i fail i like to do another task
i read in the doc that i can use : block/rescue
https://docs.ansible.com/ansible/2.7/user_guide/playbooks_blocks.html
so im doing this which as stand alone task is working fine
---
- name: s3 Handler
gather_facts: false
hosts: localhost
connection: local
tasks:
- name: Handle the error
block:
- name: Get S3 key
module: aws_s3
bucket: dist
object: packages/foo.zip11
dest: /home/foo.zip
mode: get
overwrite: different
rescue:
- debug:
msg: 'I caught an error, can do stuff here to fix it, :-)'
but here im getting syntax error :
The error appears to have been in '/home/create_s3.yml': line 12, column 19, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Get S3 key
module: aws_s3
^ here
Looking at the S3 module from Ansible, it looks like you could replace module: aws_s3 with aws_s3.
Also, you want to align the indentation so aws_s3 is at the same level as name since they are in the same lexical scope. YAML tends to be very picky about indentation.
Applying this to your code snippet, we get...
---
- name: s3 Handler
gather_facts: false
hosts: localhost
connection: local
tasks:
- name: Handle the error
block:
- name: Get S3 key
aws_s3:
bucket: dist
object: packages/foo.zip11
dest: /home/foo.zip
mode: get
overwrite: different
rescue:
- debug:
msg: 'I caught an error, can do stuff here to fix it, :-)'
Related
Trying to execute this code but having the said errors encountered above
code
---
- hosts: localhost
connection: local
collections:
- community.general.terraform
tasks:
- name: Execute Terraform Template
project_path: '/Users/<username>/Desktop/<repository>/<file>'
state: present
force_init: true
The offending line appears to be:
tasks:
- name: Execute Terraform Template
^ here
been trying to figure this one out.. I am using a macOS, installed Ansible locally already.
Thank you in advance!!
Trying to execute the code above but unable to succeed.
You have an indentation problem in your script.
Should work like this:
- name: Run Terraform
gather_facts: No
hosts: localhost
tasks:
- name: Execute Terraform Template
terraform:
project_path: '/Users/<username>/Desktop/<repository>/<file>'
state: present
force_init: true
I have two playbooks driver.yaml invoking another play after converting from template called main_driver.yml
Main playbook driver.yml
---
- name: Setup Services
hosts: all
gather_facts: no
vars_files:
- var_input_driver.yaml
tasks:
- name: Converting template to playbook
template:
src: template_for_driver.yaml.j2
dest: main_driver.yaml
delegate_to: localhost
- name: run roles
include_tasks: main_driver.yaml
Content of main_driver.yaml
---
- name: Setup Services
vars:
java_package: java-11-openjdk
dock_version: 18.09.0
- name: Setup services
include_role:
name: configure-java
Receiving error as below:
FAILED! => {"reason": "no module/action detected in task.\n\nThe error appears to be in 'main_driver.yaml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Setup Services\n ^ here\n"}
vars is not a module name but a task option. You should fix your included file like so:
---
- name: Setup services
include_role:
name: configure-java
vars:
java_package: java-11-openjdk
dock_version: 18.09.0
An other possible way would be:
---
- name: Set variables
set_fact:
java_package: java-11-openjdk
dock_version: 18.09.0
- name: Setup services
include_role:
name: configure-java
couple of things:
main_driver.yaml seems to include itself (main_driver) - that probably won't work.
the task file used with "include_tasks" should be a "pure" task file, not a playbook. In other words, it should not contain a playbook header (hosts, name, etc), but simply a list of tasks.
Im struggling to understand the logic in your example, maybe double-check that the file contents are correct?
I am trying to make ansible playbook that connects to the server via ssh and sends a file.
Most of my google search yield no concrete results.
-
become: true
hosts: all
remote_user: artur
tasks: ~
-
copy:
dest: /home/artur/grep_error.py
group: UnixUsers
mode: 420
owner: artur
src: /Users/artur/Desktop/sublime/projects/scripts/grep_error.py
name: "example copying file with owner and permissions"
I expect to copy the file over to the ssh server.
Take Y minutes to learn yaml. Pay particular attention to the fact that indentation and new lines are syntactically significant
Install yamllint and validate your yaml files. It will save you a lot of precious time
Install ansible-lint and validate your files again. This one will go over the particular ansible syntax and watch for good practice
Read the doc about playbooks and make sure you respect the syntax (i.e. understand the errors you get from valiators above).
Now I gave you some references, here is a correction of your playbook
---
- name: My first play to copy files
become: true
hosts: all
remote_user: artur
tasks:
- name: Example copying file with owner and permissions
copy:
src: /Users/artur/Desktop/sublime/projects/scripts/grep_error.py
dest: /home/artur/grep_error.py
owner: artur
group: UnixUsers
mode: 0420
- name: I'm just a dummy task to show you a play can go on
debug:
msg: I'm a dummy task
I have a json file in the same directory where my ansible script is. Following is the content of json file:
{ "resources":[
{"name":"package1", "downloadURL":"path-to-file1" },
{"name":"package2", "downloadURL": "path-to-file2"}
]
}
I am trying to to download these packages using get_url. Following is the approach:
---
- hosts: localhost
vars:
package_dir: "/var/opt/"
version_file: "{{lookup('file','/home/shasha/devOps/tests/packageFile.json')}}"
tasks:
- name: Printing the file.
debug: msg="{{version_file}}"
- name: Downloading the packages.
get_url: url="{{item.downloadURL}}" dest="{{package_dir}}" mode=0777
with_items: version_file.resources
The first task is printing the content of the file correctly but in the second task, I am getting the following error:
[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this
will be a fatal error.. This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
You have to add a from_json jinja2 filter after the lookup:
version_file: "{{ lookup('file','/home/shasha/devOps/tests/packageFile.json') | from_json }}"
In case if you need to read a JSON formatted text and store it as a variable, it can be also handled by include_vars .
- hosts: localhost
tasks:
- include_vars:
file: variable-file.json
name: variable
- debug: var=variable
for future visitors , if you are looking for a remote json file read. this won't work
as ansible lookups are executed in the local
you should use a module like Slurp
My playbook appears as follows:
hosts: localhost
tasks:
name: Get the build synchronize:
mode=pull src=jenkins_server_ip:/home/capsilon/Jenkins/trunk/builds/{{item}}/ dest=/home/builds/{{item}}
with_items:
['as2-client', 'amc-gateway', 'router']
hosts: localhost
tasks:
name: Zip and send
command: /bin/sh "/home/zipfile.sh"
hosts: windows
tasks:
name: Deployment
win_get_url:
url: 'http://server_ip/builds/build.zip'
dest: 'D:\build.zip'
win_unzip:
src: D:\build.zip
dest: D:\
Get the following error:
ERROR! conflicting action statements
The error appears to have been in '/etc/ansible/playbooks/new_logic_zip.yaml': line 16, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Deployment
^ here
What is the error here??
Using latest git developer code. Any help would be really appreciated.
It needs to have quotes around the src and dest on the win_unzip task which also need to be defined separate to win_get_url:
- name: Deployment
win_get_url:
url: 'http//server_ip/builds/build.zip'
dest: 'D:\build.zip'
- win_unzip:
src: 'D:\build.zip'
dest: 'D:\'