Introduction to Ansible, common commands and basic configuration (6 machine examples)

What is ansible

Ansible is an IT automation and DevOps software launched in 2013. It was acquired by Redhat in 2015. It is developed based on Python and combines the advantages of many old operation and maintenance tools to achieve batch operating system configuration, batch program deployment, batch running commands and other functions.

Ansible can achieve:

1. Automated deployment of APP
2. Automated deployment of configuration items
3. Automated continuous delivery
4. Automated (AWS) cloud service management

Why choose ansible

Choosing a configuration management software is nothing more than weighing the pros and cons from several points:
1. Activeness (community)
2. Learning cost
3. Use cost
4. Coding language
5. Performance
6. Whether it is widely used

Ansible advantages

1. Only need SSH and Python to use
2. No client
3. Ansible is powerful and rich in modules
4. Easy to use, low threshold
5. Based on Python development, it is easier to do secondary development
6. Use more companies, community active.

ansible feature 1

1. Modular design, call specific modules to complete specific tasks
2. Implementation based on Python language-
paramiko-
PyYAML (semi-structured language)
-Jinja2
Its modules support standard output formats such as JSON, and can be rewritten in any programming language

ansible feature 2

1 Simple deployment
2 Master-slave mode work
3 Support custom modules
4 Support playbook
5 Easy to use
6 Support multi-layer deployment
7 Support heterogeneous IT environment

work process

The general execution process of ansible
read the configuration → grab the full machine & group list → use host-pattern to filter the machine list → determine the execution module and configuration according to the parameters → Runner execution return → output, end

======================================================================================

ansible installation

Software dependencies
For the management host-Python 2.6 or Python 2.7
ansible is required to use the following modules, all of which need to be installed
1.paramiko 2.PyYAML 3.Jinja2 4.httplib2 5.six


For the managed host-Ansible manages the machine through the SSH protocol by default

  1. Ansible manages the machine through SSH protocol by default
  2. The managed host needs to enable the ssh service and allow the ansible host to log in
    . 3. Python 2.5 or above must be installed on the managed node
    4. If SElinux is turned on on the managed node, libselinux-python needs to be installed

Real machine management

Yum installation

1. Copy the software package to the real machine FTP shared directory
2. Update the index file
createrepo /var/ftp/ file name
createrepo --update.

Installation and verification

1. Configure yum configuration file on the ansible hosting host
2. Install yum install ansible
3. Verify ansible --version


Start 6 virtual machines 2CPU 1.5G or more memory, 10G or more hard disk, 1 network card
CPU name IP address Character
ansible 192.168.1.40 Management host
web1 192.168.1.41 Hosting
web2 192.168.1.42 Hosting
db1 192.168.1.43 Hosting
db2 192.168.1.44 Hosting
cache 192.168.1.45 Hosting

Host definition and grouping
  • After installing ansible, you can do some simple tasks ansible configuration file search order
  • First detect the configuration file defined by the ANSIBLE_CONFIG variable
  • Secondly, check the ./ansible.cfg file in the current directory
  • Check again the ~/;ansible.cfg file in the current user's home directory
  • Finally check the /etc/ansible/ansible.cfg file
  • etc/ansible/ansible, cfg is the default configuration file path of ansible

Configure ansible management machine

]# eip 40
]# echo ansible > /etc/hostname
]# hostname ansible
]# reboot
]# vim /etc/yum.repos.d/local.repo
[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/centos-1804"
enabled=1
gpgcheck=0


[ansible]
name=ansible
baseurl="ftp://192.168.1.254/ansible"
enabled=1
gpgcheck=0
~
]# yum repolist
]# yum -y install ansible
]# ssh-keygen -t rsa  -b 2048 -N '' -f key
]# for i in web1 web2 db1 db2 cache ; do ssh-copy-id -i key.pub ${i};done
]# vim /etc/hosts
]# 192.168.1.40 ansible
192.168.1.41 web1
192.168.1.42 web2
192.168.1.43 db1
192.168.1.44 db2
192.168.1.45 cache
]# for i in web1 web2 db1 db2 cache ; do rsync -av /etc/hosts root@${i}:/etc/hosts ;done
]# vim /etc/ansible/hosts
[web]
web1
web2

[db]
db1
db2

[other]
cache

[app:children]
web
db
[all:vars]
ansible_ssh_private_key_file="/root/key"
 ]#vim ansible.cfg
[defaults]
inventory=myhost
host_key_checking=false
]# ansible app1 -m ping
]# cd
]# ansible web -m shell -a 'echo ${HOSTNAME}'
]# ansible cache -m shell -a 'touch testfile'
]# ansible web1,db2 -a "useradd nb"
]# ansible web1,db2 -a 'echo 123 |passwd --stdin nb'
]# ansible web1,db2 -a 'id nb'
]# vim 1.sh
#!/bin/bash
id nb
if [ $?  != 0 ];then
useradd wk
echo 456 |passwd --stdin wk
fi
]# ansible all -m script -a "/root/1.sh"
]# vim /etc/resolv.conf
]# ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'
]# ansible all -a 'cat /etc/resolv.conf'
]# ansible db -m yum -a 'state=installed name=mariadb-server'
]# ansible db -m service -a 'state=started name=mariadb enabled=yes'
]# ansible-doc script
]# ansible db -m script -a '/root/1.sh'

================================================== ==============================1.
Common command module

  1. ansible all -m ping
  2. ansible all -m command -a ‘’
  3. ansible all -m shell -a ‘’
  4. ansible all -m lineinfile -a ‘’
  5. ansible all -m replace -a ‘’
  6. ansible all -m copy -a ‘scr: dest:’
  7. ansible all -m yum -a ‘state= name’
  8. ansible all -m script -a ‘/root/1.sh’
  9. ansible all -m setup -a ‘’
  10. 10ansible all -m service -a ‘’

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104288414