Ansible - 01

ansible

一、ansible 概述
ansible是一个自动化统一配置管理工具,自动化主要体现在ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。

官网:https://www.ansible.com/

官方文档:https://docs.ansible.com/

源代码仓库:https://github.com/ansible/ansible

# 自动化运维应用场景
#  企业实际应用场景分析
Dev开发环境
Test测试环境
Pre预发布环境
Master生成环境

#  自动化运维应用场景
文件传输
应用部署
配置管理
任务流编排

2.自动化工具
1. puppet 学习难,安装ruby环境难,没有远程执行功能

2. ansible 轻量级,大规模环境下只通过ssh会很慢,串行的,也会有小量的并行。

#   ansible 安装方式 yum install -y ansible

3. saltstack 一般选择salt会使用C/S结构的模式,salt-master和salt-minion,并行的,大规模批量操作的情况下,会比ansible速度快一些,底层使用的是zero-MQ消息队列

3. saltstack安装
1.` 下载saltstack源
yum install -y https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
2.` 服务端下载
yum install salt-master -y
2.` 服务端下载
yum install salt-minion -y

'ansible与saltstack的差别'
# ansible  方便,只需要配置一个主机清单就可以一键执行         ---> 灰度发布
# saltstack  超过200台机器,如果用ansible就比较慢了,因为ansible是串行的,是一台一台执行的,所以用saltstack。
3.自动化运维相较于手动运维
1.提高工作效率
2.提高了工作的准确度
3.减少人员成本
4.减少了重复的工作

# 珍贵的例子
'例如公司要你用源码安装100台nginx,如果一台一台安装,就太麻烦了。如果用了ansible,就可以用一条命令,让让100台服务器执行源码安装'
# cmdp 资源管理平台

ansible:python,Agentless,中小型应用环境
Saltstack:python,一般需部署agent(客户端),执行效率更高
Puppet:ruby, 功能强大,配置复杂,重型,适合大型环境
Fabric:python,agentless
Chef:ruby,国内应用少
4.ansible的功能及优点
1.远程执行
批量执行远程命令,可以对多台主机进行远程操作

2.配置管理
批量配置软件服务,可以进行自动化方式配置,服务的统一配置管理,和启停

3.事件驱动
通过ansible的模块,对服务进行不同的事件驱动
比如:
    1、修改配置后重启
    2、只修改配置文件,不重启
    3、修改配置文件后,重新加载
    4、远程启停服务管理

4.管理公有云
通过API接口的方式管理公有云,不过这方面做的不如saltstack.
saltstack本身可以通过saltcloud管理各大云厂商的云平台。

5.二次开发
因为语法是Python,所以便于运维进行二次开发。

6.任务编排
可以通过playbook的方式来统一管理服务,并且可以使用一条命令,实现一套架构的部署

7.跨平台,跨系统
几乎不受到平台和系统的限制,比如安装apache和启动服务
# 珍贵的例子
    在Ubuntu上安装apache服务名字叫apache2
    在CentOS上安装apache服务名字叫httpd

    在CentOS6上启动服务器使用命令:/etc/init.d/nginx start
    在CentOS7上启动服务器使用命令:systemctl start nginx
# 可以有两种方式
# ansible有多种模块,例如安装的模块,启动服务的模块。根据ansible的安装模块,各个服务器识别自己的系统,使用各自的语法来安装下载软件。也可以根据ansible的启动模块,使不同版本的CentOS来启动nginx服务。

#  同类型软件对比
1.puppet 学习难,安装ruby环境难,没有远程执行功能
2.ansible 轻量级,大规模环境下只通过ssh会很慢,串行的
3.saltstack 一般选择salt会使用C/S结构的模式,salt-master和salt-minion,并行的,大规模批量操作的情况下,会比ansible速度快一些,底层使用的是zero-MQ消协队列

# ansible特性
1. 模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块
2. Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
3. 基于Python语言实现
4. 部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
5. 安全,基于OpenSSH
6. 幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性非绝对
7. 支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
8. 较强大的多层解决方案 roles

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zJGumuRz-1622473132701)(C:\Users\17155\Desktop\下载图片\1618420583992.png)]

二、ansible 构成
1.组成部分
1.连接插件connection plugins用于连接主机 用来连接被管理端
2.核心模块core modules连接主机实现操作, 它依赖于具体的模块来做具体的事情
3.自定义模块custom modules根据自己的需求编写具体的模块
4.插件plugins完成模块功能的补充
5.剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6.主机清单inventor定义ansible需要操作主机的范围

最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ih64QSDo-1622473132703)(C:\Users\17155\Desktop\下载图片\1618448270268.png)]

2.ansible执行流程
1.ansible读取playbook剧本,剧本中会记录对哪些主机执行哪些任务。         # 例如让 web 安装nginx
2.首先ansible通过主机清单找到要执行的主机,然后调用具体的模块。	      # web 是谁
3.其次ansible会通过连接插件连接对应的主机并推送对应的任务列表。	      # 使用yum模块安装nginx
4.最后被管理的主机会将ansible发送过来的任务解析为本地Shell命令执行。	  # 受控端执行yum install -y nginx
三、ansible使用
1.环境准备
主机外网IP内网IP身份
m01192.168.15.161172.16.1.161控制端
web01192.168.15.107172.16.1.107受控端
web03192.168.15.109172.16.1.109受控端
2.控制端安装Ansible
#1.安装epel源
[root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#2.安装ansible
[root@m01 ~]# yum install -y ansible

# 3.编译安装ansble
[root@localhost ~]# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
[root@localhost ~]# wget https://releases.ansible.com/ansible/ansible-2.9.18.tar.gz
[root@localhost ~]# tar xf ansible-2.9.18.tar.gz
[root@localhost ~]# cd ansible-2.9.18
[root@localhost ~]# python setup.py build
[root@localhost ~]# python setup.py install
[root@localhost ~]# mkdir /etc/ansible
[root@localhost ~]# cp -r examples/* /etc/ansible
Copy to clipboardErrorCopied
# 4.PIP安装
[root@localhost ~]# yum install python-pip
[root@localhost ~]# pip install --upgrade pip
[root@localhost ~]# pip install ansible --upgra
# 5.校验
[root@localhost ~]# ansible --version
ansible 2.9.18
  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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

3.Ansible参数
# ansible <host-pattern> [options]
#不常用参数
--version   #ansible版本信息
-v          #显示详细信息  
-i          #自己指定主机清单文件路径,默认是在/etc/ansible/hosts,乱配干嘛。。。
-k          #提示输入ssh密码,而不使用基于ssh的密钥认证   # 就是不想输入密码。
-C          #模拟执行测试,但不会真的执行         #没什么卵用。只能识别语法错误,不识别逻辑错误
-T          #执行命令的超时,默认10s
-u, --user=REMOTE_USER   #执行远程执行的用户
-b, --become             #代替旧版的sudo 切换
--become-user=USERNAME   #指定sudo的runas用户,默认为root
-K, --ask-become-pass   #提示输入sudo时的口令
#常用参数
-m          #使用的模块名称,默认使用command模块
-a          #使用的模块参数,模块的具体动作
--syntax-check		#验证语法

[root@m01 ~]# ansible --version  ##查看版本信息
ansible 2.9.15
  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, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
4.Ansible配置文件
[root@m01 ~]# vim /etc/ansible/ansible.cfg
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ansible_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[root@m01 ~]# rpm -ql ansible
[root@m01 ~]# zcat /usr/share/man/man1/ansible-config.1.gz

#要查看完整列表,请访问https://docs.ansibe.com/或使用ansibe-config命令。
For a full list check \fI\%https://docs.ansible.com/\fP\&. or use the \fIansible\-config\fP command.

#/etc/ansible/ansible.cfg 配置文件,如果存在则使用
/etc/ansible/ansible.cfg \-\- Config file, used if present

#~/.ansible.cfg 用户配置文件,覆盖默认配置(如果存在)
~/.ansible.cfg \-\- User config file, overrides the default config if present

#\&/ansible.cfg 本地配置文件(在当前工作目录中)假定为(aqproject-specific)(aq,如果存在,则重写其余文件)。
\&./ansible.cfg \-\- Local config file (in current working directory) assumed to be \(aqproject specific\(aq and overrides the rest if present.

# 生效优先级
ansible_CONFIG >> $ansible_CONFIG/ansible.cfg >> ~/.ansible.cfg >>/etc/ansible/ansible.cfg
 # 1. $ansible_CONFIG
 # 2. ./ansible.cfg 
 # 3. ~/.ansible.cfg 
 # 4. etc/ansible/ansible.cfg(配置文件)
#如上所述,ansible_CONFIG环境变量将覆盖所有其他环境变量。
As mentioned above, the ansible_CONFIG environment variable will override all others.

5.Ansible配置文件解释
# ansible配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录

[root@m01 ~]# cat /etc/ansible/ansible.cfg 
#inventory      = /etc/ansible/hosts      #主机列表配置文件
#library        = /usr/share/my_modules/  #库文件存放目录
#remote_tmp     = ~/.ansible/tmp          #临时py文件存放在远程主机目录
#local_tmp      = ~/.ansible/tmp          #本机的临时执行目录
#forks          = 5                       #默认并发数
#sudo_user      = root                    #默认sudo用户
#ask_sudo_pass = True                     #每次执行是否询问sudo的ssh密码
#ask_pass      = True                     #每次执行是否询问ssh密码
#remote_port    = 22                      #远程主机端口  #如果远程主机的ssh端口改了其他的,才修改
host_key_checking = False                 #跳过检查主机指纹
log_path = /var/log/ansible.log           #ansible日志,这个开不开无所谓
# 以上两项必须配置
#普通用户提权操作
[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False 

四、主机清单配置
1.基于密码的方式
1.1、单主机配置
#方式一: ip + 端口 + 用户名 + 用户密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01] 
192.168.15.107 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='123'
[web03]
192.168.15.109 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='123'

## 方式一简洁版
[root@m01 ~]# vim /etc/ansible/hosts 
[web1] 
192.168.15.107  ansible_ssh_pass='123'
[web3]
192.168.15.109  ansible_ssh_pass='123'

#测试单台机器
[root@m01 ~]# ansible 'web01' -m ping
192.168.15.107 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#测试多台机器
[root@m01 ~]# ansible '*' -m ping
192.168.15.107 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.15.109 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
# 方式二 m01本地hosts配置
[root@m01 ~]# cat/etc/hosts

  192.168.15.107 web01
 
  192.168.15.109 web03
#方式三: ip + 用户密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
192.168.15.107 ansible_ssh_pass='123'
[web03]
192.168.15.109 ansible_ssh_pass='123'

[root@m01 ~]# ansible web03 -m ping
192.168.15.109 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#方式四: 密钥配置
 1.` 生成密钥对
