【ansible】ansible的介绍和安装

前言运维自动化

云计算核心职能

  1. 搭建平台架构

  2. 日常运营保障

  3. 性能效率优化

相关工具

  • 代码管理(SCM):GitHub、GitLab、BitBucket、SubVersion

  • 构建工具:maven、Ant、Gradle

  • 自动部署:Capistrano、CodeDeploy

  • 持续集成(CI):Jenkins、Travis

  • 配置管理:Ansible、SaltStack、Chef、Puppet

  • 容器:Docker、Podman、LXC、第三方厂商如AWS

  • 编排:Kubernetes、Core、Apache Mesos

  • 服务注册与发现:Zookeeper、etcd、Consul

  • 脚本语言:python、ruby、shell、go

  • 日志管理:ELK、Logentries

  • 系统监控:Prometheus、Zabbix、Datadog、Graphite、Ganglia、Nagios

  • 性能监控:AppDynamics、New Relic、Splunk

  • 压力测试:JMeter、Blaze Meter、loader.io

  • 应用服务器:Tomcat、JBoss、IIS

  • Web服务器:Apache、Nginx

  • 数据库:MySQL、Oracle、PostgreSQL等关系型数据库;mongoDB、redis等NoSQL数据库

  • 项目管理(PM):Jira、Asana、Taiga、Trello、Basecamp、Pivotal Tracker

1. Ansible 发展史及功能

作者:Michael DeHaan( Cobbler pxe kikstar 与 Func 作者)ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具,使用它可以在相距数光年的距离,远程实时控制前线的舰队战斗2012-03-09,发布0.0.1版,2015-10-17,Red Hat宣布1.5亿美元收购。

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

1.1Ansible 功能

批量执行远程命令,可以对远程的多台主机同时进行命令的执行

批量安装和配置软件服务,可以对远程的多台主机进行自动化的方式配置和管理各种服务

编排高级的企业级复杂的IT架构任务, Ansible的Playbook和role可以轻松实现大型的IT复杂架构

提供自动化运维工具的开发API, 有很多运维工具,如jumpserver(堡垒机)就是基于 ansible 实现自动化管理功能

1.2 Ansible 特性

- 模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块(账号,软件等)
- Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
- 基于Python语言实现
- 部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
- 安全,基于OpenSSH
- 幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性非绝对
- 支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构   剧本演员要按照    系统按照你规定的方式去执行命令
- 较强大的多层解决方案 role

1.3 Ansible 架构

1.3.1 Ansible组成
- INVENTORY:Ansible管理主机的清单 /etc/anaible/hosts   需要管理的服务清单     
- MODULES:Ansible执行命令的功能模块,多数为内置核心模块,也可自定义
- PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
- API:供第三方程序调用的应用程序编程接口
1.3.2 Ansible 命令执行来源
- USER 普通用户,即SYSTEM ADMINISTRATOR

- PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,通常是JSON格式的YML文件

- CMDB(配置管理数据库) API 调用

- PUBLIC/PRIVATE CLOUD API调用

2.Ansible 安装和入门

2.1安装

开启SELinux来限制进程的权限,防止恶意程序通过提权等方式对系统进行攻击

########yum源安装###############
[root@node1 yum.repos.d]#vim CentOS-Base.repo 
#加入 epel源
[epel]
name=gn
baseurl=https://mirrors.aliyun.com/epel/$releasever/x86_64
        https://mirrors.cloud.tencent.com/epel/$releasever/x86_64
        https://mirrors.huaweicloud.com/epel/$releasever/x86_64
        https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64
gpgcheck=0


[root@node1 yum.repos.d]#yum info  ansible
已加载插件:fastestmirror, langpacks
base                                                                                | 3.6 kB  00:00:00     
epeel                                                                               | 4.7 kB  00:00:00     
extras                                                                              | 2.9 kB  00:00:00     
updates                                                                             | 2.9 kB  00:00:00     
(1/3): epeel/7/group_gz                                                             |  96 kB  00:00:00     
(2/3): epeel/7/updateinfo                                                           | 1.0 MB  00:00:00     
(3/3): epeel/7/primary_db                                                           | 7.0 MB  00:00:04     
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epeel: mirrors.aliyun.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
可安装的软件包
名称    :ansible
架构    :noarch
版本    :2.9.25
发布    :1.el7
大小    :17 M
源    :epeel/7
简介    : SSH-based configuration management, deployment, and task execution system
网址    :http://ansible.com
协议    : GPLv3+
描述    : Ansible is a radically simple model-driven configuration management,
         : multi-node deployment, and remote task execution system. Ansible works
         : over SSH and does not require any software or daemons to be installed
         : on remote nodes. Extension modules can be written in any language and
         : are transferred to managed machines automatically.


