Ansible Roles and handlers - Cannot get role handlers to work - ansible

I need to set up Apache/mod_wsgi in Centos 6.5 so my main YAML file is as such:
---
- hosts: dev
tasks:
- name: Updates yum installed packages
yum: name=* state=latest
- hosts: dev
roles:
- { role: apache }
This should update all yum-installed packages then execute the apache role.
The apache role is configured to install Apache/mod_wsgi, set Apache to start at boot time and restart it. The following are the contents of roles/apache/tasks/main.yml:
---
- name: Installs httpd and mod_wsgi
yum: name={{ item }} state=latest
with_items:
- httpd
- mod_wsgi
notify:
- enable httpd
- restart httpd
And the handlers in roles/apache/handlers/main.yml:
---
- name: enable httpd
service: name=httpd enabled=yes
- name: restart httpd
service: name=httpd state=restarted
The handlers do not seem to run since the following output is given when I execute the playbook:
PLAY [dev] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [dev.example.com]
TASK: [Updates yum installed packages] ****************************************
ok: [dev.example.com]
PLAY [dev] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [dev.example.com]
TASK: [apache | Installs httpd and mod_wsgi] **********************************
ok: [dev.example.com] => (item=httpd,mod_wsgi)
PLAY RECAP ********************************************************************
dev.example.com : ok=4 changed=0 unreachable=0 failed=0
And when I vagrant ssh into the virtual machine, sudo service httpd status shows httpd is stopped and sudo chkconfig --list shows it has not been enabled to be started by init.
I'm just starting out with Ansible, so is there something obvious I could be missing?

Well, to answer my own question, I realized that there's a subtle point I missed:
http://docs.ansible.com/playbooks_intro.html#handlers-running-operations-on-change
Specifically, the notify signal is produced only if the task introduces a change. So for my use case I think I'll go with enabling and starting Apache in standalone tasks instead of relying on change signal handlers.

Related

Ansible module to stop and start `ssh` service

Question:
This scenario is used to explain the usage of modules in Ansible.
For this you have to stop and start a service named ssh.
Tasks to be done:- Write a task in main.yml file present in fresco_module\tasks folder.
The task is to stop and start the service named ssh using the service module in Ansible.
Note:
Run project install to install ansible.mainplaybook.yml file is provided to ansible-playbook.
Use the localhost for the inventory for ansible-playbook.
My Code:
- hosts: localhost
become: yes
tasks:
- name: Stop and Start ssh
service:
name: ssh
state: "{{ item }}"
with_items:
- stopped
- started
Output:
PLAY [localhost] *******************************************************************************
TASK [Gathering Facts] *************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host localhost should use /usr/bin/python3,
but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future
Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information. This feature will be removed in version 2.12. Deprecation warnings can be disabled
by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost]
TASK [Stop and Start ssh] **********************************************************************
changed: [localhost] => (item=stopped)
ok: [localhost] => (item=started)
PLAY RECAP *************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Issue: The service is already running after Ansible stopped it, which looks like sshd was never stopped in the first place.
Command used to check the status: service ssh status. I used this command with state:stopped also but the sshd is still running. I have been facing this issue for so long. I tried with state:restarted also.
Hi Vivek and welcome to the community!
This should be an easy one. You can tell Ansible to restart the service directly without stopping and starting it in two separate steps.
The following code should work:
- hosts: localhost
become: yes
tasks:
- name: Stop and Start ssh
service:
name: ssh
state: restarted
This way Ansible ensures, that the ssh service was stopped an started - in short: restarted. You don't even need the with_items loop.
Ive tried the below and getting the same "sshd running " output , but the issue here i think is they want us to have both stop and start under one task. Also we are not allowed to use the restarted state :/
-
name: "Stop ssh"
service:
name: ssh
state: stopped
-
name: "start ssh"
service:
name: ssh
state: started
---
- hosts: localhost
connection: local
become: true
become_method: sudo
tasks:
- name: stop a service
service:
name: ssh
state: stopped
- name: start a service
service:
name: ssh
state: started
Add become-method sudo and task as stop and start the service.
Even i tried this problem with many other modules, like systemd, still i was not able to stop the service. But using command module, and passing 'sudo service ssh stop', i was able to stop the service. but still not passed the problem.
Even tried felix's answer before, still not able to pass.
And also if anybody can help me with "Ansible Choral | 5 | Ansible Roles problem"
would be great. even in that problem after getting 100% fs score not able to pass.
Just run the playbook for stopping starting ssh service without restart.
or use this
-
name: "Stop ssh"
service:
name: ssh
state: stopped
-
name: "start ssh"
service:
name: ssh
state: started
After running playbook successfully. Just stop the service by
sudo serivce ssh stop and then start the service sudo service ssh start.
then just submit the test. you will pass the handson
Just write below command in your main yaml file. This will first stop the ssh service and then start it again.
- name: Stop service ssh, if started
ansible.builtin.service:
name: ssh
state: stopped
- name: Start service ssh, if not started
ansible.builtin.service:
name: ssh
state: started
On AWS EC2 instances the ssh service is sshd.
- name: restart ssh daemon
hosts: all
remote_user: ec2-user
become: yes
become_method: sudo
tasks:
- name: Stop and Start ssh
service:
name: sshd
state: restarted
In the above YAML, replacing sshd with ssh will fail with "msg": "Could not find the requested service ssh

