ansible

Ansible的安装部署、配置文件解析、命令模块

安装:

环境准备 :python环境 、setuptools 、pip(可选)

1、通过pip安装 :easy_install pip     

      安装Ansible:pip install ansible

      卸载  pip uninstall ansible

 2、源码安装

获取源码  从github上获取   进入源码目录  运行source ./hacking/env-setup

centos上  配置epel源yum -y install epel-release  yum install -y ansible    

ubuntu上   apt-get install software-properties-common

       apt-add-repository ppa:ansible/ansible

       apt-get update

       apt-get install ansible

ansible运行图

配置文件路径优先级:    export ANSIBLE_CONFIG

           ./ansible.cfg

           ~/.ansible.cfg

           /etc/ansible/ansible.cfg   (源码安装会在这生成)

配置文件获取  :  若以上路径没有可以在github上取下来  https://raw.github.com/ansible/ansible/devel/examples/ansible.cfg  

配置文件详解

        【defaults】  默认配置项

        【privilege_escalation】 执行名令的用户权限设置

        【paramiko_connection】 paramika 插件设置

        【ssh_connection】  ssh链接设置

        【accelerate】 

        【selinux&colors】

【defaults】:

ask_pass  可以控制,ansible剧本playbool是否会自动默认弹出密码

ask_sudo_pass  用户使用的系统平台开启了sudo 密码的话 应该开绿这一参数

gather_subset  设置收集的内容:all 、network、hardware、virtual  facter  ohai

remote_port  remote_tmp  remote_user:客户机的设置  分别对登录的用户和端口 及临时目录

sudo_exe  sudo_flags sudo_usesudo命令相关的设置 :路径 、参数、能够使用sudo的user

action_plugins  callback_plugins connection_plugins  vars_plugins   开发插件: 激活事件 回调 廉洁 过滤器 加载路径  任何地方加载 (默认配置即可)

forks :最大开辟的进程数,这个数不易过大 ,不易过小,一般设置的方法是 cpu核数*2

module_name :这个是/usr/bin/ansible的默认模块名 (-m) 默认是 command模块  但是 command不支持shell变量 管道配额  可使用shell模块

vault_password_file :存放远程客户机密码的文件  

pattern  :如果没有提供hosts节点 只是playbool要通信的默认主机组,默认是对所有主机统计 最好设置这个选项

inventory: 存放可通信主机的目录 

library:默认模块搜索路径

添加一台机器

1、编辑/etc/ansible/hosts 。将被控制的机器ip添加到这,我这里建立了个web组,可以直接添加

2、添加本机器public ssh key到目标机器的authorized_key

[root@controller ansible-2.8.4]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xvKFxAcZQzt1bIkdD87DIyd4IOwilLGlfXfEzq1ir1A root@controller
The key's randomart image is:
+---[RSA 2048]----+
|  .o....*+o++o   |
|  o=  .o.Oo==o   |
| .o ... Bo*oB .  |
|  . ...+ *o+.o   |
|   . .. E ..     |
|       =o..      |
|      ...o       |
|       .  .      |
|        ..       |
+----[SHA256]-----+

添加认证

[root@controller ansible-2.8.4]# ssh-copy-id root@172.30.3.160
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.30.3.160 (172.30.3.160)' can't be established.
ECDSA key fingerprint is SHA256:XdAO63JMvf69BAJvq1xLwwH7dU8TgmhVg16pSQeMbz0.
ECDSA key fingerprint is MD5:8b:fd:d0:67:d6:e5:1a:ad:51:2b:ec:2b:38:1c:56:c9.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.30.3.160's password: 
Permission denied, please try again.
root@172.30.3.160's password: 
Permission denied, please try again.
root@172.30.3.160's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.30.3.160'"
and check to make sure that only the key(s) you wanted were added.
#验证
[root@controller ansible-2.8.4]# ssh root@172.30.3.160
Last failed login: Sun Sep 29 12:56:58 CST 2019 from 172.30.3.159 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Sun Sep 29 08:08:46 2019 from 172.30.3.6

4、运行 ansible IP/组 -m ping

[root@localhost ~]# ansible 172.30.3.160 -m ping
172.30.3.160 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

ansible命令


1、ansible命令主题——ansible/ansible-playbook

2、被操作的目标机器的正则表达式——all

3、指定要使用的模块—— -m ping

4、传入参数  例如:ansible all -a ‘ls’ 

-a 指定传入模块的参数

-C -D 一起使用,检查hosts规则文件的修改

-l 限制配置规则的主机数

--list-hosts 显示所有匹配规则的主机数

-m -M 指定所使用的模块和模块的路径

--syntax-check 检查语句

-v 详细日志

-f 10   每次10个并行子进程

Inventory分组  /etc/ansible/hosts

【web】上面使用的这个就是分组 ansible web -m ping  就只对这个组里的ip起作用

端口直接在ip后面:端口 

【起始:结束】指定域名或ip  例如:   http:[1-10].ansible.com

参数:

ansible_ssh_host

ansible_ssh_port

ansible_ssh_user

ansible_ssh_pass  ssh 密码

ansible_sudo_pass sudo 密码  获取sudo权限

ansible_sudo_exe 执行sudo执行的命令

ansible_connection 与主机连接的类型 local  ssh  paramiko

ansible_ssh_private_key_file 私钥文件

ansible_shell_type 目标机器的shell类型

ansible_python_interpreter 

Patterns 相当于使用正则表达来决定哪台机器连通

指定一组连续机器  ansible 172.30.3.* -m ping

指定多台   ansible www.baidu.com:www.googl.com -m ping

文件管理

创建文件夹:ansible web -m file -a "dest=/path/to/cmode=755 owner=mdehaan group=medhaan state=directory"

拷贝文件 :ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

软件包管理

确定软件包安装:ansible web -m yum -a "name=acme state=present"

确定一个软件包的安装版本:ansible web -m yum -a "name=acem-1.5 state=present"

git模块

service系统服务模块

setup系统环境模块

原文地址:https://www.cnblogs.com/aloneysir/p/11594202.html