[root@node1 yum.repos.d]#yum install ansible -y
......................省略..................................
已安装:
  ansible.noarch 0:2.9.25-1.el7                                                                            

作为依赖被安装:
  PyYAML.x86_64 0:3.10-11.el7                          libyaml.x86_64 0:0.1.4-11.el7_0                    
  python-babel.noarch 0:0.9.6-8.el7                    python-jinja2.noarch 0:2.7.2-4.el7                 
  python-markupsafe.x86_64 0:0.11-10.el7               python-paramiko.noarch 0:2.1.1-9.el7               
  python2-httplib2.noarch 0:0.18.1-3.el7               python2-jmespath.noarch 0:0.9.4-2.el7              

完毕!



yum install epel-release.noarch  -y
yum install ansible -y

###############Git方式#######################
yum install git
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
(操作)安装ansible
[root@test1 ~]# systemctl stop firewalld
[root@test1 ~]# systemctl disable firewalld
[root@test1 ~]# setenforce 0
[root@test1 ~]# yum install -y epel-release

Installed:
  epel-release.noarch 0:7-11                                                         

Complete!
[root@test1 ~]# yum install ansible -y

Dependency Installed:
  PyYAML.x86_64 0:3.10-11.el7               libyaml.x86_64 0:0.1.4-11.el7_0         
  python-babel.noarch 0:0.9.6-8.el7         python-jinja2.noarch 0:2.7.2-4.el7      
  python-markupsafe.x86_64 0:0.11-10.el7    python-paramiko.noarch 0:2.1.1-9.el7    
  python2-httplib2.noarch 0:0.18.1-3.el7    python2-jmespath.noarch 0:0.9.4-2.el7   
  sshpass.x86_64 0:1.06-2.el7              

Complete!
[root@test1 ~]# ansible --version
ansible 2.9.27
  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, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
[root@test1 ~]# 
[root@test1 ~]# yum info  ansible
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.nju.edu.cn
Installed Packages
Name        : ansible
Arch        : noarch
Version     : 2.9.27
Release     : 1.el7
Size        : 103 M
Repo        : installed
From repo   : epel
Summary     : SSH-based configuration management, deployment, and task execution
            : system
URL         : http://ansible.com
License     : GPLv3+
Description : Ansible is a radically simple model-driven configuration management,
            : multi-node deployment, and remote task execution system. Ansible works
            : over SSH and does not require any software or daemons to be installed
            : on remote nodes. Extension modules can be written in any language and
            : are transferred to managed machines automatically.

2.2查看基本信息

[root@test1 ~]# ansible --version
ansible 2.9.27
  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, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

2.3相关文件

- /etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性,也可以在项目的目录中创建此文件,当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文 件

- /etc/ansible/hosts 主机清单;存放下属客机
- /etc/ansible/roles/ 存放角色的目录

2.4 ansible 主配置文件

Ansible 的配置文件可以放在多个不同地方,优先级从高到低顺序如下

ANSIBLE_CONFIG #环境变量,注意此项用 ansible --version 看不到,但可以生效
./ansible.cfg   #当前目录下的ansible.cfg
~/.ansible.cfg #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg  #系统默认配置文件
​
可以使用 来查看
[root@node1 yum.repos.d]#ansible --version

Ansible 的默认配置文件 /etc/ansible/ansible.cfg ,其中大部分的配置内容无需进行修改

[defaults]
#inventory     = /etc/ansible/hosts     #主机列表配置文件
#library = /usr/share/my_modules/       #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp        #临时py命令文件存放在远程主机目录
#local_tmp     = $HOME/.ansible/tmp     #本机的临时命令执行目录
#forks         = 5                      #默认并发数
#sudo_user     = root                   #默认sudo 用户
#ask_sudo_pass = True                   #每次执行ansible命令是否询问ssh密码
#ask_pass     = True   
#remote_port   = 22
#host_key_checking = False              #检查对应服务器的host_key,建议取消此行注释,实现第一次连接自动信任目标主机
#log_path=/var/log/ansible.log          #日志文件,建议启用
#module_name = command          、mod        #默认模块,可以修改为shell模块
[privilege_escalation]                  #普通用户提权配置
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

