Ansible module to stop and start `ssh` service - ansible

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

Related

Ansible playbook error while running on - hosts:

write a task in main.yml to stop and start service in service "ssh" using service module in ansible.
---
- hosts: localhost
become: true
become_method: sudo
tasks:
- name: stop service
service:
name: ssh
state: stopped
- name: start service
service:
name: ssh
state: started
when run it's giving below error
[WARNING]: Unable to parse /projects/challenge/localhost as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_module/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be
---
- hosts: localhost
^ here
Firstly, you should be able to do SSH to localhost.
You can try,
ssh user#localhost date
You can create a hosts file and name it hosts and add the following content to it.
[localhost]
localhost
[localhost:vars]
ansible_ssh_user=user
ansible_ssh_pass=pass
ansible_sudo_pass=sudopass
And run the playbook as
ansible-playbook -i hosts main.yml
Using command module was able to stop and start the service, using sudo service ssh stop and sudo service ssh start served my purpose.
was not able to do so with service module, still don't know about that
Resolved at my end by using complete path for .yml file
ansible-playbook -i /etc/ansible/hosts myfirstplaybook.yml

Ansible module bigip_pool_member for BIGIP always returning "Changed" status

I am trying to add pool members to a bigip pool using bigip_pool_member.
Tested on ansible version 2.5 and 2.6
Result - Returns changed ALWAYS, even when it is not making any changes.
Involcation command:
ansible-playbook -i test_inventory add_pool_members.yaml --extra-vars '{"hostgroup": "test-bigip"}'
I am wondering if anyone has insights into what could be going on ?
The contents of the playbook are as under
--
- hosts: "{{ hostgroup }}"
gather_facts: no"
tasks:
- name: Add servers to connection pool
bigip_pool_member:
user: username
password: password
server: "{{inventory_hostname}}"
validate_certs: no
state: present
partition: test
pool: testpool
host: 14.34.45.X
name: test-server
port: 80
description: test
delegate_to: localhost
Run Result
PLAY [f5-test] *****************************************************************************
TASK [Add servers to connection pool ] *****************************************************
changed: [f5-test -> localhost]
PLAY RECAP *********************************************************************************
f5-test : ok=1 changed=1 unreachable=0 failed=0
This could be related to this known bug in the module.
When running playbook with bigip_pool_member module with state: present against live device, each run results in change being made when in reality there's no need for a change.
I'm nor f5 neither network expert but from I understand that happen if you set a monitor to your pool.
There is a pull request already with fixes related to correct state of down machine. Check if it applies to you, else I would suggest to add a detailed comment on the bug.

Ansible - start tomcat on host [duplicate]

This question already has answers here:
How to run tomcat catalina script on Ansible
(2 answers)
Closed 5 years ago.
I've been working on ansible playbook to download and start tomcat in a host.
This is my inventory host file:
[group1]
machine1 ansible_host=10.40.0.168
I have group1.yml file in my group_vars:
---
ansible_ssh_user: user
ansible_ssh_pass: pass
ansible_sudo_pass: passp
My playbook is:
---
- hosts: group1
sudo: yes
tasks:
- name: Update all packages to the latest version
apt:
upgrade: dist
- name: Download tomcat
get_url: url=http://mirrors.up.pt/pub/apache/tomcat/tomcat-9/v9.0.1/bin/apache-tomcat-9.0.1-fulldocs.tar.gz dest=/opt/apache-tomcat-9.0.1.tar.gz
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /opt/apache-tomcat-9.0.1.tar.gz
dest: /opt/
remote_src: yes
- name: Run Tomcat
shell: ./startup.sh
args:
chdir: /opt/apache-tomcat-9.0.1/bin
I try to run ./startup.sh in /opt/apache-tomcat-9.0.1/bin folder to start tomcat.
I run the following command:
ansible-playbook playbookname.yml
If I run ./startup.sh in a host machine it works fine, but when I run it from the control machine I get:
PLAY [group1] **********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [myname]
TASK [Update all packages to the latest version] ***********************************************************************
ok: [myname]
TASK [Download tomcat] *************************************************************************************************
ok: [myname]
TASK [Unarchive a file that is already on the remote machine] **********************************************************
ok: [myname]
TASK [Run Tomcat] ******************************************************************************************************
changed: [myname]
PLAY RECAP *************************************************************************************************************
myname : ok=5 changed=1 unreachable=0 failed=0
After this I try to open tomcat, but it's not running on the host.
How can I start tomcat from ansible?
You should add it as a service, eg as below:
service file: /etc/systemd/system/tomcat.service (It should be in target destination machine)
File should contain as below, (Adjust to your java environment)
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/jre
Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-8.0.47/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.47
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-8.0.47
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/apache-tomcat-8.0.47/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
Then start the server with below systemd ansible module,
- name: enable tomcat startup
systemd:
name: tomcat
enabled: yes
state: restarted
become: true
You should use system tools for that, like Systemd etc. depending on your OS.
Ansible should only create the service file and start the service. Running the servie is not the job of Ansible.
Possible way to do that: https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7#install-tomcat

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 Roles and handlers - Cannot get role handlers to work

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.

Resources