ansible 基础keys的ssh协议配置的

特性:幂等性:一个任务执行1遍和执行n遍效果一样。

ansible是个管理软件不是服务,不需要长期运行

 一、通过epel源安装ansible,

1、下载阿里云base源和epel源

1 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
2 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

二、主机清单(将需要管理的机器放到这个文件里)(在最后一行添加即可)

/etc/ansible/hosts

三、以组的方式管理主机

[webserver]
172.16.1.100
172.16.1.101

#webserver是组名,下面是要管理的机器的IP地址

[webserver]
172.16.1.100
172.16.1.101 [appserver]
172.16.1.10[1:2]

#按组分,172.16.1.10[1:2] 表示 172.16.1.101和172.16.1.102

四、比如想了解hostname相关的模块信息

ansible-doc hostname

-s 以简单方式查看的选项

[10:35:56 root@ansible ~]$ansible-doc -s hostname
- name: Manage hostname
hostname:
name: # (required) Name of the host
use: # Which strategy to use to update the hostname. If not set
we try to autodetect, but
this can be problematic,
specially with containers
as they can present
misleading information.

五、ansible单条命令执行

1、列出当前管理的主机清单

ansible all --list-host
hosts (3):
172.16.1.101
172.16.1.102
172.16.1.100

2、列出webserver下的主机

ansible webserver --list-hosts
hosts (2):
172.16.1.100

六、

1、首次远程控制会问你yes or no 去掉配置文件中的host_key_checking = False注释即可

vim /etc/ansible/ansible.cfg 

host_key_checking = False

2、在配置中开启日志

log_path = /var/log/ansible.log

七、两种连接方式

1、手工输入密码 ansible 172.16.1.101 -m ping -k  ,-k输入对方主机的root密码即可

ansible 172.16.1.101 -m ping -k

172.16.1.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}

2、基于key验证(不需要每次都输入密码)

1)、ssh-keygen

ssh-keygen 

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:d01XYNc5GBGsLKkTACOsJC5kGKb/QD/gigfJ0J5zZMA root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|+=.o. .+B.=|
|=*E... + +o|
|X + o . o . . o|
|=B * . o o o . |
|+.B + S o . . |
|.o = . o . . |
|o . . . |
| . |
| |
+----[SHA256]-----+

2、拷贝刚刚生成的key到需要远程的主机(只需要ansible主机到其他机器,不需要其他机器到ansible主机)(输入密码即可)

[16:42:43 root@ansible ~]$ssh-copy-id 172.16.1.100
/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
root@172.16.1.100's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '172.16.1.100'"
and check to make sure that only the key(s) you wanted were added.

八、ansible 的使用

ansible "webser:&appser" -m ping -C
#逻辑与webser组和appser组中都有的主机,执行ping模块 ansible 'webser:!appser' -m ping -C
#逻辑非,在webser组合但不在appser组中

 九、ansible常用模块使用步骤(模块名-m,模块参数-a)

1、command 默认模块(可以不加command)

ansible-doc -s command

1)、在所有主机上执行ls /data

ansible all -m command -a "ls /data"

2)、creates判断文件是否存在,如果存在就不执行后面的ls /data

ansible all -a "creates=/etc/fstab ls /data"

#command模块不支持 $VARNAME < > | ;&这些

2、shell 模块  和command相似,用shell命令执行

1)、在所有主机上创建用户

ansible all -m shell -a 'useradd alex'

2)对所有主机,重置alex用户密码为China123

ansible all -m shell -a 'echo China123 |passwd --stdin alex'

3)、对webser这个组里的所有主机显示$hostname

ansible webser -m shell -a 'echo $HOSTNAME'

4)、将默认模块由command修改为shell模块(shell模块可以替代command)

module_name = command
修改为
module_name = shell

5)、通过ansible批量修改所有主机的selinux为disabled(当然他有专门的模块做,这里只是演式)

ansible all -a "sed -i 's@SELINUX=enforcing@SELINUX=Disabled@' /etc/selinux/config"

3、script 模块在远程节点上运行本地脚本(最好绝对路径)

ansible all -m script -a /root/test1.sh 

4、copy 模块、从当前主机拷贝文件到远程主机

1)、将本机的/etc/fstab 拷贝到/data/下

ansible all -m copy -a 'src=/etc/fstab dest=/data/'

# src 源目录(支持相对路径和绝对路径),dest 目的目录  (仅支持绝对路径)再次执行相同的文件拷贝命令,源文件有变化内容属性等变化才会重新拷贝一遍,文件一模一样的将不拷贝

2)、将本机的/etc/fstab 拷贝到所有主机下的/data/fstab 下将所有者改为alex,所有组改为bin,文件权限改为777

ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin'

3)、避免文件存在呗覆盖,加backup,备份

ansible all -m copy -a 'src=/etc/fstab dest=/data/fstab mode=777 owner=alex group=bin backup=yes'

4)、content 指定内容,职级生成目标文件

ansible all -m copy -a 'content="line1\nline2\nline3\n" dest=/data/test.txt'

5、fetch 从远程主机提取文件至ansible主机(不支持目录)(会在/data/messages下生成远程主机IP+/var/log/messages的文件)(用来抓取远程主机的配置、日志等)(执行多个文件的抓取也不会覆盖文件IP文件夹)

