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. VBA驱动SAP GUI实现办公自动化(一)

    小爬之前写过一系列Python驱动SAP GUI实现办公自动化的文章,其实如果我们的实际业务不是太复杂,且我们对VBA语法比较熟悉的话,我们完全可以借助Excel VBA来驱动SAP GUI做很多自动 ...

  2. 3D大场景展示功能你了解多少?见详解!

    裸眼3D技术的出现打破了真实与虚拟的界限,人们不仅希望能够体验奇妙的虚拟场景,也希望足不出户在短短几分钟内就能看到遍布各地的场景,希望能实时对接关键数据. 裸眼3D技术的出现打破了真实与虚拟的界限,人 ...

  3. VueX的模块你知道多少?

    为什么会出现VueX的模块呢?当你的项目中代码变多的时候,很难区分维护.那么这时候Vuex的模块功能就这么体现出来了. 那么我们就开始吧!一.模块是啥? /* eslint-disable no-un ...

  4. 集成学习——XGBoost(手推公式)

  5. 关键字——this,super,static,final

    this 理解为当前对象. //测试 public static void main(String[] args){ Person person = new Person(3, "xiaoM ...

  6. SAP 实例 5 CFW Events

    REPORT demo_custom_control . * Declarations ***************************************************** CL ...

  7. 一个紧张刺激的聊天器,要不要进来看看(Python UDP网络模型)

    先来哔哔两句:(https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 互联网的本质是什么?其实就是信息的交换.那么如何将自己的信息发送到其他人的电脑上呢?那就需要借助 ...

  8. NC23046 华华教月月做数学

    NC23046 华华教月月做数学 题目 题目描述 找到了心仪的小姐姐月月后,华华很高兴的和她聊着天.然而月月的作业很多,不能继续陪华华聊天了.华华为了尽快和月月继续聊天,就提出帮她做一部分作业. 月月 ...

  9. Tapdata PDK 生态共建计划启动!Doris、OceanBase、PolarDB、SequoiaDB 等十余家厂商首批加入

      2022年4月7日,Tapdata 正式启动 PDK 插件生态共建计划,致力于全面连接数据孤岛,加速构建更加开放的数据生态,以期让各行各业的使用者都能释放数据的价值,随时获取新鲜的数据.截至目前, ...

  10. 扩展-PageHelper分页插件

    1.PageHelper 分页插件简介 1) PageHelper是MyBatis中非常方便的第三方分页插件 2) 官方文档: https://github.com/pagehelper/Mybati ...