Ansible Quick Start Tutorial

Ansible Quick Start

Introduction

AnsibleIs a simple operation and maintenance of automated tools, only need to use sshprotocol since you can connect the system management, automated order execution, deployment, and other tasks.

Ansible features

Ansible composition structure

    • Ansible
      is Ansiblea tool command, execution core tool; one-time or temporary operation is performed by executing the command.
    • Ansible Playbook
      task script (also known set of tasks), choreography defined Ansibletask set profile by Ansibleperforming the order, yamlformat.
    • Inventory
      Ansible inventory management host, the default is /etc/ansible/hostsfile.
    • Modules
      Ansible executing the command function modules Ansible2.3in version, there are 1039modules. It may also be custom module.
    • Plugins
      add-on module, the module function, often plug type connector, plug-loop, variable plug filter insert, the insert with fewer features.
    • API
      provides application programming interfaces to third-party program called.

Preparing the Environment

IP system CPU name description
192.168.1.30 7 ansible ansible management node
192.168.1.31 CentOS7 linux.node01.com Managed nodes 1
192.168.1.32 CentOS7 linux.node02.com Managed Node 2
192.168.1.33 CentOS7 linux.node03.com Managed node 3
192.168.1.36 CentOS6 linux.node06.com Managed node 6

Ansible installation

1) Configuration epelsource

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ~]# yum clean all
[root@ansible ~]# yum makecache

2) Installationansible

[root@ansible ~]# yum -y install ansible

# 查看ansible版本
[root@ansible ~]# ansible --version
ansible 2.8.0
  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, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 ( 4.8.5-16)]

Ansible Inventory File

Inventory Chinese Documents

InventoryFiles are typically used to define the authentication information to be managed host, such as sshlogin user name, password and keyrelated information. Simultaneous operation of a group of multiple hosts, the relationship between the group and the group is through a host inventoryfile configuration. Configuration file path:/etc/ansible/hosts

Based password to connect

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密码
[webserver]
192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.36 ansible_ssh_port=22ansible_ssh_user = the root ansible_ssh_pass = " 123456 " 


# Method Two host interface numbers Password 
[the webserver] 
192.168 . 1.3 [ . 1 : . 3 ] ansible_ssh_user = the root ansible_ssh_pass = " 123456 " 


# Method Two host interface numbers Password 
[the webserver] 
192.168 . 1.3 [ . 1 : . 3 ] 
[the webserver: VARS] 
ansible_ssh_pass = " 123456 "

Based on secret key connection

Secret key to the managed appliance connection need to create public and private keys, based on concurrent

1) generates a public and private key

[root@ansible ~]# ssh-keygen
[root@ansible ~]# for i in {1,2,3,6}; do ssh-copy-id -i 192.168.1.3$i ; done

2) configure the connection

[ansible the root @ ~] # Vim / etc / ansible / the hosts 
# Method a host + port + key 
[the webserver] 
192.168 . 1.31 : 22 is 
192.168 . 1.32 
192.168 . 1.33 
192.168 . 1.36 

# host alias method + + port key 
[the webserver] 
node1 ansible_ssh_host = 192.168 . 1.31 ansible_ssh_port = 22 is 
node2 ansible_ssh_host = 192.168 . 1.32 ansible_ssh_port = 22 is 
node3 ansible_ssh_host = 192.168 . 1.33 ansible_ssh_port=22
node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22

Use the host group

# Host group, host variable name Password 
[Apache] 
192.168 . 1.36 
192.168 . 1.33 
[apache.vars] 
ansible_ssh_pass = ' 123456 ' 

# host group variable name , host key 
[Nginx] 
192.168 . 1.3 [ . 1 : 2 ] 

# defining a plurality of groups, a group when the other group members 
[webserver: children] #webserver group comprises two subgroups: Apache Nginx 
Apache 
Nginx

Temporary specified inventory

1) First edit a master list of definitions

[root@ansible ~]# vim /etc/dockers
[dockers]
192.168.1.31 ansible_ssh_pass='123456'
192.168.1.32
192.168.1.33

2) the execution order is specifiedinventory

[root@ansible ~]# ansible dockers -m ping -i /etc/dockers -o 
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

Inventory built-in parameter

Ansible Ad-Hoc

Ad-Hoc Chinese documents

hoc-AD - temporary, in ansiblethe need to quickly perform refers to, and does not require a saved command. It means to perform simple command - a command. For complex command was playbooksimilar to saltstackthe state slsstate of the file.

ansible command format

1) Common command parameters

[ansible the root @ ~] # ansible - H 
the Usage: ansible <Host-pattern> [Options]
 - A parameter module MODULE_ARGS #
 -C, - Check Check Syntax #
 - F # FORKS concurrent
 --list- the hosts listed host # list
 - m module name MODULE_NAME #
 -o use streamlined output

2) Examples

[root@ansible ~]# ansible webserver -m shell -a 'uptime' -o
192.168.1.36 | CHANGED | rc=0 | (stdout)  13:46:14 up 1 day,  9:20,  4 users,  load average: 0.00, 0.00, 0.00
192.168.1.33 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:51,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.31 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:50,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.32 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:59,  3 users,  load average: 0.00, 0.01, 0.05

3) Command Description

host-pattern format

Target targethost, the host group matches the way

Match Host

# A destination host 
[root @ ansible ~] # ansible 192.168 . 1.31 -m of ping 

# multiple target host 
[root @ ansible ~] # ansible 192.168 . 1.31 , 192.168 . 1.32 -m of ping 

# all target hosts 
[root @ ansible ~] # ansible All -m the ping

Group matches

# Group configuration information as follows: This defines a nginx apache group and a group 
[the root ansible @ ~] # ansible nginx - List 
  the hosts ( 2 ):
     192.168 . 1.31 
    192.168 . 1.32 
[ansible the root @ ~] # ansible apache - - List 
  the hosts ( . 3 ):
     192.168 . 1.36 
    192.168 . 1.33 
    192.168 . 1.32 

all hosts matching # a group 
[the root ansible @ ~] # -m apache ansible of ping 

# apache group have matched, but not all of the group nginx hosts 
[root @ ansible ~] # ansible ' the Apache:! nginx ' -mping -o
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, " Changed " : to false , " of ping " : " Pong " } 

# match nginx apache group and the group has a machine (and current) 
[the root ansible @ ~] # ansible ' apache: & nginx ' -m of ping - O
 192.168 . 1.32 | SUCCESS => { " ansible_facts " : { " discovered_interpreter_python " : " / usr / bin / Python " }, " changed " :false, "of ping " : " Pong " } 

# match two sets apache group nginx group all machines (union); apache equal ansible, nginx -m of ping 
[ansible the root @ ~] # ansible ' apache: nginx ' -m of ping - O
 192.168 . 1.32 | SUCCESS => { " ansible_facts " : { " discovered_interpreter_python " : " / usr / bin / Python " }, " changed " : to false , " of ping " :"pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

Guess you like

Origin www.linuxidc.com/Linux/2019-06/159057.htm