部署ansible以及简单示例,部署ansible



配置ssh互信

1.在ansible所在的主机下生成公钥/私钥对。
ssh-keygen -t rsa

部署ansible

yum install ansible

注:ansible安装完成,配置完成之后,执行ansible all -m ping 命令有可能报错:
selinux but python bindings (libselinux-python) aren’t installed
执行
yum install libselinux-python
修改配置
编辑配置文件,添加主机组以及主机ip

/etc/ansible/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 

# Ex 3: A collection of database servers in the 'dbservers' group 

## [dbservers] 
## 
## db01.intranet.mydomain.net 
## db02.intranet.mydomain.net 
## 10.25.1.56 
## 10.25.1.57 

# Here's another example of host ranges, this time there are no 
# leading 0s: 

## db-[99:101]-node.example.com 

[test] 
10.1.1.1

注:如果执行ansible-playbook 命令时,总碰到 shared connection to host xxxx closed的错误,修改 /etc/ansible/ansible.cfg

[ssh_connection]
# ssh arguments to use
# Leaving off ControlPersist will result in poor performance, so use
# paramiko on older platforms rather than removing it, -C controls compression use
# ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
ssh_args = -C -o ControlMaster=no -o ControlPersist=60s

编写脚本
ansible-playbook:
vim ansible_playbook_update2.yml

---
- hosts: test              ---- test是/etc/ansible/hosts 文件中配置的host组
  serial: 1                ---- 可有可无,打开多个tty时,控制执行顺序使用
  vars:
     files_1: '{{ files_1 }}'   ----定义变量
     user_2: '{{ user_2 }}'
  remote_user: root     ----指定远端执行时的用户,agent部署时可有可无
  tasks:
   - name: copy files
     script: /root/test_ansible_pass_parameters.sh {{ files_1 }} {{ user_2 }}
     ignore_errors: yes    ----忽略shell脚本执行返回的结果。因playbook只把shell脚本返回值为 0 的情况当做 ok,其他值全当做 fail
     register: output     ----可有可无,用来记录playbook的stdout

   - debug: msg='{{ output.stdout_lines }}'    ----打印playbook的stdout
   - debug: msg='{{ output.stderr }}'        ----打印playbook的stderr

Shell脚本:
vim /root/test_ansible_pass_parameters.sh

#!/bin/bash

SERVER_IP=$2
echo "i'm running"
echo $#
echo $1
echo $2
sed -i -e "s/SERVER_IP=/SERVER_IP=$SERVER_IP/g" /root/test.txt

注意: ansible-playbook的所有:之后,都必须要有空格!!

执行ansible-playbook

命令:
ansible-playbook ansible_playbook_update2.yml -v --extra-vars "files_1=/root/test.txt user_2=10.1..1.1"

结果:

Using /etc/ansible/ansible.cfg as config file

PLAY [test] ********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [10.1.1.1]

TASK [copy files] **************************************************************************************************
changed: [10.1.1.1] => {"changed": true, "rc": 0, "stderr": "Connection to 10.1.1.1 closed.\r\n", "stdout": "i'm running\r\n2\r\n/root/test.txt \r\n10.1.1.1\r\n", "stdout_lines": ["i'm running", "2", "/root/test.txt", "10.1.1.1"]}

TASK [debug] *******************************************************************************************************
ok: [10.1.1.1] => {
    "changed": false,
    "msg": [
        "i'm running",
        "2",
        "/root/test.txt",
        "10.1.1.1"
    ]
}

TASK [debug] *******************************************************************************************************
ok: [10.1.1.1] => {
    "changed": false,
    "msg": "Connection to 10.1.1.1 closed.\r\n"
}

PLAY RECAP *********************************************************************************************************
10.1.1.1            : ok=4    changed=1    unreachable=0    failed=0

10.1.1.1所在主机的执行结果:

[root@localhost ~]# cat test.txt
SERVER_IP=10.1.1.1
hello
test
[root@localhost ~]#

相关内容

    暂无相关文章