Error! conflicting action statements: project_path, state (ansible) - ansible

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

Related

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?

Ansible block keep failing on syntax error

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, :-)'

Ansible playbook example code fail to run

I'm started to try Ansible, and using example code from Ansible Documentation. After I try several examples, I get error at the beginning of the code. It says
- name: Change the hostname to Windows_Ansible
^ here(Point at name)"
Any advice would be appreciate.
I tried this one
https://docs.ansible.com/ansible/latest/modules/win_hostname_module.html#win-hostname-module
---
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
The below task will change the hostname of the server. Make sure you run on a test server so that it wont create issues. If you just wanted to test some playbook, use the second playbook with win_command
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Test
win_command: whoami
register: res

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

Ansible playbook for remote copy and script execution

I wish to write a playbook in ansible which will first transfer my package to remote hosts and then run a script. In detail, let's say I have apache package in local machine and need to scp/rsync it to remote nodes A & B. Then I have my script to install the package on A & B both, check whether it was installed properly followed by scrutinizing the config file etc. This script should run only if the transfer is successful.
Have written the below playbook which should meet above requirement. Please confirm if it needs further improvement. Thanks in advance !
Playbook:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result
- name: Show result
debug: msg="{{ result.stdout }}"
...
This way the installation script will only run if the copy tasks changed (was execuded in the process) and exited successfully:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
register: result_copy
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result_run
when: result_copy.changed
- name: Show result
debug: msg="{{ result_run.stdout }}"
...

Resources