creating ansible playbook to install vnc - ansible

How would you go about to write an ansible playbook to install vnc on a remote server?. I am using ubuntu 18.04, i have ansible installed and have ran a playbook to install nginx an it successfully work
---
- hosts : test-server
become: true
vars:
ansible_become_pass:"password"
tasks :
- name : install nginx
package : pkg=nginx state=installed
notify:
- start nginx
handlers :
- name : start nginx
service : name=nginx state=started
How can i refactor this or what should i add to install vnc. I am using ubuntu 18.04, ansible version 2.5.1

To answer the first question, I wouldn't write the whole thing by myself to begin with. Lot of people already did it, for example you can try this role https://galaxy.ansible.com/sdarwin/vnc, or look at its sources for some inspiration.
But if it's about improving the playbook as you started it, well, find a tutorial about installing the VNC server you're aiming at (tightvncserver, tigervnc, ...?), then translate the bash commands to Ansible. apt install something translate to package: pkg=something state=installed , etc.

Related

Can anyone help me how to tag particular version to apache and nginx

I have trouble in setting up version for apache and nginx for below playbook.
---
- name: Install appache restart apache and install git
hosts: all
become: true
tasks:
- name : install appache2
package : name=apache2 state=present
- name : restart appache2
command : systemctl restart apache2
command : systemctl stop apache2
- name : uninstall appache2
package : name=apache2 state=absent
- name : Install git
package : name=git state=present
- name : install nginx
package : name=nginx state=present
I will write the examples as if you want to install apache v4.23 (supposedly it exists in your distribution and available in your package repository)
If you are using RHEL distros (Centos - Redhat // Or yum as package manager), that would be the format you will be using "$packagename-$version"
# Apache package name on RHEL is httpd, just putting apache for comparison.
tasks:
- name: install apache2
package:
name: apache2-4.23
state: present
Compared to if you are using apt for package management, that would be the format "$packagename=$version"
tasks:
- name: install apache2
package:
name: apache2=4.23
state: present
And generally it is one of the above 2 syntaxes.. So if you are not sure, try once with "-".. If it does not work, try with "=".
And on a side note, use service module to restart your services instead of using the command module
tasks:
- name: restart apache2
service:
name: apache2
state: started

How to start apache2 service , when it is in inactive state using ansible( When it is inactive state

Already apache2 is installed in my target node
I have to start apache2 service when it is in inactive state.
How to implement such kind of solution in ansible
I have to start apache2 service whenever it is inactive
Command: /sbin/service apache2 status
if the output is showing inactive then only i have to run below command
Command: /sbin/service apache2 start
using adhoc ansible commands you should be able to run:
ansible <host_name_or_ip> -a "service httpd start"
or as part of a role:
- name: Ensure that apache2 is started
become: true
service: name=httpd state=started
Ansible is about describing states in tasks that always produce the same result (idempotence). Your problem then comes down to:
apache must be active on boot
apache must be started
We could also add in a prior separate task:
apache package must be installed on the system
A sample playbook would be
---
- name: Install and enable apache
hosts: my_web_servers
vars:
package_name: apache2 # adapt to your distribution
tasks:
- name: Make sure apache is installed
package:
name: "{{ package_name }}"
state: present
- name: Make sure apache is active on boot and started
service:
name: "{{ package_name }}"
enabled: true
state: started
Running this playbook against your target host will (make sure you change the hosts param to a relevant group or machine):
For the install task:
Install apache if it is not already installed and report "Changed"
Report "Ok" if apache is already installed
For the service task:
Enable and/or start apache if needed and report "Changed"
Report "Ok" if apache is already enabled and started.
Note: in my example, I used the agnostic modules package and service. If you which you can replace with their specific equivalent (apt, yum ... and systemd, sysvinit ...)

Ansible playbook check if service is up if not - install something

i need to install Symantec endpoint security on my linux system and im trying to write a playbook to do so
when i want to install the program i use ./install.sh -i
but after the installation when i run the installation again i get this msg:
root#TestKubuntu:/usr/SEP# ./install.sh -i
Starting to install Symantec Endpoint Protection for Linux
Downgrade is not supported. Please make sure the target version is newer than the original one.
this is how i install it in the playbook
- name: Install_SEP
command: bash /usr/SEP/install.sh -i
I would like if it's possible to maybe check if the service is up and if there is no service then install it or maybe there is a better way doing this.
Thank you very much for your time
Q: "I would like to check if the service is up and if there is no service then install it."
It's possible to use service_facts. For example to check a service is running
vars:
my_service: "<name-of-my-service>"
tasks:
- name: Collect service facts
service_facts:
- name: Install service when not running
command: "<install-service>"
when: "my_service not in ansible_facts.services|
dict2items|
json_query('[?value.state == `running`].key')"
To check a service installed use
json_query('[].key') }}"
(not tested)
Please try something like below.
- name: Check if service is up
command: <command to check if service is up>
register: output
- name: Install_SEP
command: bash /usr/SEP/install.sh -i
when: "'running' not in output.stdout"
Note: I have used running in when condition : If the service command returns something specific, include that instead of running.

How to force Ansible to use sudo to install packages?

I have a playbook than run roles, and logs in the server with a user that has the sudo privileges. The problem is that, when switching to this user, I still need to use sudo to, say, install packages.
ie:
sudo yum install httpd
However, Ansible seems to ignore that and will try to install packages without sudo, which will result as a fail.
Ansible will run the following:
yum install httpd
This is the role that I use:
tasks:
- name: Import du role 'memcacheExtension'
import_role:
name: memcacheExtension
become: yes
become_method: sudo
become_user: "{{become_user}}"
become_flags: '-i'
tags:
- never
- memcached
And this is the tasks that fails in my context:
- name: Install Memcached
yum:
name: memcached.x86_64
state: present
Am I setting the sudo parameter at the wrong place? Or am I doing something wrong?
Thank you in advance
You can specify become: yes a few places. Often it is used at the task level, sometimes it is used as command line parameter (--become, -b run operations with become). It can be also set at the play level:
- hosts: servers
become: yes
become_method: enable
tasks:
- name: Hello
...
You can also enable it in group_vars:
group_vars/exmaple.yml
ansible_become: yes
For your example, using it for installing software I would set it at the task level. I think in your case the import is the problem. You should set it in the file you are importing.
I ended up specifying Ansible to become root for some of the tasks that were failing (my example wasn't the only one failing, and it worked well. The tweak in my environment is that I can't login as root, but I can "become" root once logged in as someone else.
Here is how my tasks looks like now:
- name: Install Memcached
yum:
name: memcached.x86_64
state: present
become_user: root
Use shell module instead of yum.
- name: Install Memcached
shell: sudo yum install -y {{ your_package_here }}
Not as cool as using a module, but it will get the job done.
Your become_user is ok. If you don't use it, you'll end up trying to run the commands in the playbook, by using the user used to stablish the ssh connection (ansible_user or remote_user or the user used to execute the playbook).

Ansible differentiate local and remote connection

We have lots of playbooks that we use to setup our remote instances. We would like to use those playbooks when bringing up our local environment for test purposes as well.
Is it possible to differentiate between a playbook running locally and remotely?
I'm looking for something like:
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
when: ansible.connection_type == 'local'
Which means I only want to install apache when running ansible against my local environment.
I will then execute it with:
ansible-playbook -i /root/ansible-config/ec2.py -c local myplaybook.yml
Is it possible?
There is ansible_connection variable for every host.

Resources