playbooks framework for the deployment of remote host

Python environment and into the ansible

Python3.6 into the virtual environment

#su - deploy

#source .py3-a2.5-env/bin/activate

Load ansible 2.5 version

#source .py3-a2.5-env/ansible/hacking/env-setup -q

Verify ansible loading effect

#ansible-playbook --version

Write playbooks framework

Create a new directory and multiple directories

#mkdir test_playbooks

#cd test_playbooks/

#mkdir inventory roles

Into inventory, create testenv file,

# Cd inventory

#vi testenv

Add the target host service address

[testservers]
test.example.com

[testservers:vars]
server_name=test.example.com
user=root
output=/root/test.txt

Return to the parent directory

#cd ../

Into the roles directory, create a subdirectory

#cd roles

#mkdir -p testbox/tasks

Enter the subdirectory, create main.yml files, playbooks as the main configuration file

#cd testbox/tasks/

#vi main.yml

Add test tasks, save and exit

- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"

Return to the test_playbooks directory

#cd ../../..

Playbooks create a task entry file, save and exit

#vi deploy.yml

- hosts: "testservers"     # 对应testenv文件下主标签,用定义调用标签下目标主机
  gather_facts: true       # 获取目标主机信息
  remote_user: root        # 告诉ansible在目标主机下,使用root账户权限,进行所有系统的文件操作
  roles:            # 进入roles下testbox任务目录,进行接下来的任务执行
    - testbox

Tree in print playbooks directory

#tree .

Configure key authentication ansible host and directory host

Returns the root user

#su - root

Edit hosts

#vi /etc/hosts

Add dns records test.example.com

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.96.151 test.example.com

Return deploy user

#exit

To deploy a user creates a key authentication

#ssh-keygen -t rsa

Enter has been

This time the public and private keys are generated in the same directory /home/deploy/.ssh/

Designated deploy user's public key

#ssh-copy-id -i /home/deploy/.ssh/id_rsa.pub [email protected]

Enter the target host password, you can establish a connection

Test connection to the host without a password

#ssh [email protected]

Testing is completed, the user is returned to deploy next

#exit

Test_playbooks into the directory, execute files under deploy.yml ansible-playbook. At this point execution is complete playbooks

#ansible-playbook -i inventory/testenv ./deploy.yml

Log on to the target host

#ssh [email protected]

View the current directory test.txt. Successful write parameters into the

#cat test.txt

Successfully music playing to the audience! !

Guess you like

Origin www.cnblogs.com/joy-sir/p/12162911.html