[root@m01 ~]# ssh-keygen

2.` 推送公钥,不用在主机清单,对应的主机IP,配密码,
[root@m01 ~]# ssh-copy-id 192.168.15.107
[root@m01 ~]# ssh-copy-id 192.168.15.109 
3.` 配置hosts
[root@m01 ~]# vim /etc/hosts
[web1]
web01
[web3]
web03

# 主机组配置
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
192.168.15.107 ansible_ssh_pass='123'
192.168.15.109 ansible_ssh_pass='123'
# 查看组下面的机器
[root@m01 ~]# ansible 'web_group' --list-host
 hosts (2):
    192.168.15.107
    192.168.15.109
# 操作多个组
[root@m01 ~]# ansible 'nfs_group,web_group' -m ping
# 或者操作了多个组
[root@m01 ~]# ansible 'nfs_group:web_group' -m ping  # 冒号
 # 注意: 主机名和组名称不要一致,否则执行命令时不知道找主机还是找主机组
 
 #本机配置
 [test]
  192.168.15.61 ansible_connection=local   #指定本地连接,无需ssh配置
 # 如果发送known_hosts的key报错
 # 在 /etc/ssh/ssh.config 添加 
 StrictHostKeyChecking no # (相当于输入yes)
 # 重启sshd systemctl restart sshd
 
 # 别名连接
 linux ansible_ssh_host=172.16.1.107
 # 连接的就是172.16.1.107这台机器
 
 # 指定本地机器
 web01 ansible_connection=local

