首页 > 其他分享 >【ansible】ansible的介绍和安装

【ansible】ansible的介绍和安装

时间:2024-03-13 18:59:24浏览次数:32  
标签:test1 介绍 python 安装 192.168 ansible ssh root

前言运维自动化

云计算核心职能

  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

标签:test1,介绍,python,安装,192.168,ansible,ssh,root
From: https://blog.csdn.net/Mo_nor/article/details/136632688

相关文章

  • 2024年HP笔记本安装Win7虚拟机的过程
    2024年HP笔记本安装Win7虚拟机的过程背景一个项目安装说明指定要求是Win7x64外加firefox+flash的组合。因为Win7已经EOL两年多了flash也EOL快四年了。所以没办法想通过WorkStation进行一下部署和创建。结构发现坑不少。所以记录一下。关于WorkStation的版本部分Wor......
  • Python collections详细介绍及实例
    Python的collections库提供了许多有用的数据结构,这些数据结构是对内建数据类型的补充。以下是一些collections库中主要数据结构的详细介绍和示例:CounterCounter是一个字典子类,用于计数可哈希对象。常用于统计字符出现的次数、列表中元素出现的次数等。示例:fromcollections......
  • MySQL安装最全最简教程
    MySQL的下载与安装、基本使用、系统服务制作MySQL简介MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。安装与下载1、下载流程访问官方(www.mysql.com)点击‘DOWNLOADS’,进入下载界面......
  • linux(centos7)通过ckman安装clickhouse并设置自启动
    软件所需安装包:链接:https://pan.baidu.com/s/1MvvS-UoZgn-c0H8pPAavEg?pwd=li9f提取码:li9f--来自百度网盘超级会员V5的分享安装ckman1.使用rpm的方式安装:将rpm包放到服务器,执行命令rpm-ivhckman-2.2.3.x86_64.rpm2.启动:systemctlstartckman3.默认来说ckman是配置了......
  • QT TCP通信介绍
    QT是一个跨平台的C++应用程序开发框架,它提供了一套完整的工具和库,用于开发各种类型的应用程序,包括图形用户界面(GUI)应用程序、命令行工具、网络应用程序等。QT提供了丰富的功能和类来简化网络通信的开发,其中包括TCP通信。TCP通信是一种可靠的、面向连接的通信协议,它在网络中......
  • salt-minion离线安装
    1.在nexus做salt官方的yum源代理http://xxxx/repository/gw-yum-proxys代理https://repo.saltproject.io/salt/py3/2.在yum仓库里上传repo文件及salt.sh安装文件latest.repo 文件[salt-repo]name=SaltrepoforRHEL/CentOS7PY3baseurl=http://xxxx/repository/gw-yum-......
  • win11安装wsl2没有网络解决方法
    1、启用hyper-v2、打开hyper-v管理器-点击虚拟交换机管理器-先看下有无一个名为WSL(这个名字可以被修改,和下面对应就行)的虚拟交换机,有的话先设置为外部网络3、编辑%USERPROFILE%.wslconfig添加如下内容[wsl2]networkingMode=bridgedvmSwitch=WSLipv6=true12344、执行wsl-......
  • python(pip)包/模块:如何离线安装?
    1、生成requirements.txt文件如果有同环境服务器,可直接生成requirements.txt,会把当前服务器下的包和版本写入文件中。pipfreeze>requirements.txt如安装指定包,创建requirements.txt,输入包名==版本号//只输入包名,默认最新版本。例:xlwt==1.3.02、下载包在requirements.t......
  • Maven的基本安装与使用
    前言Maven翻译为"专家"、"内行",是Apache下的一个纯Java开发的开源项目。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。Maven是一个项目管理工具,可以对Java项目进行构建、依赖管理。简单来说,Maven是一个框架,可以简化......
  • 关于自动唤醒(AWU)的使用介绍
    1、关于AWU的介绍AWU模块可以实现无需外部中断的情况下自动唤醒。通过对时间基数进行编程,可周期性地从停止或待机模式下唤醒。AWU是一个6位的自加型计数器,关于其时钟源的选择如下:CH32X035可选择内部高速时钟HSI的47KHz分频时钟作为AWU模块时钟源,可以在低功耗模式下运行......