I have started to learn Ansible watching online vids . But got stuck at the first step when creating and running a simple playbook.
When I run below playbook as below ->
$ ansible-playbook download.yml
Then output displayed is ->
ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to have been in '/etc/ansible/download.yml': line 3, column 11, but may
be elsewhere in the file depending on the exact syntax problem. The offending line appears to be:
-name: Test
get_url:
^ here
Below are the contents of my playbook download.yml ->
tasks:
-name: Test
get_url:
url: https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
dest: /home/sunny/ec2.ini
mode: 700
What am I doing wrong here ?
Space behind the dash in name is missing. Instead of
-name: Test
the correct syntax is
- name: Test
Related
Worked on another machine with Ansible 2.9.27. It is installed on a new machine ansible [core 2.12.2].I assume that the problem is the difference in versions.
[root#localhost ansible]# ansible-playbook test.yml
ERROR! couldn't resolve module/action 'synchronize'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/root/ansible/test.yml': line 4, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Start copy scripts
^ here
Playbook contents.
---
- hosts: gate_test
tasks:
- name: Start copy scripts
synchronize:
src: ~/ansible/build/base_ca
dest: ~/
mode: push
The task of the playbook, transferring a directory with contents from a local machine to a virtual one
I use modules from the collection netapp.ontap in my ansible playbook which works perfectly fine when run using ansible-playbook command.
However, when run from AWX, it fails to detect the collection and immediately throws an errors that it cannot detect the ansible module/collection.
I even tried to re-install the collection from the playbook itself but with no luck.
The ansible collection is confirmed to be installed as it already works fine when run outside AWX.
The host is running ansible 2.10.4.
Here is my playbook:
---
- hosts: all
gather_facts: yes
collections:
- netapp.ontap
tasks:
- name: Install Netapp Collection from Ansible Galaxy
shell: ansible-galaxy collection install netapp.ontap
- name: Run Task
import_tasks: tasks/hil.yml
Task:
- name: 'Gather SVMs'
netapp.ontap.na_ontap_info:
state: info
hostname: "{{ netapp_hostname }}"
username: "{{ netapp_username }}"
password: "{{ netapp_hv_password }}"
gather_subset:
- vserver_info
Error from AWX:
SSH password:
Vault password:
ERROR! couldn't resolve module/action 'netapp.ontap.na_ontap_info'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/tmp/awx_421_gey54bdw/project/tasks/hil.yml': line 6, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: 'Gather SVMs'
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
Update:
I created a collections/requirements.yml file, with the below details but now AWX fails the task itself.
collections/requirements.yml
collections:
name: https://github.com/ansible-collections/netapp.git
type: git
Error:
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1279, in run self.pre_run_hook(self.instance, private_data_dir) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1862, in pre_run_hook sync_task.run(local_project_sync.id) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 698, in _wrapped return f(self, *args, **kwargs) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1444, in run raise AwxTaskError.TaskError(self.instance, rc)
Exception: project_update 435 (failed) encountered an error (rc=2), please
see task stdout for details.
I am building some role in handlers/main.yml I had spacified some handler jobs but I was unable to execute them. This is the error message:
ERROR! The handlers/main.yml file for role 'sample-mysql' must contain
a list of tasks
The error appears to be in '/home/automation/plays/roles/sample-mysql/handlers/main.yml': line 2, column 3, but maybe somewhere else in the file depending on the exact syntax problem.
The offending line appears to be:
---
handlers:
^ here
I had done some changes but still not working. I also want this file to load handlers from another file. Is it possible to do so? such as - include: directive
---
handlers:
- name: "Start mysql"
service:
enabled: true
name: mysqld
state: started
- name: "Start firewalld"
service:
enabled: true
name: firewalld
state: started
ERROR! The handlers/main.yml file for role 'sample-mysql' must contain
a list of tasks
The error appears to be in '/home/automation/plays/roles/sample-mysql/handlers/main.yml': line 2, column 3, but maybe somewhere else in the file depending on the exact syntax problem.
The offending line appears to be:
---
handlers:
^ here
In your role, there is file: handlers/main.yml.
Edit that file, and add this task:
---
- name: a task
shell: echo
save it
The handler file location is /repo_name/roles/sample-mysql/handlers/main.yaml
Inside the main.yaml add the below contents.
---
- name: Start mysql
service:
name: mysqld
state: started
enabled: true
To call this handler include the below line in your main playbook, /repo_name/roles/sample-mysql/tasks/main.yaml
notify: "Restart iis service"
You cannot simply execute a handler. Inside the sample-mysql folder, there should be folders called tasks and handlers.
If you use a separate file like handlers/main.yml, you must omit the keyword "handlers:". You will only need that keyword, if you put all in one role file.
I got this playbook to run on a VM running Ansible 2.3.1, with no errors, however, I am running into problems with the very first character in other versions 2.3.0 and older and I am NOT seeing where I am going wrong.
The format exactly follows the examples given in the documentation for cl-license, and this is pretty vanilla in that there is no logic or conditional statements or anything to this YAML.
---
- hosts: accton_as6712_32x
tasks:
- name: install_license_for_6712
cl_license:
src: "http://10.43.255.182/cumulus/license-4x5712-4x6712.txt"
- hosts: accton_as4610_54
tasks:
- name: install_license_for_4610
cl_license:
src: "http://10.43.255.182/cumulus/license-2x4600.txt"
- hosts: mlnx_x86_MSN2410B
tasks:
- name: install_license_for_2410
cl_license:
src: "http://10.43.255.182/cumulus/license-2x2410.txt"
- hosts: mlnx_x86_MSN2700
tasks:
- name: install_license_for_2700
cl_license:
src: "http://10.43.255.182/cumulus/license-mellanox-demo-2700.txt"
This is the error I get from Jenkins/Ansible:
The offending line appears to be:
---
- hosts: accton_as6712_32x
^ here
And if I use YAML Lint, I get this error:
(<unknown>): did not find expected '-' indicator while parsing a block collection at line 2 column 1
I am fairly new to Ansible and would love if somebody could point out where I am going wrong?
I'm not saying the cl-license module is the wrong way but have you considered using ZTP to install licenses on your Cumulus switches?
By the way, the indentation looks correct but make sure you have actual spaces and not tab characters in your file. Also, check for any odd trailing spaces.
Setup
I want to use Ansible to configure my IAM users, groups and permissions but I am having trouble even getting off the ground. I installed the development fork of Ansible (2.1.0) and attempted to run the simple play shown in the example in the docs.
site.yml
# Basic user creation example
tasks:
- name: Create two new IAM users with API keys
iam:
iam_type: user
name: "{{ item }}"
state: present
password: "{{ temp_pass }}"
access_key_state: create
with_items:
- jcleese
- mpython
I ran the play with the following command:
$ ansible-playbook site.yml
And received the following error:
Error
ERROR! playbooks must be a list of plays
The error appears to have been in '~/aws_kingdom/site.yml': line 2, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
# Basic user creation example
tasks:
^ here
I am going to plead ignorance on a lack of understanding of the anatomy of a playbook especially one that should effectively have no hosts since it only applies to creating users in the AWS IAM service.
References
http://docs.ansible.com/ansible/iam_module.html
You still need to tell Ansible what hosts it needs to run on, just that it needs to run locally.
So instead your site.yml file should look like:
- hosts: 127.0.0.1
connection: local
tasks:
# Basic user creation example
- name: Create two new IAM users with API keys
iam:
iam_type: user
name: "{{ item }}"
state: present
password: "{{ temp_pass }}"
access_key_state: create
with_items:
- jcleese
- mpython
I encountered the:
ERROR! playbooks must be a list of plays
error myself and after double checking everything couldn't find the error.
By accident I found that when I removed any trailing whitespace and/or newlines from my playbook.yml that it fixed the issue.
It's weird because I tried validating my YAML with a linter before encountering this fix so I can't understand why it worked.
Admittedly, I don't have much experience with YAML so there might be some rule that I don't understand that I'm missing.