1.2多主机配置
# 有些类似7层负载均衡,给web端的server起一个组名,
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
192.168.15.107 ansible_ssh_pass='123'
192.168.15.109 ansible_ssh_pass='123'

# 珍贵的例子 ,用ping去测试以下,查看[web_group]组下的所有成员。
[root@m01 ~]# ansible web_group -m ping
web01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web03 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
    
    
# 指定host
-i : 临时指定host路径

[root@m01 ~]# ansible lbserver -m ping -i ./host 

172.16.1.6 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
# 公共变量
[db_group]
db01
[db:vars]
ansible_ssh_pass='123'

2.基于秘钥的方式
1、生成密钥对
1.` 生成密钥对
[root@m01 ~]# ssh-keygen

2.` 推送公钥,不用在主机清单,对应的主机IP,配密码,
[root@m01 ~]# ssh-copy-id 172.16.1.107
[root@m01 ~]# ssh-copy-id 172.16.1.109

3.` 配置主机清单
[root@m01 ~]# vim /etc/ansible/hosts 
# 珍贵的说明
'这时候,执行ansible的命令,可以对于'`[web_group]`'的成员,都可以成功,但是,针对'`[web_group]`'下的某一个成员执行命令,会比较麻烦。虽然可以成功,但是容易记错。所以可以在 管理端的/etc/hosts文件,配上成员的名字+对应的IP。但是注意,配置的组名不要和管理端的 /etc/hosts 文件名字重名 ,不然ansible识别不了,会冲突。
[web_group]
192.168.15.107
192.168.15.109

4.` 配置hosts
[root@m01 ~]# vim /etc/hosts
192.168.15.107 web01
192.168.15.109 web03

