ERROR! 'copy' is not a valid attribute for a Play - ansible

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

Related

Trying to split my Ansible playbook based on the Operating System

I have a playbook which needs to be run based on the operating System.
UseCase: Lets assume there is a service that is running.
On Linux we can check if it is installed and running using the
systemctl status application.service
While command and on windows we will be using the
sc query "ServiceName" | find "RUNNING"
Now we have to install it based on the output of the above a commands which requires us to segregate the playbook based on the OS.
Classic Example: Create a directory based on the OS
- name: Install QCA Agent on Linux targets
hosts: all
gather_facts: true
remote_user: root
tasks:
- name: Create Directory for Downloading Qualys Cloud Agent
sudo: yes
sudo_user: root
file:
path: /usr/q1/
state: directory
owner: root
group: root
mode: 0777
recurse: no
- name: Create Directory for Downloading Qualys Cloud Agent
win_file:
path: c:\q1
state: directory
owner: Administrator
group: Administrator
mode: 0777
recurse: no
The playbook will alwayz be successful only if one of the condition is met and it is whether it is Windows or Unix OS. I can alwayz add a condition which will prompt based on:
when: ansible_distribution == 'Redhat' or ansible_distribution == 'CentOS'
However what i would like to achieve is based on a condition it should trigger my playbook.yml file.
name: Load a variable file based on the OS type, or a default if not found. Using free-form to specify the file.
include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yaml"
- "{{ ansible_os_family }}.yaml"
- default.yaml
https://docs.ansible.com/ansible/2.5/modules/include_vars_module.html?highlight=with_first_found
I would like to know if there is a better example explaining the same that i could implement or if there are other ways to achieve the same.
Thank you,
The example you show from the Ansible docs is pretty much the best practice and is common in many playbooks (and roles for that matter) that deal with multiple OSes. If you have code that is different (instead of the variable example here), you'll be using include_tasks instead of include_vars, but the concept is the same.

Ansible playbook syntax error with tasks:

I just started experimenting with ansible and I am trying to write my first simple playbook.
But I am getting a syntax error with the task keywork,
---
name: add ansible user
hosts: all
become: true
become_method: sudo
become_user:root
tasks:
- user:
name: ansible
groups: ansible
When I run this get the following:
utility:~/scripts/ansible# ansible-playbook --check add-ansible-user.yml
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/root/scripts/ansible/add-ansible-user.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
^ here
From searching I belive the best bet is that I have an indent problem, but nomatter how I have tried to change it up, I cant get it too work.
- name: add ansible user
hosts: all
become: true
become_method: sudo
become_user: root
tasks:
- user:
name: ansible
groups: ansible
It's indeed the indentation problem. Please try the code written above.
Facing same issue, by making correct Indent spacing able to resolve it
As ref below
---
- name: my ansible
hosts: webserver
remote_user: root
become: true
tasks:
- name: intall httpd
yum:
name: httpd
state: latest
- name: run httpd
service:
name: httpd
state: started
- name: create content
copy:
content: “Congratulation on installing ansible”
dest: /var/www/html/index.html
The problem is here:
become_user:root
You need a space between : and root
become_user: root

last modified files only moved ansible?

my requirement is moving files to remote host using ansible playbook.
my ansible script
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/home/bu/Bilal/site dest=/tmp owner=root group=root mode=777
when run playbook it has moved the file to remote.
when I have ran playbook again it will overwrite the whole folder again. I am looking, what are the files I have modified that files only get overwrite because my folder size is too large its taking so much time even single file change.
Take a look at the Synchronize module:
Uses rsync to make synchronizing file paths in your playbooks quick and easy.
Example:
- name: Sync files
synchronize:
src: "{{ conf.dev_path }}/"
dest: "{{ conf.host_path }}"
delete: yes
rsync_opts:
- "--exclude=.*"

ansible synchronize with excludes file role

Im creating an ansible role for deploying laravel5 project,
now I do that with a "synchronize" (rsync)
I have my excludes file for the rsync, and the files structured like this:
the: "deploy-laravel5" role:
files
excludes
tasks
main.yml
now here is the tasks in main.yml:
- name: deploy laravel projects
synchronize:
src: "{{item.src}}"
dest: "{{item.dest}}"
rsync_opts:
- "--exclude-from=excludes"
with_items: "{{projects}}"
some playbook:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/", dest: "/web/boom/", envFile: "twitter.env"}
roles:
- deploy-laravel5
now when I run that, ansible says it can't find the "excludes" file
msg: rsync: failed to open exclude file excludes: No such file or directory (2)
I tried many different paths but nothing, any ideas how to point to the excludes file?
After I browsed the web and the docs quite extensively, I found that you can't define othe files in the "role" only files for templating or copying.
But you can define the file inside a playbook, so now the file structure is like this:
roles/
deploy/ (NOT "deploy-laravel5" as before)
playbooks/
deploy-laravel5/
excludes-file
deploy-playbook.yml
and the playbook looks quite the same:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/",
dest: "/web/boom/",
excludes-file: "path/to/excludes/file",
envFile: "twitter.env"}
roles:
- deploy

error conflicting actions statements

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:\'

Resources