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
Related
I am trying to install Php, Apache in RHEL using the Ansible Playbook. But I am getting following error.
*failed: [18.191.65.251] (item=[u'php', u'php-mysql', u'php-pdo', u'php-gd', u'php-mbstring']) => {"ansible_loop_var": "item",
"changed": false, "failures": ["No package php-mysql available."],
"item": ["php", "php-mysql", "php-pdo", "php-gd", "php-mbstring"],
"msg": ["Failed to install some of the specified packages"], "rc": 1,
"results": []}
Screenshot of error
The code that I have used to write the playbook is as follows
---
- hosts: all
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: starting httpd service
service:
name: httpd
enabled: yes
state: started
- name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
- php
- php-mysql
- php-pdo
- php-gd
- php-mbstring
- name: restart Apache service
service:
name: httpd
state: restarted
Use Amazon Linux instead of RHEL, rest code will code work.
hosts: all
become: yes
tasks:
name: Install httpd
yum:
name: httpd
state: present
name: starting httpd service
service:
name: httpd
enabled: yes
state: started
name: Installing php packages
yum:
name: "{{ item }}"
state: present
with_items:
php
php-mysql
php-pdo
php-gd
php-mbstring
name: restart Apache service
service:
name: httpd
state: restarted
I am trying to install Apache 2, PHP on Ubuntu machine using the ansible-playbook.
I am getting the following error
Error after executing playbook
fatal: [18.220.215.181]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (systemd) module: enable Supported parameters include: daemon_reexec, daemon_reload, enabled, force, masked, name, no_block, scope, state, user"}The ansible playbook is as follows---
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enable: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
- name: restart apache2 afetr restart
service:
name: apache2
enable: yes
state: restarted
`
The right parameter is enabled (not enable) in your service tasks.
- name: restart apache2 afetr restart
service:
name: apache2
enabled: yes
state: restarted
Change "enabled" task containing service module.
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enabled: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
I think last task is not required.
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.
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.
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.