5.` 修改hosts配置主机清单
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01    
web02
 # 不需要加中括号
6.` #测试
[root@m01 ~]# ansible 'web_group' -m ping
[root@m01 ~]# ansible web01 -m ping

3.定义整合组
1、定义整合组
`整合组:
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01
web02
[nfs_group]
nfs ansible_ssh_pass='123'
#定义整合组,下面包含多个组
[www:children]
web_group
nfs_group
#配置hosts
[root@m01 ~]# vim /etc/hosts
192.168.15.107 web01
192.168.15.109 web03

172.168.1.131 nfs

2、查看整合组下面的主机组
[root@m01 ~]# ansible www --list-host
[root@m01 ~]# ansible '*' --list-host
  hosts (3):
    web01
    web03
    nfs
   
# 免密脚本
[root@instance-gvpb80ao ~]# vim push_ssh_key.sh 
#!/bin/ 
#********************************************************************#
# WelCome To Login                                                    #
# Author:chenyang                                                    #
# WX:Alvins0918                                                      #
# Date:2021-05-20                                                    #
# Description:老男孩IT教育Linux12学院                                    #
# Copyright©:2021 All rightsreserved                                 #
#********************************************************************#
IPLIST="
172.16.1.7
172.16.1.8
172.16.1.9"

rpm -q sshpass &> /dev/null || yum -y install sshpass  
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa  -P ''
export SSHPASS=123
for IP in $IPLIST;do
   sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done

4.测试
`测试某个的组单个主机
[root@m01 ~]# ansible 'web01' -m ping

