Ansible error message - ansible

I'm trying to use ansible to build a docker image locally but I'm
running into problems.
- hosts: all
tasks:
- name: Build Docker image
local_action:
module: docker_image
path: .
name: SlothSloth
state: present
And my /etc/ansible/hosts contains
localhost ansible_connection=local
But when I try to run it I get:
TASK: [Build Docker image] ****************************************************
failed: [localhost -> 127.0.0.1] => {"failed": true, "parsed": false}
failed=True msg='failed to import python module: No module named docker.client'
FATAL: all hosts have already failed -- aborting

If you are using virtualenv, you are probably running ansible with /usr/bin/python by default. To bypass this behavior, you have to define the variable "ansible_python_interpreter".
Try to use :
- hosts: all
vars:
- ansible_python_interpreter: python
tasks:
- name: Build Docker image
local_action:
module: docker_image
path: .
name: SlothSloth
state: present

Related

How to use gather facts inside role?

I want to check if a package is installed but I get an error. Why the facts are not available inside the role?
TASK [docker : Check if listed package is installed or not on Debian Linux family] *****************************************************************************************************************************************************************
fatal: [10.100.0.52]: FAILED! => {"changed": false, "msg": "Could not detect which package manager to use. Try gathering facts or setting the \"use\" option."}
My playbook
- hosts: "{{ host }}"
gather_facts: yes
sudo: yes
roles:
- { role: "docker"}
and part of my role
- name: Check if listed package is installed or not on Debian Linux family
package:
name: ferm
state: present
check_mode: true
register: package_check
- name: Print execution results
debug:
msg: "Package is installed"
when: package_check is succeeded

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Service is in unknown state", "status": {}}

This is my ansible playbook and I'm only running into an issue on the final task for starting and enabling Grafana.
---
- name: Install Grafana
hosts: hosts
become: yes
tasks:
- name: download apt key
ansible.builtin.apt_key:
url: https://packages.grafana.com/gpg.key
state: present
- name: Add Grafana repo to sources.list
ansible.builtin.apt_repository:
repo: deb https://packages.grafana.com/oss/deb stable main
filename: grafana
state: present
- name: Update apt cache and install Grafana
ansible.builtin.apt:
name: grafana
update_cache: yes
- name: Ensure Grafana is started and enabled
ansible.builtin.systemd:
name: grafana-server
state: started
enabled: yes
This is the error I received:
TASK [Ensure Grafana is started and enabled]
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Service is in unknown state", "status": {}}
This is also the configuration of my hosts file just in case:
[hosts]
localhost
[hosts:vars]
ansible_connection=local
ansible_python_interpreter=/usr/bin/python3
I'm pretty much just trying to have it run these two commands I have in a bash script
sudo systemctl start grafana-server
sudo systemctl enable grafana-server.service
Got it sorted out- turns out my system wasn't booted using systemd as init system. So I changed the Ansible module from ansible.builtin.systemd to ansible.builtin.sysvinit

Getting python dependency issue when configuring droplet at boot time in digital ocean

I am using ansible playbook to create droplet in digital ocean and want to configure it at boot time using ansible. Droplet is creating successfully but when i am trying to configure it at boot time its giving python dependency issue. I am aware about it but now i am confused how we can install it during boot time or on the fly? Below is the my ansible playbook:
---
- hosts: localhost
tasks:
- name: Create new DO Droplet
digital_ocean:
state: present
command: droplet
name: ansibletest
api_token: xyz123
size_id: '1gb'
region_id: ams3
image_id: '39739486'
ssh_key_ids: '23625890'
register: my_droplet
- name: print info about my_droplet
local_action:
module: debug
msg= "ID is {{ my_droplet.droplet.id }} IP is {{ my_droplet.droplet.ip_address }}"
- name: Add new droplet to host group
local_action: add_host hostname={{ my_droplet.droplet.ip_address }} groupname=launched
- name: Wait for SSH to come up
local_action: wait_for host={{ my_droplet.droplet.ip_address }} port=22 delay=60 timeout=320 state=started
- hosts: launched
become: true
gather_facts: True
tasks:
- name: installing redis server
apt: name=redis-server state=latest
Below is the error which i got and its related to python dependency on remote client.
fatal: [188.26.76.45]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 188.166.71.116 closed.\r\n", "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", "msg": "MODULE FAILURE", "rc": 127}
I didn't phase this issue in AWS because EC2 instance have python2.7. Can you please help me to fix this issue so i can configure digital ocean droplet at boot time using ansible. Any guidance will be appreciated.
I have run the playbook using the below command:
ansible-playbook droplet.yml --key-file "/etc/ansible/tek.pem"
Thanks.
You can configure the instance using the raw: module, which requires only ssh access.
You can see an example of that kind of thing in the kubespray bootstrap role, but the tl;dr is:
- hosts: launched
gather_facts: no
become: yes
tasks:
- raw: |
set -e
# but you are responsible for your own idempotent behavior
if [ -x /usr/bin/python ]; then exit 0; fi
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y python
# now, in theory, you can resume using ansible modules
# and can do the equivalent of "gather_facts: yes"
- setup:
- # etc etc

"[Errno 13] Permission denied error" while running Py script in Ansible

When, I'm running a Python script via an Ansible playbook, I get the following error:
fatal: [localhost]: FAILED! => {"changed": false, "cmd": "/Dest/To/Repo/HW.py", "failed": true, "msg": "[Errno 13] Permission denied", "rc": 13}
I did add the sudo: yes line:
Here is my yaml file:
- name: a play that runs entirely on the ansible host
hosts: 127.0.0.1
sudo: yes
connection: local
tasks:
- name: check out a git repository
git: repo={{ repo_url }} dest=/Dest/To/Repo/ accept_hostkey=yes
vars:
repo_url: https://github.com/lorin/mezzanine-example.git
- name: Running the Python Script
command: /Dest/To/Repo/HW.py
The HW.py script is just print("Hello World")
Is there anything which I need to do, regarding the permissions error?
You need to use umask to add execution rights in you git module call :
- name: a play that runs entirely on the ansible host
hosts: 127.0.0.1
sudo: yes
connection: local
tasks:
- name: check out a git repository
git: repo={{ repo_url }} dest=/Dest/To/Repo/ accept_hostkey=yes
umask: 0022
vars:
repo_url: https://github.com/lorin/mezzanine-example.git

Ansible: want to use the var in hosts

I'm using the Ansile 2.0.1.0 And Docker.
I want to connect to the container to create a Docker container in Ansbile.
Also the name of the container want to manage in the variable.
But When executing I get error message.
main.yml
- name: data container
hosts: localhost
roles:
- role: docker
tasks:
- debug: var=docker_hostname
- name: hogefuga
hosts: "{{docker_hostname}}"
connection: docker
roles:
- hogefuga
role/dockers/tasks/main.yml
- name: Create Container
shell: "docker run --name={{docker_hostname}}"
- name: host add DockerContainer
add_host: name={{ docker_hostname }} group="dockers"
group_vars/all
docker_hostname: hoge
Error Message
TASK [debug] *******************************************************************
ok: [localhost] => {
"docker_hostname": "hoge"
}
ERROR! 'docker_hostname' is undefined
I'm guessing that it for using the var to hosts.
How can I fix the error?
I found Anser.
Is it possible to define playbook-global variables in Ansible?
I did not know that can set the group_name to hosts.
- name: hogefuga
hosts: dockers
connection: docker
roles:
- hogefuga

Resources