Learning Ansible: PART 1


Installing Ansible:



[root@licvdo1000 ~]# yum install ansible
[root@licvdo1000 ~]# ansible --version
ansible 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#

Ansible Version Check:


[root@licvdo1000 ~]# ansible-playbook --version
ansible-playbook 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]
[root@licvdo1000 ~]# ansible-galaxy --version
ansible-galaxy 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-galaxy
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]

Default Ansible Configuration:


[root@licvdo1000 ansible]# pwd
/etc/ansible
[root@licvdo1000 ansible]# more ansible.cfg
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False


[root@licvdo1000 ansible]# more hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

Creating your own configuration:


[root@licvdo1000 ansible]# pwd
/home/f776xb6/ansible
[root@licvdo1000 ansible]# ll
total 8
-rw-r--r--. 1 root root 59 Aug 16 12:04 ansible.cfg
-rw-r--r--. 1 root root 71 Aug 16 12:30 hosts
[root@licvdo1000 ansible]#

[root@licvdo1000 ansible]# cat ansible.cfg
[defaults]

inventory      = /home/f776xb6/ansible/hosts

[root@licvdo1000 ansible]# cat hosts
[testserver]
licvdo1001

[control]
licvdo1000 ansible_connection=local
[root@licvdo1000 ansible]#


Listing hosts from inventory and using wild characters:


[root@licvdo1000 ansible]# ansible --list-hosts all
  hosts (2):
    licvdo1000
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts '*'
  hosts (2):
    licvdo1000
    licvdo1001
[root@licvdo1000 ansible]# pwd
/home/f776xb6/ansible
[root@licvdo1000 ansible]# ansible --list-hosts testserver
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts licvdo1001
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts test*
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts testserver:control
  hosts (2):
    licvdo1001
    licvdo1000
[root@licvdo1000 ansible]# ansible --list-hosts testserver,control
  hosts (2):
    licvdo1001
    licvdo1000
[root@licvdo1000 ansible]# ansible --list-hosts testserver[1]
 [WARNING]: No hosts matched, nothing to do

  hosts (0):
[root@licvdo1000 ansible]# ansible --list-hosts testserver[0]
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts \!control
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts \!test*
  hosts (1):
    licvdo1000
[root@licvdo1000 ansible]#

Running Ad-hoc commands:


[root@licvdo1000 ansible]# ansible -m ping all
 [WARNING]: Platform linux on host licvdo1000 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
 [WARNING]: Platform linux on host licvdo1001 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@licvdo1000 ansible]# ansible -m command -a "hostname" all
 [WARNING]: Platform linux on host licvdo1000 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | CHANGED | rc=0 >>
licvdo1000

 [WARNING]: Platform linux on host licvdo1001 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | CHANGED | rc=0 >>
licvdo1001

[root@licvdo1000 ansible]#


#### command is the default module ###############


[root@licvdo1000 ansible]# ansible -a "hostname" all
 [WARNING]: Platform linux on host licvdo1000 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | CHANGED | rc=0 >>
licvdo1000

 [WARNING]: Platform linux on host licvdo1001 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.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | CHANGED | rc=0 >>
licvdo1001

[root@licvdo1000 ansible]#


Writing and executing playbooks:


[root@licvdo1000 ansible]# cat hostname.yml
---
 - hosts: all
   tasks:
     - name : get server hostname
       command: hostname


[root@licvdo1000 ansible]# ansible-playbook hostname.yml

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [licvdo1000]
ok: [licvdo1001]

TASK [get server hostname] ***********************************************************************************************************************************
changed: [licvdo1000]
changed: [licvdo1001]

PLAY RECAP ***************************************************************************************************************************************************
licvdo1000                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
licvdo1001                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


No comments:

Post a Comment

Installation of Jenkins on Linux and Deployment NGINX through Jenkins

Installation of Jenkins: [root@worker1 ~]# useradd -c "jenkins user" jenkins [root@worker1 ~]# passwd jenkins Changing passw...