(操作)实现免密登录
[root@test1 ~]# vim /etc/ansible/hosts 
 43 ## db-[99:101]-node.example.com
 44 
 45 [web]
    #可以指定ssh端口非默认的端口
 46 192.168.67.12:666
 47 192.168.67.13
 48
 49 [local]
    #指定本地连接,无需ssh配置
 50 192.168.67.11 ansible_connection=local


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

#使用密码连接;因为只能输入一次,所以需要被管理的主机密码一致
[root@test1 ~]# ansible web -m ping -k
SSH password: 
192.168.67.13 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.67.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}


#ansible 默认使用 ssh 连接
#所以管理前要免密登录;ask——>no  要在安全的内网环境下才用no
[root@test1 ~]# vim /etc/ssh/ssh_config 
 34 #   ConnectTimeout 0
 35     StrictHostKeyChecking no

[root@test1 ~]# vim /etc/ansible/ansible.cfg 
#开启71行的不验证
 70 # uncomment this to disable SSH key host checking
 71 host_key_checking = False

#添加免密登录的脚本文件
[root@test1 ~]# cd /opt/
[root@test1 opt]# ls
fdisk.sh  rh
[root@test1 opt]# rz -E
rz waiting to receive.
[root@test1 opt]# ls
fdisk.sh  rh  ssh_key.sh
#修改一下登录密码
[root@test1 opt]# vim ssh_key.sh 
  1 #!/bin/bash
  2 
  3 PASS=123

#bash跑一下
[root@test1 opt]# bash ssh_key.sh 
Generating public/private rsa key pair.
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:Y4JojyvznzV66aQotwDtcb8QLweNrATP/cbTqIUjn+k root@test1
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|.                |
| = + +           |
|. O O o S        |
|.+ * O = .       |
|. = * #..        |
|+..= ^o+         |
| *=+Eoo          |
+----[SHA256]-----+
sshpass-1.06-2.el7.x86_64
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -o 'StrictHostKeyChecking=no' '192.168.67.11'"
and check to make sure that only the key(s) you wanted were added.

known_hosts                                        100%  525     1.3MB/s   00:00    
known_hosts                                        100%  525   824.0KB/s   00:00    
known_hosts                                        100%  525     1.1MB/s   00:00    

#跑完后,即可实现免密登录
[root@test1 opt]# ansible web -m ping
192.168.67.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.67.13 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
(脚本)免密登录脚本文件的代码
#!/bin/bash

PASS=123
#设置网段最后的地址,4-255之间,越小扫描越快
END=254

IP=`ip a s ens33 | awk -F'[ /]+' 'NR==3{print $3}'`
NET=${IP%.*}.

rm -f /root/.ssh/id_rsa
[ -e ./SCANIP.log ] && rm -f SCANIP.log
for((i=3;i<="$END";i++));do
ping -c 1 -w 1  ${NET}$i &> /dev/null  && echo "${NET}$i" >> SCANIP.log &
done
wait

ssh-keygen -P "" -f /root/.ssh/id_rsa
rpm -q sshpass || yum -y install sshpass
sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no $IP 

AliveIP=(`cat SCANIP.log`)
for n in ${AliveIP[*]};do
sshpass -p $PASS scp -o StrictHostKeyChecking=no -r /root/.ssh root@${n}:
done

#把.ssh/known_hosts拷贝到所有主机,使它们第一次互相访问时不需要输入回车
for n in ${AliveIP[*]};do
scp /root/.ssh/known_hosts ${n}:.ssh/
done

2.5 inventory 主机清单文件

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory 主机清单文件中将其分组组织

官方文档:How to build your inventory — Ansible Documentation

默认的inventory file 位置在:/etc/ansible/hosts

参数详细说明
ansible_ssh_host 
#将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port 
#ssh端口号.如果不是默认的端口号,通过此变量设置.这种可以使用 ip:端口
192.168.1.100:2222

ansible_ssh_user 
#默认的 ssh 用户名

ansible_ssh_pass 
#ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)

ansible_sudo_pass 
#sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
#sudo 命令路径(适用于1.8及以上版本)

ansible_connection 
#与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.

ansible_ssh_private_key_file
#ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.

