error conflicting actions statements - ansible

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

Related

How can I manage groups in inventory?

I have a common playbook (task) but in my last task I want to do an action on a group of hosts and when it is finished, do the same action on another group (for my test I simply empty a folder):
- name: test clean folder on hosts
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['c_hosts']
- name: test clean folder on master
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['master’]
I have issue with my inventory file :
/root/ansible_Folder/inventories/inventories.yml :
all:
hosts:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:
Who knows how I can manage my inventories file please?
EDIT :
I tried your solution which seems to me coherent but i have this error message :
ERROR! conflicting action statements: hosts, tasks
The error appears to be in '/root/ansible-cortex-lab/playbooks/roles/cortex/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- hosts: c_hosts
^ here
I just copy past both config file you gave to me.
Do you know why I have this behaviour please ?
Separate the plays, e.g.
- hosts: c_hosts
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
- hosts: master
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
The module file can't simply clean a folder. Instead, remove a folder and create it again.
To fix the inventory, remove the 2nd line hosts:. See Inventory basics: formats, hosts, and groups
all:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:

How to run a command on localhost to define variable for an Ansible playbook?

I'm very new to Ansible and trying to figure things out. I have a simple playbook to run on a remote host. To simplify drastically:
- hosts: all
name: build render VM
tasks:
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root
I would like to have project_id set to the output of this command, run on localhost: gcloud config get-value project. Ideally I'd like that to be stored into a variable or fact that can be used throughout the playbook. I know I can pass project_id=$(...) on the ansible cmd line, but I'd rather have it set up automatically in the playbook.
Taking for granted the given command only returns the id and nothing else.
With a task delegated to localhost:
- hosts: all
name: build render VM
tasks:
- name: get project id
command: gcloud config get-value project
register: gcloud_cmd
run_once: true
delegate_to: localhost
- name: set project id
set_fact:
project_id: "{{ gcloud_cmd.stdout }}"
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root
With a pipe lookup:
- hosts: all
name: build render VM
tasks:
- name: set project id from localhost command
set_fact:
project_id: "{{ lookup('pipe', 'gcloud config get-value project') }}"
run_once: true
- copy:
src: ./project_{{ project_id }}.yaml
dest: /app/project.yaml
owner: root

Ansible can't run when vars are used in include_tasks

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?

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

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

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

Resources