`测试所有主机
[root@m01 ~]# ansible '*' -m ping

`测试所有主机
[root@m01 ~]# ansible 'all' -m ping

`测试主机组
[root@m01 ~]# ansible 'web_group' -m ping

`测试组合组
[root@m01 ~]# ansible 'www' -m ping

`逻辑与`
[root@m01 ~]# cat/etc/ansible/hosts
web_group
[web01]
[web02]
[web03]
[lb]
lb01
lb02
# 在公网组里面或者在私网组里面
[root@m01 ~]# ansible 'web_group:&web02' -m ping
`逻辑非`
[root@m01 ~]# ansible 'web_group:!web02' -m ping
`正则` #web开头的机器
[root@m01 ~]# ansible '~web' -m ping

5.ansible补充
# ansible相关工具
#     命令	              解释
/usr/bin/ansible	        主程序,临时命令执行工具
/usr/bin/ansible-doc	    查看配置文档,模块功能查看工具,相当于man
/usr/bin/ansible-playbook	定制自动化任务,编排剧本工具,相当于脚本/usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault	     文件加密工具
/usr/bin/ansible-console	 基于Console界面与用户交互的执行工具
/usr/bin/ansible-galaxy	     下载/上传优秀代码或Roles模块的官网平台

# 利用ansible实现管理的主要方式:

Ad-Hoc即利用ansible命令,主要用于临时命令使用场景
ansible-playbook主要用于长期规划好的,大型项目的场景,需要有前期的规划过程

ansible列表
#以shanhe用户执行ping存活检测
# 前提创建用户并在cat /etc/sudoers 授权 给shanhe权限
[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k
SSH password:   
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#以wang sudo至root执行ping存活检测
[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k -b
SSH password: 
106.13.81.75 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Invalid/incorrect password: Permission denied, please try again.", 
    "unreachable": true
}
172.16.0.4 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Invalid/incorrect password: Permission denied, please try again.", 
    "unreachable": true
}
#以shanhe sudo至chenyang用户执行ping存活检测
[root@instance-gvpb80ao tmp]# ansible all -m ping -u shanhe -k -b --become-user=chenyang
SSH password: 
172.16.0.4 | FAILED! => {
    "msg": "Missing sudo password"
}
106.13.81.75 | FAILED! => {
    "msg": "Missing sudo password"
}
#以shanhe sudo至root用户执行ls 
[root@instance-gvpb80ao ~]# ansible all -m command -u shanhe -a 'ls /root' -b --become-user=root -k -K
SSH password:   #root密码 
BECOME password[defaults to SSH password]:   #shange密码
172.16.0.4 | CHANGED | rc=0 >>
hosts.list
openvpn-user-crt.sh
push_ssh_key.sh
106.13.81.75 | CHANGED | rc=0 >>
hosts.list
openvpn-user-crt.sh
push_ssh_key.sh

-k, --ask-pass     #提示输入ssh连接密码,默认Key验证
-u, --user=REMOTE_USER #执行远程执行的用户
-b, --become    #代替旧版的sudo 切换
--become-user=USERNAME  #指定sudo的runas用户,默认为root
-K, --ask-become-pass  #提示输入sudo时的口令

#  ansible-vault 加密
# 剧本
[root@openvpn ~]# cat hello.yaml 
- hosts: web01
  remote_user: root
  tasks:
    - name: echo 111
      shell: 'echo 111 > /root/a.tt'
此工具可以用于加密解密yml文件。
# 帮助文档 ansible-vault -h
格式

ansible-vault [create|decrypt|edit|encrypt|rekey|view]
Copy to clipboardErrorCopied
范例

ansible-vault encrypt hello.yml #加密