ansible_shell_type 
#目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为'csh' 或 'fish'.

ansible_python_interpreter 
#目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的Python.之所以不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).与ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

例子:

[web]                        
192.168.67.12:666
#可以指定ssh端口非默认的端口
192.168.67.13

[server]
192.168.67.[11:15]
#指定连续的主机

[ky15]
node[1:5]  
#指定连续的主机

[server:ky15]
server
ky15
#可以嵌套组

[local]
192.168.67.11 ansible_connection=local
#指定本地连接,无需ssh配置

#ansible_connection=ssh 需要StrictHostKeyChecking no
192.168.67.14  ansible_connection=ssh  ansible_ssh_port=2222  ansible_ssh_user=root ansible_ssh_password=123
10.0.0.6  ansible_connection=ssh  ansible_ssh_user=root ansible_ssh_password=123
还可以指定用户身份  端口号 和密码

#执行ansible命令时显示别名,如node1
[websrvs]
node2 ansible_ssh_host=192.168.67.12
node3 ansible_ssh_host=192.168.67.13

[websrvs]
ansible_ssh_password=123

例子

[root@node1 ansible]#vim hosts 
[local]
192.168.91.100 ansible_connection=local

[web]
192.168.91.101
192.168.91.102

[accp]
192.168.91.103
192.168.91.105


[webserver]
192.168.91.[101:103]
192.168.91.105

[web:children]
web
accp

[root@node1 ~]#ansible 192.168.91.105 -m ping
#可以指定   m代表使用模块  ping代表ping模块 命令探测下,需要输入 yes 或no

2.6Ansible相关工具

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

2.6.1ansible

执行临时任务一次性任务

用法:
ansible <host-pattern> [-m module_name] [-a args]
命令     主机或者清单中的组 -m 指定模块      -a  执行的任务

选项:
--version 					#显示版本
-m module   				#指定模块,默认为command;使用时可以不写
-v 							#详细过程 -vv -vvv更详细
--list-hosts 				#显示主机列表,可简写 --list
-C, --check   				#检查脚本文件(play book),并不执行;nginx-t 检查配置文件,是不一样的
-T, --timeout=TIMEOUT 		#执行命令的超时时间,默认10s
-k, --ask-pass     			#提示输入ssh连接密码;默认密钥Key验证,密码容易被破解
-u, --user=REMOTE_USER 		#执行远程执行的用户,默认root
-b, --become    			#代替旧版的sudo 切换
--become-user=USERNAME  	#指定sudo的runas用户,默认为root  vim /etc/sudoers 用户权限
-K, --ask-become-pass  		#提示输入sudo时的口令
-f FORKS, --forks FORKS 	#指定并发同时执行ansible任务的主机数
(操作)
[root@test1 opt]# ansible localhost -m ping
localhost | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}


########  提权操作   ######
[root@centos7 ~]#grep lisi /etc/sudoers
#先在被控制端(目标主机)sudo授权
zhangsan    ALL=(ALL) NOPASSWD: ALL

#以zhangsan的用户连接用户,并利用sudo代表mage执行whoami命令
[root@ansible ~]#ansible 192.168.91.101 -m shell -a 'whoami' -u zhangsan -k -b --become-user=lisi
SSH password: #输入远程主机lisi用户ssh连接密码
10.0.0.8 | CHANGED | rc=0 >>

[root@node1 ~]#ansible 192.168.91.105 -m shell -a 'hostname >/opt/host.txt' -u zhangsan -k -b
SSH password: 
192.168.91.105 | CHANGED | rc=0 >>


命令模块	    不行
shell模块	可以使用重定向、特殊命令

########################   主机列表   ###########################
##支持通配符
ansible all -m ping
ansible "*" -m ping 
ansible 192.168.67.* -m ping
#ansible "srvs" -m ping
ansible "192.168.67.12 192.168.67.13" -m ping

[root@test1 ~]# vim /etc/ansible/hosts 
 45 [web]
 46 192.168.67.12
 47 192.168.67.13
 48 
 49 [kgc]
 50 192.168.67.12
 51 192.168.67.14
 52 
 53 [local]
 54 192.168.67.11 ansible_connection=local
    
#或关系(双方和)
[root@test1 ~]# ansible 'web:kgc' --list-hosts
  hosts (3):
    192.168.67.12
    192.168.67.13
    192.168.67.14

