I am getting error while testing the yml file .
Note- from master node I want to install httpd and want to activate httpd service in two of its nodes .
I am able to install the httpd in two nodes but httpd services are not starting .
[ansible@ip-172-31-40-236 ~]$ vi handlers.yml
[ansible@ip-172-31-40-236 ~]$ ansible-playbook handlers.yml --check
PLAY [demo] ***********************************************************************************************
TASK [Gathering Facts] ************************************************************************************
[WARNING]: Platform linux on host 172.31.41.188 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [172.31.41.188]
[WARNING]: Platform linux on host 172.31.39.20 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [172.31.39.20]
TASK [install httpd server on centos] *********************************************************************
changed: [172.31.41.188]
changed: [172.31.39.20]
RUNNING HANDLER [restart httpd] ***************************************************************************
fatal: [172.31.39.20]: FAILED! => {"msg": "The module service was not found in configured module paths"}
fatal: [172.31.41.188]: FAILED! => {"msg": "The module service was not found in configured module paths"}
NO MORE HOSTS LEFT ****************************************************************************************
PLAY RECAP ************************************************************************************************
172.31.39.20 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
172.31.41.188 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
My yml file is
--- #My test yml file
- hosts: demo
user: ansible
become: true
connection: ssh
tasks:
- name: install httpd server on centos
action: yum name=httpd state=present
notify: restart httpd
handlers:
- name: restart httpd
action: service name=httpd state=restarted
please let me know where i am doing wrong .
Hello,
Please, share how did you install the ansible package in the ansible vm system.
Regards,
Fabio
Hi Fabio ,
I am new to ansible .I have created three EC2 instances in AWS and named the master node to Ansible-server and two other nodes to node 1 and node 2. All are linux instances .
i just wanted to install httpd into node 1 and node 2 through a yml file which i have created in an instance named as Ansible-server .
Below is my yml file.I have named this file as handlers.yml. i have created this file in my master node named as Ansible-server.Now this yml file is creating the httpd service in other nodes (node 1 & node 2 ) . I confirmed this by supplying the command in two nodes as which httpd and it gives me out put as
Node-1
[ansible@ip-172-31-41-188 ~]$ which httpd
/usr/sbin/httpd
[ansible@ip-172-31-41-188 ~]$
[ansible@ip-172-31-41-188 ~]$ sudo service httpd status
Redirecting to /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd.service(8)
[ansible@ip-172-31-41-188 ~]$
same output i am getting from Node-2 as well. I have created a user named as ansible in all the three instances .
Master-->[ansible@ip-172-31-40-236 ~]$
node-1 -->[ansible@ip-172-31-41-188 ~]$
node -2-->[ec2-user@ip-172-31-39-20 ~]$ sudo su
[root@ip-172-31-39-20 ec2-user]# su - ansible
Last login: Wed Dec 28 20:28:48 UTC 2022 on pts/1
[ansible@ip-172-31-39-20 ~]$
So when i am executing [ansible@ip-172-31-40-236 ~]$ ansible-playbook handlers.yml from the master node it is able to install the httpd but not able to restart the htppd service .
thats why in out put we are getting one failed=1 and ok=2 and change =1 .But it should be ok=3 and change =2 .
--- #My test yml file
- hosts: demo
user: ansible
become: true
connection: ssh
tasks:
- name: install httpd server on centos
action: yum name=httpd state=present
notify: restart httpd
handlers:
- name: restart httpd
action: service name=httpd state=restarted
could you please let me know why my httpd service is showing inactive in all the nodes .
Have you tried to start and enable the service first?
I mean:
- name: start and enabled httpd
service:
name: httpd
state: started
enabled: yes
Please try to use the above example.
Hi Fabio,
I have created another file named as test1.yml and did a dry run as you said to do it first .Below are the details for your reference .
[ansible@ip-172-31-40-236 ~]$ cat test1.yml
---
- name: start and enabled httpd
service:
name: httpd
state: started
enabled: yes
[ansible@ip-172-31-40-236 ~]$ ansible-playbook test1.yml --check
ERROR! 'service' is not a valid attribute for a Play
The error appears to be in '/home/ansible/test1.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: start and enabled httpd
^ here
[ansible@ip-172-31-40-236 ~]$
while doing the dry run it is throwing an error as service is not a valid attribute for a play .
Best Regards
Javed
@expertjaved In your last example, check your indentation. Under the "- name", the "s" in service should be aligned with the "n". Under "service", the "name", "state", and "enabled" lines should be further indented by another two spaces (aligned with the "r" in service). I think the exact number of spaces is somewhat flexible, as long as you're consistent in the indentation.
If this post doesn't mess with my formatting, that'd be:
- name: start and enabled httpd
service:
name: httpd
state: started
enabled: yes
In your original problem, "fatal: [172.31.39.20]: FAILED! => {"msg": "The module service was not found in configured module paths"}" suggests that Ansible can't find the "service" module. That shouldn't hapen unless for some reason something is overriding the default module path, which is usually ~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules (trying the directory before the colon, then the directory after the colon). The service module is in the ansible.builtin collection for Ansible Core 2.11+ so it should always be present even if you're using that.
As a third point, I'd also recommend that you don't use the action keyword in your tasks or put all the arguments on the same line by using the = syntax...that's obsolete and most folks think it's harder to read.
You had:
- hosts: demo
user: ansible
become: true
connection: ssh
tasks:
- name: install httpd server on centos
action: yum name=httpd state=present
notify: restart httpd
handlers:
- name: restart httpd
action: service name=httpd state=restarted
I'd write this play as the following:
- name: My first play
hosts: demo
remote_user: ansible
become: true
tasks:
- name: install httpd server on centos
yum:
name: httpd
state: present
notify: restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
I'd probably also add enabled: true to the service module's arguments to make sure that httpd is running after a reboot. There's one catch with this playbook: if httpd is already installed, then restart httpd won't be notified (because that task won't report changed) and there's no other task to make sure that httpd is started!
If you're using Ansible Core 2.11+ I'd change the yum module to ansible.builtin.yum and the service module to ansible.builtin.service since both are in the ansible.builtin Ansible Content Collection included with Ansible Core, but the play would probably run with the old names, too.
Don't trust cut-and-paste from this posting. This posting will put tabs where spaces should be, mess with hyphens, and do all sorts of other things that you'd need to fix. Make sure the indentations look correct visually in your programming editor, and check the playbook file with ansible-lint if you have it.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.