[root@instance-gvpb80ao ~]# ansible-vault encrypt hello.yaml 
New Vault password: 
Confirm New Vault password: 
Encryption successful
[root@instance-gvpb80ao ~]# cat hello.yaml 
$ANSIBLE_VAULT;1.1;AES256
62373435386162613961376134643237366461653734626664353737363432323761366263336461
36323537633837316664306439303534343536373162333537646462303864323233303436643839
336130356436613230633231623035633536

ansible-vault decrypt hello.yml #解密

[root@instance-gvpb80ao ~]# ansible-vault decrypt hello.yaml 
Vault password: 
Decryption successful
[root@instance-gvpb80ao ~]# cat hello.yaml 
- hosts: public
  remote_user: root
  gather_facts: no
  tasks:
    - name: hello world
      command: echo "Hello Chenyang" > /root/123456

ansible-vault view hello.yml #查看

[root@instance-gvpb80ao ~]# ansible-vault view hello.yaml
Vault password: 
- hosts: public
  remote_user: root
  gather_facts: no
  tasks:
    - name: hello world
      command: echo "Hello Chenyang" > /root/123456
      
ansible-vault edit hello.yml #编辑加密文件

[root@instance-gvpb80ao ~]# ansible-vault edit hello.yaml
Vault password: 
[root@instance-gvpb80ao ~]# cat hello.yaml 
$ANSIBLE_VAULT;1.1;AES256
32623539636538376665373330626334643538306161613462393334656633613030663634396334
66666333316466313161333061383930376562366631643962386462353535363535333461353035
62376463663630383465666662616139636631626334373564346131363531636439386431623666
3933393266353638346232366666313137653962356130373739

ansible-vault rekey hello.yml #修改口令

[root@instance-gvpb80ao ~]# ansible-vault rekey hello.yaml
Vault password: 
New Vault password: 
Confirm New Vault password: 
Rekey successful

ansible-vault create new.yml #创建新文件

[root@instance-gvpb80ao ~]# ansible-vault create abc.yaml
New Vault password: 
Confirm New Vault password: 
[root@instance-gvpb80ao ~]# cat abc.yaml 
$ANSIBLE_VAULT;1.1;AES256
32653230653437313566623734666166346632613366373732346533396130396132323433653238
6139333135386637303939306437306637303837663361650a653466663938313032613330653636
66346639613937333939393036336334396234616139633066386330343831333439643039353436
6134313033616235390a353362643562633763303063313264633435306635643965356636303964
6430

# ansible-console
此工具可交互执行命令,支持tab,ansible 2.0+新增

提示符格式

执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$
Copy to clipboardErrorCopied
常用子命令

设置并发数: forks n 例如: forks 10
切换组: cd 主机组 例如: cd web
列出当前组主机列表: list
列出所有的内置命令: ?或help
范例:

[root@instance-gvpb80ao ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (2)[f:5]$ ping
172.16.0.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
106.13.81.75 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
root@all (2)[f:5]$ list
106.13.81.75
172.16.0.4


#  ansible-galaxy
此工具会连接 https://galaxy.ansible.com 下载相应的roles
# 查看列表
[root@instance-gvpb80ao ~]# ansible-galaxy list
# /root/.ansible/roles
- geerlingguy.nginx, 3.0.0
# /usr/share/ansible/roles
# /etc/ansible/roles

# 下载nginx roles
[root@instance-gvpb80ao ~]# ansible-galaxy install geerlingguy.nginx
- downloading role 'nginx', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-nginx/archive/3.0.0.tar.gz
- extracting geerlingguy.nginx to /root/.ansible/roles/geerlingguy.nginx
- geerlingguy.nginx (3.0.0) was installed successfully

# 删除
[root@instance-gvpb80ao ~]# ansible-galaxy remove geerlingguy.nginx
- successfully removed geerlingguy.nginx

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL-09-19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值