Error on simple ansible play-book - ansible

I'm running a simple ansible playbook and getting an error:
ERROR: parse error: playbooks must be formatted as a YAML list, got type 'str'
---
- hosts: all
tasks:
- name: Get server availability by pinging it
ping:
- name: Get server hostname
command: hostname
Not sure where the problem is. Ansible v1.9.6

Answer from comment: missing -i flag in ansible-playbook hostname.yml inventory.

Related

Ansible collection not detected when running playbook on AWX

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.

openwhisk ansible setup.yml failed with error "ansible/_distribution is undefined"

I want to install Openwhisk with docker using ansible. But when I run
ansible-playbook -i environments/local setup.yml
I get this error:
> The conditional check 'nginx.ssl.cert == "openwhisk-server-cert.pem"'
failed. The error was: error while evaluating conditional (nginx.ssl.cert ==
"openwhisk-server-cert.pem"): 'ansible_distribution' is undefined The error
appears to have been in '/home/parichehr/openwhisk/ansible/setup.yml': line 60,
column 5, but may be elsewhere in the file depending on the exact syntax
problem. The offending line appears to be: # Generate nginx certificates -
name: gen untrusted server certificate for host ^ here
ansible_distribution is a variable defined through the task Gathering Facts. Gather facts is enable by default but if you want to be sure you are gathering facts you can wirte gather_facts: yes in your playbook as below
- name: Setup.yml
hosts: all
gather_facts: yes
tasks:
...........

using netconf_get in an ansible playbook

I am testing out using netconf via Ansible and I keep getting this error when running the playbook:
ansible.module_utils.connection.ConnectionError:
AuthenticationException('Authentication timeout.',)
I am ablte to use ssh over port 830 to my Cisco device from the scripting server:
ssh cisco#10.1.1.1 -p 830 -s netconf
This is the playbook:
---
- name: My Playbook
hosts: 'my_host'
gather_facts: false
tasks:
- name: Execute the get_config RPC
netconf_get:
display: json
register: result
- name: Print the configuration as JSON
debug:
var: result.output
And the inventory is something like this:
[my_lab:children]
my_lab_iosxr
[my_lab:vars]
look_for_keys = False
host_key_checking = False
ansible_ssh_pass = 'cisco'
ansible_user = 'cisco'
[my_lab_iosxr]
my_host ansible_host=10.1.1.1 ansible_network_os=iosxr ansible_connection=netconf
I should add, I see this error on the console of the cisco device when attempting the play above:
Sep 8 17:37:42.218 UTC: SSHD_[67398]: %SECURITY-SSHD-3-ERR_GENERAL :
Failed to receive User authentication request
Looks like I have found the answer to my own question.
I decided to just write my own netconf module in python and I was getting the same error.
So I switched to using python installed on another machine and the same code works.
Seems like there is an issue with the version of python installed on that server...

Ansible playbook error while running on - hosts:

write a task in main.yml to stop and start service in service "ssh" using service module in ansible.
---
- hosts: localhost
become: true
become_method: sudo
tasks:
- name: stop service
service:
name: ssh
state: stopped
- name: start service
service:
name: ssh
state: started
when run it's giving below error
[WARNING]: Unable to parse /projects/challenge/localhost as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_module/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be
---
- hosts: localhost
^ here
Firstly, you should be able to do SSH to localhost.
You can try,
ssh user#localhost date
You can create a hosts file and name it hosts and add the following content to it.
[localhost]
localhost
[localhost:vars]
ansible_ssh_user=user
ansible_ssh_pass=pass
ansible_sudo_pass=sudopass
And run the playbook as
ansible-playbook -i hosts main.yml
Using command module was able to stop and start the service, using sudo service ssh stop and sudo service ssh start served my purpose.
was not able to do so with service module, still don't know about that
Resolved at my end by using complete path for .yml file
ansible-playbook -i /etc/ansible/hosts myfirstplaybook.yml

How to use passwords in encrypted vars-file (vault) in Ansible inventory?

I'm new to Ansible and struggle with Ansible vaults in combination with inventory files.
What I want to achieve is a playbook that updates three machines. Two of these machines require a password (for sudo) so I can become root.
So here's my playbook...
---
- name: update all hosts, make sure default software is installed
hosts:
- vhosts
- physical
vars_files:
- 'vars/main.yml'
tasks:
- name: Update apt cache
apt: update_cache=yes
- name: Upgrade packages
apt: upgrade=dist
... and this is my inventory (filename inventory) with passwords still in plain text:
[vhosts]
10.0.0.1 ansible_user=root
[physical]
10.0.0.200 ansible_user=xxx ansible_become=yes ansible_become_method=sudo ansible_become_pass=secretpassword1
10.0.0.201 ansible_user=yyy ansible_become=yes ansible_become_method=sudo ansible_become_pass=secretpassword2
This is working fine.
Of cause I do not want secretpassword1 or secretpassword2 in the inventory. So I created a vault (stored in file vars/main.yml that looks like that:
---
- vars:
pass1:secretpassword1
pass2:secretpassword2
And I changed the inventory file to this:
[vhosts]
10.0.0.1 ansible_user=root
[physical]
10.0.0.200 ansible_user=xxx ansible_become=yes ansible_become_method=sudo ansible_become_pass="{{ pass1 }}"
10.0.0.201 ansible_user=yyy ansible_become=yes ansible_become_method=sudo ansible_become_pass="{{ pass2 }}"
Now when I try to execute the goodness with ansible-playbook update.yml -i inventory --ask-vault-pass I receive following errors:
fatal: [10.0.0.200]: FAILED! => {"msg": "The field 'become_pass' has an invalid value, which includes an undefined variable. The error was: 'pass1' is undefined\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'pass1' is undefined"}
fatal: [10.0.0.201]: FAILED! => {"msg": "The field 'become_pass' has an invalid value, which includes an undefined variable. The error was: 'pass2' is undefined\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'pass2' is undefined"}
It seems like I was unable to define my variables correctly. But I have absolutely no clue how to do so. If anyone could give me a hand I'd be super happy. Thanks.
Make your vault file look like this:
---
pass1: secretpassword1
pass2: secretpassword2
Your error:
You define variables in incorrect format inside YAML file.

Resources