Ansible
Ansible
Ansible
2 mkdir .ssh
3 cd .ssh/
4 cd ..
7 cd .ssh/
9 ll
10 history
Install python pip and then run pip install ansible
Ansible 1.2.1 and later have host key checking enabled by default.
If a host is reinstalled and has a different key in ‘known_hosts’, this will result in an error message
until corrected. If a host is not initially in ‘known_hosts’ this will result in prompting for confirmation of
the key, which results in an interactive experience if using Ansible, from say, cron. You might not want
this.
If you understand the implications and wish to disable this behavior, you can do so by
editing /etc/ansible/ansible.cfg or ~/.ansible.cfg:
[defaults]
host_key_checking = False
To install pip and wheel for the system Python, there are two options:
1. Enable the EPEL repository using these instructions. On EPEL 6 and EPEL7,
you can install pip like so:
2. sudo yum install python-pip
On EPEL 7 (but not EPEL 6), you can install wheel like so:
Since EPEL only offers extra, non-conflicting packages, EPEL does not offer
setuptools, since it’s in the core repository.
3. Enable the PyPA Copr Repo using these instructions [1]. You can install pip
and wheel like so:
4. sudo yum install python-pip python-wheel
[pypa-pypa]
name=Copr repo for pypa owned by pypa
baseurl=https://copr-be.cloud.fedoraproject.org/results/pypa/pypa/epel-7-
$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/pypa/pypa/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
How to create password less authentication
ssh ansibleadmin@52.66.68.142
ssh-copy-id ansibleuser@remotehost
when doing ssh into ubuntu then all files and folders should be owned by ansibleadmin within home
directory
Make entry in
[webserver]
13.127.58.87
[devel]
52.66.68.142
Running the above did not preserve the permission so will run with - -become
For my system it is
/usr/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled/tests/ansible.cfg
This will copy a file from control node to the hosts
Modules in Ansible
Installing latest tree package through yum
Command to ensure a service is running
The default location for the host inventory file is /etc/ansible/hosts. The ansible*
commands will use a different host inventory file when they are used with the --inventory
PATHNAME option, -i PATHNAME for short.
Ansible host inventories can include groups of host groups. This is accomplished with the
:children suffix. The following example creates a new group, called nwcapitols, that
includes all of the hosts from the olympia and salem groups.
[olympia]
washington1.example.com
washington2.example.com
[salem]
oregon01.example.com
oregon02.example.com
[nwcapitols:children]
olympia
salem
Ranges match all the values between START and END, inclusive. Consider the following examples:
• 192.168.[4:7].[0:255] - all IP addresses in the 192.168.4.0/22 network (192.168.4.0
through 192.168.7.255).
• server[01:20].example.com - all hosts named server01.example.com through
server20.example.com.
Managed hosts
Managed hosts do not need to have any special Ansible agent installed. They do need to have
Python 2, version 2.4 or later installed. If the version of Python installed on the managed host is
earlier than Python 2.5, then it must also have the python-simplejson package installed.
At times, it is desirable to use Ansible to manage systems that can not have Python
installed. Systems in this category, such as network routers, can be managed using
Ansible's raw module. Arguments passed to this module are run directly through the
configured remote shell instead of going through the module subsystem. However, in
most other cases the raw module should be avoided.
Wildcards
Another method of accomplishing the same thing as the all host pattern is to use the '*'
wildcard character, which matches any string. The following example shows how the '*' host
pattern can be used to reference all hosts defined in an inventory.
[student@controlnode ~]$ ansible '*' --list-hosts
hosts (6):
labhost1.example.com
test1.example.com
labhost2.example.com
test2.example.com
In contrast, when used in conjunction with the '&' character to separate groups in a host pattern,
the ':&' characters denote the intersections of two groups in the inventory. The following
example shows the use of a host pattern referencing hosts that are members of both the lab
and datacenter1 groups.
Another example
/etc/ansible/files/
OR
/etc/ansible/
Handlers :- How to use Handlers as below
Yaml is a set of key value pairs
Suppose you have created your own file example myhosts then you can use -i switch to provide
the filename
FORK controls how many hosts will run the adhoc command together if we have multiple hosts in
the hosts file:-
[ansible@master ansible]$ ansible all -a "ls -l" -f 100
192.168.38.47 | SUCCESS | rc=0 >>
total 0
---
- hosts: appserver
become: yes
tasks:
- name: Ensure apache is installed
yum:
name: httpd
state: present
- hosts: dbserver
become: yes
tasks:
- name: Ensure MYSQL server is installed
yum:
name: mysql-server
state: present
USER CREATION:
Using variables with variable file
Vprofile
$ cat ansible.cfg
[defaults]
host_key_checking = False
inventory=int-vprohosts
appserver.yml
DBDeploy.yml
DBSERVER.yml
Installing Wordpress
To open
http:// http://54.153.49.186
http://docs.ansible.com/ansible/latest/playbooks_variables.html#registered-variables
Installation
The playbook to install the Jenkins server on the CentOS VM is given below:
---
hosts: jenkins
gather_facts: true
become: yes
become_method: sudo
tags: [jenkins]
tasks:
name: ‘*’
update_cache: yes
package:
state: latest
with_items:
- java-1.8.0-openjdk
- git
- texlive-latex
- wget
rpm_key:
key: http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
state: present
package:
state: latest
with_items:
- jenkins
shell: iptables -I INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
name: jenkins
state: started
- wait_for:
port: 8080
The playbook first updates the Yum repository and installs the Java OpenJDK software
dependency required for Jenkins. The Git and Tex Live LaTeX packages are required to
build our project github.com/shakthimaan/di-git-ally-managing-love-letters. We then
download the Jenkins repository file and import the repository GPG key. The Jenkins
server is then installed, port 8080 is allowed through the firewall, and the script waits for
the server to listen on port 8080. The above playbook can be invoked using the following
command:
With the Command module the command will be executed without being proceeded through a shell. As a consequence
variables like $HOME are not available. And also stream operations like <, >, | and & will not work.
The Shell module runs a command through a shell, by default /bin/sh. This can be changed with the option executable
and redirection are here therefor available.
The command module is more secure, because it will not be affected by the user’s environment.
--extra-vars