#并且关系(双方都有)
[root@test1 ~]# ansible "web:&kgc" --list-hosts
  hosts (1):
    192.168.67.12

#逻辑非;(前者有后者无)在web里但不在kgc里的
[root@test1 ~]# ansible 'web:!kgc' --list-hosts
  hosts (1):
    192.168.67.13


#正则表达式
ansible "websrvs:dbsrvs" -m ping
ansible "~(k|a).*" -m ping
[root@node1 ~]#ansible "~(k|a).*" -m ping

################例子###
#除了本机外都重启
ansible 'kube*:etcd:!10.0.0.101' -a reboot && reboot



## -f 并行执行;-f1,一台一台执行;睡眠3秒
[root@test1 opt]# ansible all -a "sleep 3" -f1
## -f4 四台被管理机同时执行
[root@test1 opt]# ansible all -a "sleep 30" -f4

192.168.67.11 | CHANGED | rc=0 >>

192.168.67.12 | CHANGED | rc=0 >>

192.168.67.13 | CHANGED | rc=0 >>

192.168.67.14 | CHANGED | rc=0 >>

#睡眠时被管理机会在用户主目录生成一个临时文件,可以装一个tree来看,也可以直接cd过去
[root@test2 ~]# yum -y install tree
[root@test2 ~]# tree .ansible/
.ansible/
└── tmp
    └── ansible-tmp-1710239948.82-21869-160369154402830
        └── AnsiballZ_command.py
#一般ctrl+c强行停止睡眠,临时文件会被保留,多了会卡
##ansible命令执行过程
1. 加载自己的配置文件,默认/etc/ansible/ansible.cfg
#[root@test1 opt]# ansible all -a "sleep 3" -f4
#cd /root/.ansible/tmp
2. 加载自己对应的模块文件,如:command
3. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
4. 给文件+x执行
5. 执行并返回结果
6. 删除临时py文件,退出

$HOME 为当前用户的家目录
执行返回结果
  • 绿色:执行成功并且不需要做改变的操作

  • 黄色:执行成功并且对目标主机做变更

  • 红色:执行失败

配置文件中定义了颜色(可改,但一般不改)

vim /etc/ansible/ansible.cfg 
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
2.6.2 ansible-doc