ansible role can't start apache via handler

I have this simple role for apache (in CentOS7):
roles/apache/tasks/main.yml
---
- name: Add epel-release repo
yum:
name: epel-release
state: present
- name: Install Apache2
yum:
name: httpd
state: present
- name: Insert Index Page
copy:
src: index.html
dest: /var/www/html/index.html
roles/apache/handlers/main.yml
---
- name: Start Apache
service: name=httpd state=started
- name: verify that the web service is running
command: systemctl status httpd
register: status_result
- name: debug
debug: var=status_result
with-roles.yml - playbook same level as 'roles' directory
---
- name: Install apache2 in CentOS 7
hosts: 1.23.4.56
become: true
roles:
- apache
I then run the playbook as follows:
$ ansible-playbook -u root --private-key ~/.ssh/this_key.ppk with-roles.yml -i "1.23.4.56" -vvvv
Here's the tail-end part of the verbose output on screen:
...
...
"mode": "0644",
"owner": "root",
"path": "/var/www/html/index.html",
"size": 11,
"state": "file",
"uid": 0
}
META: ran handlers
META: ran handlers
PLAY RECAP *******************************************************************************************************************
1.23.4.56 : ok=4 changed=0 unreachable=0 failed=0
but when I logged-in to 1.23.4.56, machine has the httpd installed but is stopped (which means that the handler didn't run). What am I doing wrong?
A handler is only executed when it's notified.
See Handlers: Running Operations On Change
The answer turns out to be in https://serverfault.com/questions/617548/always-trigger-handler-execution-in-ansible as per hints from René Pijl.
Specifically, I had to add this to the bottom of roles/apache/tasks/main.yml
...
...
- name: Apache Starter
command: /bin/true
notify: Start Apache

Can't install python, pip related apps through Ansible

I am using below ansible yml file to install python, pip, etc.
roles/python/main.yml:
---
- name: python
apt:
pkg: python
- name: python-pip
apt:
pkg: python-pip
- name: mongopy
pip:
pkg: mongopy
- name: mtools
pip:
pkg: mtools
when I run ansible-playbook on this script, I get below
PLAY [ec2] ***********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [xxxxx.ap-southeast-2.compute.amazonaws.com]
PLAY RECAP ***********************************************************************************************************************************************************************************************
xxxxxap-southeast-2.compute.amazonaws.com : ok=1 changed=0 unreachable=0 failed=0
there is no error on them but I checked these apps are not installed on the remote host. What wrong with my yml file? Is there any place I can check what the error is?
below is my playbook:
python.yml:
---
- hosts: ec2
remote_user: ubuntu
roles:
- python
below is the command I run:
ansible-playbook -i hosts python.yml
There are no tasks in your python role. Please have a look at the role structure.
If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
Tasks file (main.yml) should be placed in the tasks subdirectory of the role, not in the main role's directory.
And this has nothing to do with how you described the problem (installing Python or Pip). Even if you replaced the tasks with a single debug task which displays Hello world by default, it would not run.

Ansible playbook error

I am testing with ansible, what I'm trying to do is install apache2 on another ubuntu server, I already have the group "test" defined with 1 ip. but what happens is that ansible throws me some errors when executing it, I've searched a lot of sites and a lot of people have had this issue, but on different situations and I amd starting to get frustrated with it. Can somebody help me?
Ansible Playbook:
---
- hosts: test
sudo: yes
tasks:
- name: Check if Im sudo
command: echo $USER
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
handlers:
- name: start apache2
service: name=apache2 state=started
STDOUT
root#ip-172-31-35-33:/etc/ansible/example# ansible-playbook example.yml
PLAY [test] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [172.31.36.176]
TASK: [Check if Im sudo] ******************************************************
changed: [172.31.36.176]
TASK: [install packages] ******************************************************
failed: [172.31.36.176] => {"failed": true}
msg: this module requires key=value arguments (['name:apache2', 'update_cache=yes', 'state=latest'])
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/root/example.retry
172.31.36.176 : ok=2 changed=1 unreachable=0 failed=1
BTW, the host is reachable, I can ssh into it, even with ansible, this is the proof
root#ip-172-31-35-33:/etc/ansible/example# ansible -m shell -a "ifconfig | grep 'inet addr'" test
172.31.36.176 | success | rc=0 >>
inet addr:172.31.36.176 Bcast:172.31.47.255 Mask:255.255.240.0
inet addr:127.0.0.1 Mask:255.0.0.0
another thing is that I'm able to install apache2 by hand on the other server, BUT IT IS NOT INSTALLED BECAUSE I WANT TO INSTALL IT USING ANSIBLE
Thanks
Within an individual task, Ansible requires you to make the choice between standard YAML syntax and their own parsed version with equals signs. In this task, you are mixing the two:
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
This could be either written:
- name: install packages
apt:
name: apache2
update_cache: yes
state: latest
notify: start apache2
Or:
- name: install packages
apt: name=apache2 update_cache=yes state=latest
notify: start apache2
YAML also allows for using bracket and comma syntax to allow you to specify your key-value information on the same line:
- name: install packages
apt: {name: apache2, update_cache: yes, state: latest}
notify: start apache2
Any of these are valid.
You're using a colon where an equal is needed. You need to change the name:apache2 to name=apache2.

Ansible - msg: No package matching '$item' is available

I need to set-up my server. I've the following ansible playbook.
---
- hosts: webservers
user: root
sudo: yes
tasks:
- name: add nginx ppa
action: apt_repository repo=ppa:nginx/stable state=present
- name: install common packages needed for python application development
action: apt pkg=$item state=installed
with_items:
- libpq-dev
- libmysqlclient-dev
- libxml2-dev
- libxslt1-dev
- mysql-client
- python-dev
- python-setuptools
- python-mysqldb
- build-essential
- git
- nginx
- name: install pip
action: easy_install name=pip
- name: install various libraries with pip
action: pip name=$item state=present
with_items:
- uwsgi
handlers:
- name: restart nginx
service: name=nginx state=restarted
When I run this script, following is the output
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [IP]
TASK: [add nginx ppa] *********************************************************
ok: [IP]
TASK: [install common packages needed for python application development] *****
failed: [IP] => (item=libpq-dev,libmysqlclient-dev,libxml2-dev,libxslt1-dev,mysql-client,python-dev,python-setuptools,python-mysqldb,build-essential,git,nginx) => {"failed": true, "item": "libpq-dev,libmysqlclient-dev,libxml2-dev,libxslt1-dev,mysql-client,python-dev,python-setuptools,python-mysqldb,build-essential,git,nginx"}
msg: No package matching '$item' is available
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/home/praful/setup_server.yaml.retry
IP : ok=2 changed=0 unreachable=0 failed=1
I've referred this link for the same.
I'm new to ansible and dont understand the error, since there is no typo in the package name provided in with_items. What exactly is the error??
You need the jinja variable syntax {{item}} where you have $item.

Resources