ansible all -m fetch -a 'src=/var/log/messages dest=/data/messages/'

6、file文件模块(都是直接操作目标主机的文件)

1)、path指定要管理的文件,设置所有者所有组权限。

ansible all -m file -a 'path=/data/fstab owner=alex mode=777'

2)、创建软链接(hard硬链接,link软链接)

ansible all -m file -a 'src=/data/fstab path=/data/fstab.link state=link'

3)、创建空文件state=touch

ansible all -m file -a 'path=/data/f1.txt state=touch'

4)、删除文件 state=absent

ansible all -m file -a 'path=/data/fstab.link state=absent'

5)、清空目录,/data/是挂载点事删不掉的(最好不用,以防删根)

ansible all -m file -a 'path=/data/ state=absent'

7、unarchive:解包解压缩,

1)、在ansible这台主机上的压缩包在本地解压然后拷贝到远程主机上去(默认copy=yes 可以不写)

ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ owner=alex group=bin mode=777'

#将本地的/data/sysconfig.tar.gz 拷贝到所有主机的/data/下,所有者改为alex,所有组该为bin 权限改为777

2)、将远程主机上的压缩包,解压缩到指定目录上去

ansible all -m copy -a 'src=/data/sysconfig.tar.gz dest=/data/ '

ansible all -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ copy=no'

ansible 的安装及常见模块使用的更多相关文章

  1. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  2. Ansible常见模块介绍

    本节内容: ansible命令基础 常见模块举例 一.ansible命令基础 语法: ansible <host-pattern> [-f forks] [-m module_name] ...

  3. ansible入门二(Ansible常见模块介绍)

    本节内容: ansible命令基础 常见模块举例 一.ansible命令基础 语法: ansible <host-pattern> [-f forks] [-m module_name] ...

  4. Ansible 常见模块介绍

    目录 Ansible 常见模块介绍 ping 模块 command 模块 cron 模块 user 模块 group 模块 copy 模块 file 模块 service 模块 shell 模块 sc ...

  5. ANSIBLE安装和常用模块模块使用详细教程

    目录 ANSIBLE安装和各种模块应用功能 安装配置ANSIBLE ANSIBLE使用 ansible-galaxy工具 ansible-pull工具 ansible-playbook ansible ...

  6. ansible的安装及命令相关模块

    ansible 第一步:下载epel源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...

  7. Ansible安装及常用模块

    配置文件:/etc/ansible/ansible.cfg 主机列表:/etc/ansible/hosts  安装anslibe  wget -O /etc/yum.repos.d/epel.repo ...

  8. ansible环境部署及常用模块总结 - 运维笔记

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  9. ansible网络模块安装httplib2

    ansible网络模块安装httplib2 在进行使用ansible的网络模块的时候,需要安装httplib2模块 下载地址: https://pypi.python.org/pypi?%3Aacti ...

随机推荐

  1. 【ASP.NET Core】配置应用程序地址的N多种方法

    下面又到了老周误人子弟的时间,今天要误大伙的话题是:找找有多少种方法可以设置 ASP.NET Core 应用的地址,即 URL. 精彩马上开始! 1.UseUrls 方法 这是一个扩展方法,参数是可变 ...

  2. CabloyJS v4.0.0支持工作流引擎及更多 🎉

    截至2020年12月21日冬至,花了近5年时间作出最小可用NodeJS开源全栈框架,这就是CabloyJS V4.0.0 5年,90个模块,30万行代码,5400次提交(Commits),开启Node ...

  3. 一个全新的Vue拖拽特性实现:“移动”部分

    关于拖拽 CabloyJS提供了完备的拖拽特性,可以实现移动和调整尺寸两大类功能,这里对移动的开发进行阐述 关于调整尺寸的开发,请参见:拖拽:调整尺寸 演示 开发步骤 下面以模块test-party为 ...

  4. 使用Group By子句的时候,一定要记住下面的一些规则

    使用Group By子句的时候,一定要记住下面的一些规则:(1)不能Group By非标量基元类型的列,如不能Group By text,image或bit类型的列(2)Select指定的每一列都应该 ...

  5. 这个Spring Security登录插件牛啊,验证码、小程序、OAuth2都能快速接入

    上次我们把验证码登录.小程序登录优雅地集成到了Spring Security,很多同学大呼过瘾,相比较一些传统玩法高级了很多.胖哥就赶紧抓住机会举一反三,把几个非标准的OAuth2也接入了进来,主要是 ...

  6. DevStream 成为 CNCF Sandbox 项目啦!- 锣鼓喧天、鞭炮齐鸣、红旗招展、忘词了。

    开局两张图,内容全靠"编" 来,有图有真相! DevStream ️ CNCF DevStream joins CNCF Sandbox CNCF Cloud Native Int ...

  7. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  8. SAP APO-PP / DS

    在SAP APO中,使用生产计划/详细计划(Production Planning/Detailed Scheduling)生成满足生产要求的采购建议. 此组件还用于定义资源计划和订单明细. 您还可以 ...

  9. 自定义监控lvs

    1. 修改zabbix_agent配置文件添加以下内容,重启agent Include=/etc/zabbix/zabbix_agentd.d/ 2. 在zabbix安装目录下的scripts目录下添 ...

  10. 论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》

    论文信息 论文标题:Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering论文作者:Chaki ...