Issues with Running Ansible Playbook on Linux T2 Instance Localhost - linux

I am trying to figure out why my Ansible playbook is not working. I have tried 20 different ways of indenting the playbook but it is not working.
I am currently launching an Amazon Linux t2 instance and then installing ansible using following commands:
sudo yum update -y
sudo amazon-linux-extras install ansible2 -y
Then I create a playbook first.yml using "vim first.yml" , and the playbook looks like this:
---
- name: update web servers
hosts: localhost
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
I run playbook using "ansible-playbook first.yml" and get the following error:
ERROR! We were unable to read either as JSON nor YAML, these are the
errors we got from each: JSON: No JSON object could be decoded
Syntax Error while loading YAML. mapping values are not allowed in
this context
The error appears to be in '/home/ec2-user/first.yml': line 7, column
8, but may be elsewhere in the file depending on the exact syntax
problem.
The offending line appears to be:
tasks:
^ here
I would appreciate any help, thank you !

Related

Getting an error when running pg_dump on cloud foundry

I have a manifest that looks like the below:
applications:
- name: sfcbackups-staging
memory: 1G
disk_quota: 4GB
instances: 1
buildpacks:
- https://github.com/cloudfoundry/apt-buildpack
- nodejs_buildpack
services:
- sfcstaging-db
And an apt.yml that looks like this to install Postgres
---
cleancache: true
keys:
- https://www.postgresql.org/media/keys/ACCC4CF8.asc
repos:
- deb https://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
packages:
- postgresql-client-11
But whenever I try running the command pg_dump (either via node or ssh) I get the following error:
Can't locate PgCommon.pm in #INC (you may need to install the PgCommon module)(#INC contains:
/etc/perl
/usr/local/lib/x86_64-linux-gnu/perl/5.26.1
/usr/local/share/perl/5.26.1
/usr/lib/x86_64-linux-gnu/perl5/5.26
/usr/share/perl5
/usr/lib/x86_64-linux-gnu/perl/5.26
/usr/share/perl/5.26
/usr/local/lib/site_perl
/usr/lib/x86_64-linux-gnu/perl-base)
at ./pg_dump line 22.
BEGIN failed--compilation aborted at ./pg_dump line 22.
There's a couple of things I have tried so far but to no avail:
Added the following to my apt.yml
- libdbd-pg-perl
- libpq-dev
Running cpan install DBD::Pg (via SSH) to see if this would fix the issue however the same error occurs.
Has anyone had any similar issues?
Thanks in advance

Not able to set environment variable for sudo -u "user"

I am at my wits end trying to figure this out
When I execute the following command:
sudo -u icinga '/usr/lib//nagios/plugins/check_db2_health' '--database' 'mydatabase' '--environment' 'DB2DIR=/opt/IBM/db2/V11.1.4fp5a' '--environment' 'DB2INSTANCE=mydatabase' '--environment' 'INSTHOME=/srv/db2/home/mydatabase' '--report' 'short' '--username' 'icinga' '--mode' 'connection-time' '--warning' '50'
The output as follow
[DBinstance : mydatabase] Status : CRITICAL - cannot connect to mydatabase. install_driver(DB2) failed: Can't load '/usr/lib/nagios/plugins/PerlLib/lib/perl5/site_perl/5.18.2/x86_64-linux-thread-multi/auto/DBD/DB2/DB2.so' for module DBD::DB2: libdb2.so.1: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.18.2/x86_64-linux-thread-multi/DynaLoader.pm line 190.
at (eval 10) line 3.
Compilation failed in require at (eval 10) line 3.
Perhaps a required shared library or dll isn't installed where expected
at /usr/lib//nagios/plugins/check_db2_health line 2627.
But when I login to the user icinga using su - icinga
And run
'/usr/lib//nagios/plugins/check_db2_health' '--database' 'mydatabase' '--environment' 'DB2DIR=/opt/IBM/db2/V11.1.4fp5a' '--environment' 'DB2INSTANCE=mydatabase' '--environment' 'INSTHOME=/srv/db2/home/mydatabase' '--report' 'short' '--username' 'icinga' '--mode' 'connection-time' '--warning' '50'
It works fine.
How do I setup environment variables when sudo - u icinga command is fired ?
I am on a SUSE linux
I am kind of trying to setup a global environment variable just like the environment variable in icinga which can work across all commands executed on the server without have to use sudo -E etc because I cannot change the way icinga calls the plugin
you need to run the db2profile when you sudo
sudo -u icinga sqllib/db2profile; '/usr/lib//nagios/plugins/check_db2_health' ...

Handle YUM package installation deployment for target environments(dev/prod/systest) using ansible playbook