#使用ansible-doc 模块名    可以查看该模块的用法和实例
[root@test1 opt]# ansible-doc ping
> PING    (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
....省略了

#加上-s可以简略查看
[root@test1 opt]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  # Data to return for the `ping' return value. If this
                               parameter is set to
                               `crash', the module
                               will cause an
                               exception.

#查看所有支持的模块
[root@test1 opt]# ansible-doc -l
q

#统计模块数量
[root@test1 opt]# ansible-doc -l | wc -l
3387

[root@test1 opt]# ansible-doc -l | grep ^ping
ping                                                          Try to connect to ...
pingdom                                                       Pause/unpause Ping...

[root@test1 opt]# ansible-doc file
q退出

#在vim中使用  :set ai  可以在回车时使光标下移到与当前行相同的位置

2.6.3 ansible-console
#此工具可交互执行命令,支持tab,ansible 2.0+新增
提示符格式
执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$

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

[root@test1 opt]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (4)[f:5]$ cd web
root@web (2)[f:5]$ forks 10
root@web (2)[f:10]$ list
192.168.67.12
192.168.67.13

  #还可以直接使用模块
root@web (2)[f:10]$ ping
192.168.67.13 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.67.12 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

root@web (2)[f:10]$ ?

Documented commands (type help <topic>):
========================================
EOF
...省略

root@web (2)[f:10]$ 
# ctrl+c 退出
[root@test1 opt]# 
#2.6.4 playbook
此工具用于执行编写好的 playbook 任务
范例

[root@test1 opt]# vim hello.yml
---
#hello world yml file
- hosts: web
 remote_user: root
 gather_facts: no
  
 tasks:
    - name: hello world
      command: /usr/bin/wall hello world

ansible-playbook hello.yml
cat hello.yml


#添加超级管理员张三,并设置成免密登录(慎用)
[root@test1 opt]# vim /etc/sudoers
 91 ## Allow root to run any commands anywhere 
 92 root    ALL=(ALL)       ALL
 93 zhangsan        ALL=(ALL)       NOPASSWD: ALL

ansible-doc user

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/460921.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

JVM-5

1.选择垃圾收集器 如果你的堆大小不是很大&#xff08;比如 100MB &#xff09;&#xff0c;选择串行收集器一般是效率最高的。 参数&#xff1a; -XX:UseSerialGC 。如果你的应用运行在单核的机器上&#xff0c;或者你的虚拟机核数只有单核&#xff0c;选择串行收集器依然是合…

P6技巧:ORACLE Primavera P6 反馈项目Reflections的使用

前言 反馈是一个有趣的概念&#xff0c;就目前的了解而言&#xff0c;他是 Primavera P6 所独有的。 你可以将反馈视为项目的特殊假设副本。 然而&#xff0c;与直接拷贝副本不同的是&#xff0c;反馈保留了返回源项目的链接。 这意味着如果反馈发生更改&#xff0c;你可以将部…

Javascript 线性搜索算法

线性搜索被定义为一种顺序搜索算法&#xff0c;从一端开始&#xff0c;遍历列表中的每个元素&#xff0c;直到找到所需的元素&#xff0c;否则搜索将继续&#xff0c;直到数据集的末尾。 线性搜索算法 线性搜索算法如何工作&#xff1f; 在线性搜索算法中&#xff1a; …

C++语法、Linux命令查询网站

文章目录 1.cplusplus2.cppreference3.Linux命令查询网站 1.cplusplus 网址&#xff1a;https://legacy.cplusplus.com/ 2.cppreference 1.cppreference中文网站&#xff1a;https://zh.cppreference.com/w/首页 2.cppreference英文原站&#xff1a;https://en.cppreference…

openssl3.2 - note - Decoders and Encoders with OpenSSL

文章目录 openssl3.2 - note - Decoders and Encoders with OpenSSL概述笔记编码器/解码器的调用链OSSL_STORE 编码器/解码器的名称和属性OSSL_FUNC_decoder_freectx_fnOSSL_FUNC_encoder_encode_fn官方文档END openssl3.2 - note - Decoders and Encoders with OpenSSL 概述 …

【STM32学习】PWM学习,(二)驱动LED呼吸灯

上文学习了PWM的基本概述&#xff0c;和PWM的各种参数&#xff0c;本文 学习使用PWM信号去驱动LED实现呼吸灯的效果。 1、PWM驱动LED呼吸灯 1.1介绍 目标&#xff1a;单片机输出一个PWM信号&#xff0c;驱动LED呼吸亮灭。PWM占空比高&#xff0c;则LED更亮&#xff1b;PWM占空…

第12章 指针

以下内容是学习尚硅谷 12.1 指针基本介绍 1&#xff09;指针是C语言的精华&#xff0c;也是C语言的难点 2&#xff09;指针&#xff0c;也就是内存的地址&#xff1b;所谓指针变量&#xff0c;也就是保存了内存地址的变量。关于指针的基本使用&#xff0c;在讲变量的时候做了…

【SCI论文】“学术丑闻揭露:当AI写作遭遇学术审稿,ChatGPT意外成为论文共作者!“

在最近的学术圈中出现了一篇令人哭笑不得的论文。这篇文章标题为“The three-dimensional porous mesh structure of Cu-base…”发表在《Surfaces and Interfaces》杂志上&#xff0c;竟然包含了ChatGPT的提示语&#xff0c;暴露出了审稿过程中可能的疏忽。 文章讨论了铜基金…

【四 (5)数据可视化之 Pyecharts常用图表及代码实现 】

目录 文章导航一、介绍[✨ 特性]二、安装Pyecharts三、主题风格四、占比类图表1、饼图2、环形图3、玫瑰图4、玫瑰图-多图5、堆叠条形图6、百分比堆叠条形图 五、比较排序类1、条形图2、雷达图3、词云图4、漏斗图 六、趋势类图表1、折线图2、堆叠折线图3、面积图4、堆叠面积图 七…

Linux:kubernetes(k8s)有状态的服务部署(14)

之前我都是对无状态进行的一个操作&#xff0c;我们想扩容就扩容&#xff0c;想缩容就缩容&#xff0c;根本不用去考虑他的一个网络环境&#xff0c;本地储存环境啥的一个状态 当我们做有状态的服务的操作&#xff0c;肯定要申请一个持久化的一个空间&#xff0c;以及网络&…

tcp/ip协议2实现的插图,数据结构8 (30 - 32章)

(201) 201 三十0 中断优先级补充 (202) 202 三十1 TCP的用户需求 函tcp_usrreq一 (203) 203 三十2 TCP的用户需求 函tcp_usrreq二 (204) 204 三十3 TCP的用户需求 函tcp_usrreq三 (205) 205 三十4 TCP的用户需求 函tcp_usrreq四 (206) 206 三十5 TCP的用户需求 函tcp_usrreq五 …

邮件协议(SMTP、POP3、IMAP4)

电子邮件系统 1、概述 &#xff08;1&#xff09;网络电子邮件系统&#xff0c;好处在于价格低廉&#xff0c;速度非常快 &#xff08;2&#xff09;形式多样化&#xff08;文字、图像、声音……&#xff09; 2、电子邮件系统组成部分 用户代理 MUA&#xff08;Mail User …

【调参】如何为神经网络选择最合适的学习率lr-LRFinder-for-Keras

【调参】如何为神经网络选择最合适的学习率lr-LRFinder-for-Keras_学习率选择-CSDN博客文章浏览阅读9.2k次&#xff0c;点赞6次&#xff0c;收藏55次。keras 版本的LRFinder&#xff0c;借鉴 fast.ai Deep Learning course。前言学习率lr在神经网络中是最难调的全局参数&#x…

java抽象类的作用及解析

在 Java 中&#xff0c;抽象类是一种特殊的类&#xff0c;它可以用于定义一些抽象的方法和属性&#xff0c;这些方法和属性可能在子类中有不同的实现。 抽象类的主要作用包括&#xff1a; 提供抽象方法&#xff1a;抽象类可以包含一些没有具体实现的抽象方法&#xff0c;这些…

德人合科技 | 公司办公终端、电脑文件资料 \ 数据透明加密防泄密管理软件系统

天锐绿盾是一款全面的企业级数据安全解决方案&#xff0c;它专注于为企业办公终端、电脑文件资料提供数据透明加密防泄密管理。 首页 德人合科技——www.drhchina.com 这款软件系统的主要功能特点包括&#xff1a; 1. **透明加密技术**&#xff1a; 天锐绿盾采用了透明加密技…

〔理论与代码分析〕Fast-SCNN:Fast Semantic Segmentation Network(语义分割、经典网络、速度、高效、实时)

论文地址&#xff1a;Fast-SCNN: Fast Semantic Segmentation Network论文提出时间&#xff1a;2019 年 2 月 12 日官方代码&#xff1a;作者并没有放出源代码&#xff0c;因此下面是一些第三方的实现 PaddleSeg 复现代码&#xff1a;百度飞桨团队复现的代码MMSegmentation 复现…

Spring Boot+Vue前后端分离项目如何部署到服务器

&#x1f31f; 前言 欢迎来到我的技术小宇宙&#xff01;&#x1f30c; 这里不仅是我记录技术点滴的后花园&#xff0c;也是我分享学习心得和项目经验的乐园。&#x1f4da; 无论你是技术小白还是资深大牛&#xff0c;这里总有一些内容能触动你的好奇心。&#x1f50d; &#x…

jeesite列表jqGrid表格底部汇总,基于onSelectRow和onSelectAll实现选中行汇总合计

一、最终效果图 二、表格启用复选框并初始化赋值 onSelectAll: function() { calc_sum(); }, onSelectRow: function() { calc_sum(); },// 加载成功后执行事件 ajaxSuccess: function(data){var dy 0;var glbzqmrsdtyg 0;var glbzqmrsschyg 0;var glbzqmrsqtcy 0;…

鸿蒙-自定义组件的生命周期

目录 自定义组件的生命周期 1.aboutToAppear 2.aboutToDisappear 3.onPageShow 4.onPageHide 5.onBackPress 日志输出 1.显示页面 2.页面点击返回按钮 3.页面跳转 4.页面返回 自定义组件的生命周期 先来一段列子 import router from ohos.router Entry Component…

IO Watch:用 Arduino UNO 制造的可编程手表

MAKER:mblaz/译:趣无尽 Cherry(转载请注明出处) 关于手表的项目,之前我们已经介绍过一款《Arduino + 3D 打印 DIY 电子手表》。本期的项目同样的一款基于 Arduino UNO 的可编程的手表,相比之下制造门槛更高一些。同时它更成熟、实用,外形也很有设计感,非常的漂亮! 这…
最新文章