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

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:
...........

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.

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 the listen_ports_facts module, ansible

Using ansible I want to check in which port tomcat is running, of course I think there are different ways to do it, but I found this ansible module
https://docs.ansible.com/ansible/latest/modules/listen_ports_facts_module.html
And I would like to use it, but according to the examples, I do not know how to use it.
I mean if I setup
gather_facts: true
And run the task
- name: List TCP ports
debug:
msg: "{{ ansible_facts.tcp_listen }}"
I got the error
TASK [discover-servers : List TCP ports] *******************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'tcp_listen'\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: List TCP ports\n ^ here\n"}
If I set up the task exactly how it is in the example, I got an error in the first task Gather facts on listening ports
TASK [discover-servers : include_tasks] ********************************************************************************************************************************
task path: /home/A78252689/sap_bo/roles/discover-servers/tasks/main.yml:4
fatal: [2a00:da9:2:21ca:111:0:426:2]: FAILED! => {"reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to be in '/home/A78252689/sap_bo/roles/discover-servers/tasks/tomcat_servers.yml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Gather facts on listening ports\n ^ here\n"}
So, do you know how to use the module listen_ports_facts?
Thanks in advance for your kind support
The gather_facts stage of your playbook runs uses the setup module. It does not run listen_ports_facts, so if you don't run the module explicitly you won't have those facts available.
From the docs, the listen_ports_module creates the following facts:
tcp_listen
udp_listen
Using Ansible 2.9.2, the following works just fine:
---
- gather_facts: false
hosts: localhost
tasks:
- listen_ports_facts:
- debug:
msg: "{{ tcp_listen }}"
- debug:
msg: "{{ udp_listen }}"
If you're trying to run the listen_ports_facts module and you're getting the error "no action detected in task", it may be that you're running an older version of Ansible that doesn't have the listen_ports_facts module. It first showed up in version 2.9.

Unable to set environment variables for use in ansible roles

I have playbook running fine when I have environment variables and tasks defined in one single playbook without roles.
But when I structure my project into roles, I see that running tasks is not finding the environment variables that are set from the original playbook.
Any hint how to set env variables so they are available for all roles inside a playbook?
Do I need to specify the environment variables in tasks/main.yaml file?, if yes how should do this exactly?
cat playbook.yaml
-
name: Deploy Team Services Playbook
hosts: all
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
KUBECONFIG: "{{ ansible_env.HOME }}/.kube/config/{{ ansible_env.USER }}.kubeconfig"
roles:
- prereq1_setup
- prereq2_k8s
prereq1_setup\tasks\main.yaml
- name: "Validate kubeconfig set?"
shell: echo {{ ansible_env.KUBECONFIG }}
failed_when: "'KUBECONFIG' not in ansible_env"
Above works if I don't use roles and directly add tasks below. currently, am getting error as
output:
|TASK [prereq1_setup : Validate kubeconfig set?] *****************************************************
fatal: [target1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'KUBECONFIG'\n\nThe error appears to be in '/Users/testu/ansible/ansible-team/team_deploy/roles/prereq1_setup/tasks/main.yaml': line 57, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: \"Validate kubeconfig set?\"\n ^ here\n"}
Any hint how to set env variables so they are available for all roles inside a playbook?
The mechanism you are using is correct, and that environment variable is being correctly set, but it is set in the environment, and not in the ansible facts. Those facts are gathered before the playbook boots up, and thus your environment: happens after fact gathering, which explains why ansible_env does not contain it
You have a few paths forward, depending on what you prefer:
Explicitly re-gather facts inside the playbook (or even change your playbook to gather_facts: no and invoke setup: manually)
Stop looking for the environment in ansible_env, with the trust that it is actually there, and just use the commands which need the environment variable
Explicitly declare a separate fact to make that variable available to both the environment: and to the ansible tasks
If you want the first one, it would look like:
-
name: Deploy Team Services Playbook
hosts: all
gather_facts: no
environment:
whatever: goes here
pre_tasks:
- setup:
roles:
- and so forth
You can confirm the second via:
- name: ensure $KUBECONFIG is set
shell: echo $KUBECONFIG
And the third would look like:
- hosts: all
environment:
alpha: beta
vars:
alpha: beta
roles:
- # now {{ alpha }} is available to ansible and as $alpha in `commands:`

Error on simple ansible play-book

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.

Resources