Ansible podman deploy - ansible

I'm trying to create a playbook which configures a RHEL 8 machine with a container. It all seems to go just fine until i try to use environment variables. That gives me the following error:
FAILED! => {"changed": false, "msg": "argument env is of type <class 'list'> and we were unable to convert to dict: <class 'list'> cannot be converted to a dict"}
2021-03-06T12:09:19.0217935Z
My code is as follows:
- name: Run zookeeper container
containers.podman.podman_container:
name: zookeeper
image: bitnami/zookeeper:3.6.2
state: started
ports:
- 2181:2181
- 3181:3181
- 10001:10001
- 2888:2888
- 3888:3888
env:
- ALLOW_ANONYMOUS_LOGIN= "yes"
I've tried a lot of different combinations but i cannot seem to get it too work.
Any idea's?
Ansible version is 2.9.0.
Rick.

The error is accurate: the env keyword expects a dictionary, but you're providing a list. Just make it a dictionary and it will work fine:
- name: Run zookeeper container
containers.podman.podman_container:
name: zookeeper
image: bitnami/zookeeper:3.6.2
state: started
ports:
- 2181:2181
- 3181:3181
- 10001:10001
- 2888:2888
- 3888:3888
env:
ALLOW_ANONYMOUS_LOGIN: "yes"
The documentation shows this clearly in the example.

Related

Error! conflicting action statements: project_path, state (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

Ansible URI not working in Gitlab CI pipeline in first run

I am facing currently a strange issue and I am not able to guess what causes it.
I wrote small Ansible scripts to test if Kafka schema-registry and connectors are running by calling their APIs.
I could run those Ansible playbooks on my local machine successfully. However, when running them in Gitlab CI pipeline (im using the same local machine as gitlab runner), the connect_test always breaks with the following error:
fatal: [xx.xxx.x.x]: FAILED! => {"changed": false, "elapsed": 0, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://localhost:8083"}
The strange thing, that this failed job will work when I click on retry button in CI pipeline.
Has anyone an idea about this issue? I would appreciate your help.
schema_test.yml
---
- name: Test schema-registry
hosts: SCHEMA
become: yes
become_user: root
tasks:
- name: list schemas
uri:
url: http://localhost:8081/subjects
register: schema
- debug:
msg: "{{ schema }}"
connect_test.yml
---
- name: Test connect
hosts: CONNECT
become: yes
become_user: root
tasks:
- name: check connect
uri:
url: http://localhost:8083
register: connect
- debug:
msg: "{{ connect }}"
.gitlab-ci.yml
test-connect:
stage: test
script:
- ansible-playbook connect_test.yml
tags:
- gitlab-runner
test-schema:
stage: test
script:
- ansible-playbook schema_test.yml
tags:
- gitlab-runner
update
I replaced URI module with shell. as a result, I see the same behaviour. The initial run of the pipeline will fail and retrying the job will fix it
maybe you are restarting the services in a previous job, take in mind that kafka connect needs generally more time to be available after the restart. Try to pause ansible after you restart the service for a minute or so.

How to disable Molecule idempotence check on Ansible role test?

Using Molecule v.2 to test Ansible roles, I faced an issue with the check for a role to be idempotent.
How can I disable this check?
As documented, Molecule configuration parameters are required to be set in molecule.yml file, but I could not find how to disable idempotence check.
---
# molecule.yml file
dependency:
name: galaxy
driver:
name: docker
lint:
name: ansible-lint
options:
x: ANSIBLE0006,ANSIBLE0010,ANSIBLE0012,ANSIBLE0013
platforms:
- name: mongo01
image: mongo:3.2
privileged: yes
groups:
- mongodb
- mongodb_master
- name: mysql_server
image: mysql
environment:
MYSQL_ROOT_PASSWORD: some_password
groups:
- mysql
- name: elasticsearch
image: molecule_local/centos:6
command: sleep infinity
dockerfile: Dockerfile
privileged: yes
groups:
- elastic
- name: esb
image: molecule_local/centos:6
command: sleep infinity
dockerfile: Dockerfile
links:
- "elasticsearch-default:elasticsearch elasticsearch01"
- "mongo01-default:mongo mongo_b2b mongo01"
- "mysql_server-default:mysql mysql_server"
groups:
- fabric
provisioner:
name: ansible
config_options:
defaults:
vault_password_file: /path/to/vault/file
diff: yes
scenario:
name: default
# Probably something like below should disable idempotency check.
idempotent: false
# Uncomment when developing locally to
# keep instances running when tests are completed.
# Must be kept commented when building on CI/CD.
# test_sequence:
# - destroy
# - create
# - converge
# - lint
# - verify
verifier:
name: testinfra
I want to get rid of idempotency check altogether and rely on my own tests.
You should uncomment the test_sequence and include only the tests you want, for example:
test_sequence:
- destroy
- create
- converge
# - idempotence
- lint
- verify

How to iterate through a list variable to fill out an Ansible task's options?

Say I'm starting a Docker Container, and have a list of apps, including port information like so:
my_apps:
- name: App1
ports:
- "2000:2000"
- name: App2
ports:
- "2001:2001"
In the following task, would there be an easy way to extract all the ports from the above variable, for all apps, into the ports option below?
- My Docker Container
docker_container:
name: ubunty
image: ubuntu
ports:
- "2000:2000"
- "2001:2001"
Currently, I have another list going for all the ports, but in order to add another port, I have to add it to both lists, which becomes cumbersome. Was hoping there would be another way.
You can do something like:
- vars:
list_of_ports:
- 2000:2000
- 2001:2001
...and then in your play:
- name: App1
ports: {{ list_of_ports }}
- name: App2
ports: {{ list_of_ports }}
The above may not be perfectly syntactically correct, but it's close enough to give you the idea.
You can use set_fact to create a new variable and add the ports value to each. Then use that to call your container.
tasks:
- name: Initialize ports
set_fact: ports=[]
- name: Collect ports from apps
set_fact: ports="{{ports}} + {{item.ports}}"
with_items: "{{ my_apps }}"
Then call your container with the ports variable
- My Docker Container
docker_container:
name: ubunty
image: ubuntu
ports: "{{ ports }}"
Here is a solution with jinja filters to reduce your list of apps into a list of ports:
my_apps | map(attribute='ports') | list
or in your task:
- My Docker Container
docker_container:
name: ubunty
image: ubuntu
ports: "{{ my_apps | map(attribute='ports') | list }}"

Simple ansible playbook syntax error (YAML)

I've just started ansible and have created a simple playbook to deploy nginx on a target server. The YAML playbook file (myplaybook.yml) looks like this:-
- name: Configure webserver with nginx
hosts: webservers
sudo: True
tasks:
- name: install nginx
- apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088
When I execute:-
$ ansible-playbook myplaybook.yml
I get:-
ERROR: Syntax Error while loading YAML script, nginx-ansible.yml
Note: The error may actually appear before this position: line 7, column 23
- apt: name=nginx update_cache=yes
environment:
^
I don't see why this error occurs - the hosts file contains the [webservers] section OK - can anyone help?
Thanks!
You've got a couple problems with your YAML. First, - name and - apt shouldn't both have the - prefix. That's making Ansible think you have one task with a name of install nginx but no module or anything else associated with it, then you have a second task with no name but invokes the apt module.
The second problem is indentation. You have an extra space in front of the word environment that makes YAML think you're starting a new child element and not just adding attributes to the current task. So your entire task should look something like this (and remember that spacing is critical):
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088

Resources