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

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

Related

Trying to git clone the repository using ssh in ansible-playbook got error occurred

While trying to git clone the repository using ssh in ansible-playbook, the below-mentioned error occurred. Could you please suggest a solution to this issue?
Error details:
FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "cmd": ["/bin/git", "fetch", "origin"], "msg": "Failed to download remote objects and refs: fatal: cannot exec '/tmp/tmpitfHF0': Permission denied\nfatal: unable to fork\n"}
An error occurred in Linux machine
Code:
hosts: App-1
become: yes
become_method: sudo
become_user: admin
vars:
git_uri: ssh://git#gitURLlink
tasks:
- name: App deploy
git: repo={{ git_uri }} dest=/etc/Repo update=yes force=yes track_submodules=yes

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

Why Ansible keeps giving me error "Could not find the requested service httpd: cannot check nor set state"?

I am doing a dry run on installing apache web server on a centos 7 box.
This is the webserver.yml file:
--- # Outline to Playbook Translation
- hosts: apacheWeb
user: aleatoire
sudo: yes
gather_facts: no
tasks:
- name: date/time stamp for when the playbook starts
raw: /bin/date > /home/aleatoire/playbook_start.log
- name: install the apache web server
yum: pkg=httpd state=latest
- name: start the web service
service: name=httpd state=started
- name: install client software - telnet
yum: pkg=telnet state=latest
- name: install client software - lynx
yum: pkg=lynx state=latest
- name: log all the packages installed on the system
raw: yum list installed > /home/aleatoire/installed.log
- name: date/time stamp for when the playbook ends
raw: /bin/date > /home/aleatoire/playbook_end.log
When I do a dry run with:
ansible-playbook webserver.yml --check
I keep getting this error:
fatal: [<ip_address>]: FAILED! => {"changed": false, "failed": true, "msg": "Could not find the requested service httpd: cannot check nor set state"}
to retry, use: --limit #/home/aleatoire/Outline/webserver.retry
I tried adding ignore_issues: true and that did not work either.
--check is not going to actually install the httpd package if it's not there yet. So then the service: call will fail if there is no httpd unit file installed yet.
You can use --syntax-check option instead.

Why does my Ansible task hang?

I have the following ansible playbook:
- hosts: node1
sudo: yes
gather_facts: no
tasks:
- name: update apt
apt: update_cache=yes
- name: install python-setuptools
apt: name=python-setuptools update_cache=yes
- name: easy_install pexpect module
easy_install: name=pexpect state=latest
- name: add geonode repo
apt_repository: repo='ppa:geonode/stable' state=present
- name: update apt
apt: update_cache=yes
- name: install geonode
apt: name=geonode update_cache=yes
- expect:
command: geonode createsuperuser
responses:
(?i)username: 'test'
(?i)email: 'test#test.com'
When I run it I get:
PLAY [node1] *******************************************************************
TASK [update apt] **************************************************************
ok: [node1]
TASK [install python-setuptools] ***********************************************
changed: [node1]
TASK [easy_install pexpect module] *********************************************
changed: [node1]
TASK [add geonode repo] ********************************************************
changed: [node1]
TASK [update apt] **************************************************************
ok: [node1]
TASK [install geonode] *********************************************************
Then it hangs indefinitely.
In the remote node (node1), I checked the dir
/home/vagrant/.ansible/tmp/ansible-tmp-1470059145.13-122191240803512/
run the file inside to see why my task is hanging
vagrant#node1:~/.ansible/tmp/ansible-tmp-1470059145.13-122191240803512$ python apt
and get:
{"msg": "Failed to lock apt for exclusive operation", "failed": true, "invocation": {"module_args": {"dpkg_options": "force-confdef,force-confold", "autoremove": false, "force": false, "name": "geonode", "install_recommends": null, "package": ["geonode"], "purge": false, "allow_unauthenticated": false, "state": "present", "upgrade": null, "update_cache": true, "default_release": null, "only_upgrade": false, "deb": null, "cache_valid_time": null}}}
Do you have any insights?
EDIT 1:
It is all day I'm launching this script and never got it working. As I posted this question, obviously, the script successfully executed till the end in 15 minutes. I launched it before lunch today and after 1 hour it was still hanging. Why do I get such a different behaviour? Is there a way in which I can control it?
This issue might be caused by an empty /var/lib/apt folder.
Vagrant might take a while to populate these folders which could cause the apt lock.
Also the playbook is inefficient as update_cache is used multiple times. I would recommend to use something like this:
- hosts: node1
sudo: yes
gather_facts: no
tasks:
# Pause for 5 minutes to make sure vagrant does not hold apt lock.
- pause:
minutes: 5
- name: add geonode repo
apt_repository:
repo: 'ppa:geonode/stable'
state: present
- name: Install apt packages.
apt:
name: "{{ item }}"
state: present
update_cache: true
with_items:
- python-setuptools
- geonode
- name: Create geonode superuser.
expect:
command: geonode createsuperuser
responses:
(?i)username: 'test'
(?i)email: 'test#test.com'
This way Ansible won't update the repositories multiple times during the play.
Since the last thing you see is TASK [install geonode], that's where it's getting stuck.
You are asking it to run geonode createsuperuser which you expect to cause a prompt for username and password.
But what is likely happening is that there is an error being produced from that command, and the expect task is not handling the error, instead just hanging.
You can login to the server you are running this against and run the geonode createsuperuser command manually to see what error is being produced.
In my case, this was happening as a result of the username already being taken since I successfully ran the command on this machine already.
Error: That username is already taken.
Even with the echo: yes argument, ansible doesn't seem to pass on the responses to make it obvious what's happening. And it doesn't accept ignore_errors, so there seems to be no way to handle errors with the expect module.
To work around this, I added another task after the createsuperuser task which places a file in the project indicating the user was created once, and then added creates: {{ path }}/superuser_exists.txt to the createsuperuser task so that it won't run if that file is already there.
It's a hack, but an easy one, and until the module gets better error handling, it will work well enough.
- name: Create the django superuser
expect:
command: "{{ virtualenv_path }}/bin/python3 {{ project_path }}/{{ api_app_name }}/manage.py createsuperuser"
creates: "{{ project_path }}/{{ api_app_name }}/superuser_exists.txt"
responses:
(?i)username: "{{ superuser_username }}"
(?i)email: "{{ superuser_email }}"
(?i)password: "{{ superuser_password }}"
(?i)again: "{{ superuser_password }}"
- name: Create a file to indicate that the superuser was already created
file: path="{{ project_path }}/{{ api_app_name }}/superuser_exists.txt" state=touch

Ansible error message

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

Resources