Need to handle YUM package installation deployement process with different versions/packages, for target environments(dev/prod/systest) using ansible playbook.
NOTE: I have gone through groups_var and hosts_var concept but did not understand if multiple packages with different versions can handled for deployment in multiple environment based on input
As you found out, this separation can be achieved by using group_vars and host_vars. These are loaded in relation to the path of inventory file.
Simple example tasks like below will install different versions in dev and prod environments as explained below.
Example playbook1.yml:
- hosts: appservers
tasks:
- name: install app-a
yum:
name: 'app-a-{{ app_a_version }}'
- name: install app-b
yum:
name: 'app-b-{{ app_b_version }}'
Consider the example directory structure separating each environment's inventory:
dev/hosts
prod/hosts
systest/hosts
Each inventory file will contain hosts/groups for that environment.
Dev environment:
Example dev/hosts:
[appservers]
appserver1.dev
appserver2.dev
Then we can have variables specific to this environments in dev/group_vars/appservers.yml:
---
app_a_version: 1.1
app_b_version: 5.5
Will install app-a-1.1 and app-b-5.5 when run as:
ansible-playbook playbook1.yml -i dev/hosts
Prod environment:
Example prod/hosts:
[appservers]
appserver1.prod
appserver2.prod
And variables defined in prod/group_vars/appservers.yml:
app_a_version: 1.0
app_b_version: 5.0
But in prod it will install app-a-1.0 and app-b-5.0 when run as:
ansible-playbook playbook1.yml -i prod/hosts
host_vars work in similar way, and can be used to provide variables specific to each host of the inventory rather than groups in inventory.

How to set ansible mitogen strategy on playbook?

I'm trying to make it work mitogen strategy on my ansible playbook. I'm following tutorial on mitogen tutorial. My python version is 3.6 and ansible version is 2.7.10.
Mitogen is installed in /usr/local/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy
When I try to add keys on my playbook as:
- hosts: "{{ host_group | default('host-list') }}"
...
strategy: mitogen_linear
strategy_plugins: /usr/local/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy
I have the following error:
ERROR! 'strategy_plugins' is not a valid attribute for a Play
Also, I'm trying to configure it as a environment variable in playbook execution:
command = ['ANSIBLE_STRATEGY_PLUGINS=/usr/local/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy', 'ANSIBLE_STRATEGY=mitogen_linear', 'ansible-playbook', '-ihosts', 'ansible_scripts/inventory.yml']
process = subprocess.Popen(command, stdout=subprocess.PIPE)
Here is not finding directory:
FileNotFoundError: [Errno 2] No such file or directory: 'ANSIBLE_STRATEGY_PLUGINS=/usr/local/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy': 'ANSIBLE_STRATEGY_PLUGINS=/usr/local/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy'
How I can correctly configure mitogen strategy on my playbook? How I can make it work?
This might be some misconfiguration "No such file or directory". Try the configuration file. For example put into the [defaults] section:
$ grep strategy /etc/ansible/ansible.cfg
strategy_plugins = /usr/local/ansible/plugins/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
Fit the configuration file and path to your needs. (Works for me with ansible 2.7.9 and mitogen-0.2.6)
FWIW, if you want to automate the installation and configuration see plugins.yml and example of vars.

How to let ansible answer "yes" to everything sendmailconfig asks

I'm setting up a server with ansible, and I want to install and configure sendmail. To do this I first install it using apt, and then I need to run sendmailconfig AND asnwer y to all the questions it asks.
That last part is the hardest I think. sendmailconfig doesn't have a -y flag to answer yes to everything, so how do I get Ansible to simple agree to all questions it asks?
Just use the yes shell utility,
yes 'y' | <command-name>
# ^^The repeated string being 'yes' as the OP had asked.
From the man page,
NAME
yes - output a string repeatedly until killed
SYNOPSIS
yes [STRING]...
yes OPTION
DESCRIPTION
Repeatedly output a line with all specified STRING(s), or 'y'.
Basically, you would need to perform two tasks: update hosts and run sendmailconfig.
Note: If you are running Ansible 2.5.0 you might need to install the pexpect module on the remote host, so include this task into your tasks file. For example:
- name: Update hosts
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: '127.0.0.1 localhost {{ ansible_host }}'
owner: root
group: root
mode: 0644
- name: Install pexpect module
raw: sudo apt-get -y install python-pexpect
- name: Configure sendmail
expect:
command: sendmailconfig
responses:
Question:
- Configure sendmail with the existing /etc/mail/sendmail.conf? [Y]: y
- Configure sendmail with the existing /etc/mail/sendmail.mc? [Y]: y
- Reload the running sendmail now with the new configuration? [Y]: y
timeout: 30

Resources