【0725】自动化运维——ansible

24.15 ansible介绍

  • 不需要安装客户端,通过 sshd 去通信

  • 基于模块工作,模块可以由任何语言开发

  • 不仅支持命令行使用模块,也支持编写 yaml 格式的 playbook,易于编写和阅读

  • 安装十分简单,centos 上可直接 yum 安装

  • 有提供UI(浏览器图形化)www.ansible.com/tower,收费的

  • 官方文档 http://docs.ansible.com/ansible/latest/index.html

  • ansible 已经被 redhat 公司收购,它在 github 上是一个非常受欢迎的开源软件,github 地址https://github.com/ansible/ansible

  • 一本不错的入门电子书https://ansible-book.gitbooks.io/ansible-first-book/


24.16 ansible安装

1、准备两台机器,arslinux-01,arslinux-02

2、在 arslinux-01 上安装 ansible

[root@arslinux-01 ~]# yum list|grep ansible
[root@arslinux-01 ~]# yum install -y ansible ansible-doc

3、在 arslinux-01 上生成密钥对

[root@arslinux-01 ~]# ssh-keygen -t rsa

如果 /root/.ssh/ 下有 id_rsa.pub 则不需要生成密钥对

4、将公钥放到 arslinux-01,arslinux-02 上的 /root/.ssh/authorized_keys 中

5、验证连接

[root@arslinux-01 ~]# ssh 192.168.194.132
Last login: Sun Aug  4 21:08:02 2019 from 192.168.194.1

6、配置主机组

[root@arslinux-01 ~]# vim /etc/ansible/hosts
[testhost]
127.0.0.1
192.168.194.132

说明: testhost 为主机组名字,自定义的。 下面两个 ip 为组内的机器 ip


24.17 ansible 远程执行命令

  • ansible testhost -m command -a '命令'          批量远程命令

这里的 testhost 为主机组名,-m 后边是模块名字,-a 后面是命令。当然我们也可以直接写一个 ip,针对某一台机器来执行命令

[root@arslinux-01 ~]# ansible testhost -m command -a 'w'
127.0.0.1 | CHANGED | rc=0 >>
21:38:20 up  1:11,  3 users,  load average: 0.25, 0.14, 0.15
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    117月19 24days  0.05s  0.05s -bash
root     pts/1    192.168.194.1    21:07    4.00s  2.63s  0.00s ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/6220ae23ea -tt arslinux-02 /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1564925899.21-98869728293746/AnsiballZ_command.py && sleep 0'
root     pts/4    localhost        21:38    0.00s  0.25s  0.01s w
arslinux-02 | CHANGED | rc=0 >>

21:38:21 up  3:17,  3 users,  load average: 0.08, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    146月19 50days  0.04s  0.04s -bash
root     pts/1    192.168.194.1    21:08   53.00s  0.04s  0.04s -bash
root     pts/2    arslinux-01      21:38    1.00s  0.32s  0.01s w
[root@arslinux-01 ~]# ansible 192.168.194.132 -m command -a 'w'
arslinux-02 | CHANGED | rc=0 >>
21:38:52 up  3:18,  3 users,  load average: 0.05, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    146月19 50days  0.04s  0.04s -bash
root     pts/1    192.168.194.1    21:08    1:24   0.04s  0.04s -bash
root     pts/2    arslinux-01      21:38    1.00s  0.43s  0.01s w

错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

解决: yum install -y libselinux-python

  • ansible testhost -m shell -a '命令'          shell 模块同样可以实现远程执行的命令

[root@arslinux-01 ~]# ansible testhost -m shell -a 'hostname'
arslinux-02 | CHANGED | rc=0 >>
arslinux-02

127.0.0.1 | CHANGED | rc=0 >>
arslinux-01


24.18 ansible拷贝文件或目录

  • ansible arslinux-02 -m copy -a 'src= dest= owner= group= mode= '          拷贝目录或文件

[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/ansible dest=/tmp/ansible_test owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
    "changed": true,
    "dest": "/tmp/ansible_test/",
    "src": "/etc/ansible"
}
[root@arslinux-02 ~]# ll -d /tmp/ansible_test/
drwxr-xr-x 3 root root 21 8月   4 22:02 /tmp/ansible_test/
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:03:09 CST

注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。

如果拷贝的是文件,dest 指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果 desc 是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面

  • 针对文件操作

[root@arslinux-01 ~]# ansible 192.168.194.132 -m copy -a "src=/etc/passwd dest=/tmp/123 owner=root group=root mode=0755"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "66cfbbd6ccbbfb5edb8b3d364df81d2d9ce9e619",
    "dest": "/tmp/123",
    "gid": 0,
    "group": "root",
    "md5sum": "d5a72a116f1f47476e3156915f62972e",
    "mode": "0755",
    "owner": "root",
    "size": 1776,
    "src": "/root/.ansible/tmp/ansible-tmp-1564927633.07-72798416414339/source",
    "state": "file",
    "uid": 0
}
[root@arslinux-02 ~]# ll /tmp/123
-rwxr-xr-x 1 root root 1776 8月   4 22:07 /tmp/123
[root@arslinux-02 ~]# tail -3 /tmp/123
pure-ftp:x:1020:1020::/home/pure-ftp:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:997:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件


24.19 ansible远程执行脚本

1、创建一个脚本

[root@arslinux-01 ~]# vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt

2、分发脚本

[root@arslinux-01 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "6da17d4e84617796e1b3c7bfdd083d93",
    "mode": "0755",
    "owner": "root",
    "size": 49,
    "src": "/root/.ansible/tmp/ansible-tmp-1564928697.25-67620899139563/source",
    "state": "file",
    "uid": 0
}
127.0.0.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b70386033f7568a51de8209c2065dcbd917ca4b1",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/test.sh",
    "size": 49,
    "state": "file",
    "uid": 0
}

3、执行脚本

[root@arslinux-01 ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.194.132 | CHANGED | rc=0 >>


127.0.0.1 | CHANGED | rc=0 >>
[root@arslinux-02 ~]# ll /tmp/
总用量 8
-rwxr-xr-x 1 root  root  1776 8月   4 22:07 123
drwxr-xr-x 3 root  root    21 8月   4 22:02 ansible_test
-rwxr-xr-x 1 root  root    49 8月   4 22:24 test.sh
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 22:26:22 CST

脚本需要 755 权限,如果不是 755 权限,执行不了

4、shell 模块,还支持远程执行命令并且带管道,而 command 不支持

[root@arslinux-01 ~]# ansible testhost -m command -a "cat /etc/passwd |wc -l"
192.168.194.132 | FAILED | rc=1 >>
cat:无效选项 -- l
Try 'cat --help' for more information.non-zero return code

127.0.0.1 | FAILED | rc=1 >>
cat:无效选项 -- l
Try 'cat --help' for more information.non-zero return code

[root@arslinux-01 ~]# ansible testhost -m shell -a "cat /etc/passwd |wc -l"
192.168.194.132 | CHANGED | rc=0 >>
25

127.0.0.1 | CHANGED | rc=0 >>
37

ansible 需要先将脚本写好并分发到各机器上,然后在批量执行脚本

saltstack 则可以批量远程执行脚本,不需要分发


24.20 ansible管理任务计划

  • ansible 组名/ip/机器名 -m cron -a "name=' ' job=' ' weekday= "          远程管理任务计划

[root@arslinux-01 ~]# ansible 192.168.194.132 -m cron -a "name='test cron' job='/bin/touch /tmp/1234546.txt' weekday=6"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test cron"
    ]
}
[root@arslinux-02 ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/1234546.txt

说明:cron 是模块;name 自定义 crontab任务的名称;job 指的是任务;weekday 指每周几

其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month

  • ansible 组名/ip/机器名 -m cron -a "name=' ' state=absent"          删除 cron

[root@arslinux-01 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.194.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
[root@arslinux-02 ~]# crontab -l
[root@arslinux-02 ~]#


24.21 ansible安装包和管理服务

  • ansible 组名/ip/机器名 -m yum -a "name=包名"          远程 yum 安装包

  • ansible 组名/ip/机器名 -m yum -a "name=包名             state=removed" 远程卸载

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd"
[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=removed"

重新安装、启动并设置开机启动

[root@arslinux-01 ~]# ansible 192.168.194.132 -m yum -a "name=httpd state=remove state=installed"
  • ansible 组名/ip/机器名 -m service -a "name=  state=  enabled= "         启动服务并设开机启动

[root@arslinux-01 ~]# ansible 192.168.194.132 -m service -a "name=httpd state=started enabled=no"
[root@arslinux-02 ~]# ps aux|grep httpd
root      11746  0.3  0.5 224052  5000 ?        Ss   23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11747  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11749  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11750  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11751  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
apache    11752  0.0  0.2 224052  2948 ?        S    23:07   0:00 /usr/sbin/httpd -DFOREGROUND
root      11769  0.0  0.0 112724   988 pts/1    R+   23:07   0:00 grep --color=auto httpd
[root@arslinux-02 ~]# date
2019年 08月 04日 星期日 23:07:43 CST

说明:name 是服务名称;state 是操作、状态;enabled 指是否开机启动

  • Ansible文档的使用

ansible-doc -l   列出所有的模块

ansible-doc cron   查看指定模块的文档


未完待续



原文地址:https://blog.51cto.com/11530642/2426538

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


----name:setpublickeyonremotehosts&setreomtehostssudoersfilehosts:all#此脚本要求所有的主机root用户密码相同become:noremote_user:rootvars:-remoteuser:user1:rhcetasks:-name:setsecondarygroupforrhce
环境准备#cat/etcedhat-releaseCentOSLinuxrelease7.9.2009(Core)#pythonPython3.7.11(default,Jul312022,16:12:35)[GCC4.8.520150623(RedHat4.8.5-44)]onlinuxType"help","copyright","credits"or"li
准备好环境,在安装之前请先了解openshift提供的ansible有大量的安装选项文档地址:https://docs.okd.io/latest/install/configuring_inventory_file.html1、配置/etc/ansible/hosts 
Ansible:运维工作:系统安装(物理机、虚拟机)-->程序包安装、配置、服务启动-->批量操作-->程序发布-->监控OSProvisioning:物理机:PXE、Cobbler虚拟机:ImageTemplatesConfigration:puppet(ruby)saltstack(python)chefcfengineCommandand
ansible与salt对比相同都是为了同时在多台机器上执行相同的命令都是python开发不同agent(saltstack需要安装、ansible不需要)配置(salt配置麻烦,ansible基本不用配置)学习路线(salt比较陡峭,ansible比较平缓)第三方工具(salt比较少)开源社区的对接(salt比较少)现有用户(salt还是an
[root@node1playbook]#catnginx.yml-hosts:test\\主机组,要和nginx.yml在同一个目录下remote_user:root\\远端执行任务的用户tasks:\\任务-name:installhttpd\\任务描述command:yum-yinstallhttpd\\调用ansible的command模块安装httpd-na
一直不知道这个模块到底在哪,并且也挺想搞清楚官方那些模块到底在哪个目录下。1.使用grep-rl"copy.py"/usr/lib/python2.7/site-packages/ansible(这个目录是专门放ansible源码目录的)然后找到是/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py这个文件,这里需要
ansile作为去除安装agent的自动化工具,通过ssh协议的简单功能强大的自动化工作。在ansile使用的过程中,有三种用途1、ansible自行一次性任务,即执行命令如:ansible10.59.87.11-mping*ansible"dev-hdp"-mcopy-a"src=oot/HfHadoopHiveUdf.jardest=/data1/opt/cloudera/par
ansible-playbook(1) Ansible组成部分InventoryModulesAdHocCommandsplaybooksplaybooks:Tasks:任务,即调用的模块完成的某操作variables:变量Templates:模版Roles:角色 基本结构:-host:webserverremote_user:tasks:
报错:[root@jenkins~]#ansiblego_activity-mcron-a"name='log_clear'minute=0hour=2job=find/home/golanger/log/-typef-name'log$(date+\%d-d-1day)'-delete"ERROR!thistask'cron'hasextraparams,wh
一、测试环境说明1、系统:rhel6.92、ip地址:20.20.20.24/2420.20.20.41/2420.20.20.42/243、以下操作使用root身份进行,也可以使用具有sudo权限的用户进行相关操作二、环境准备1、关闭iptables防火墙、selinux#/etc/init.d/iptablesstop#
ansible常用模块安装:依赖于epel源yuminstallansible-y配置文件:/etc/ansible/ansible.cfgInvertoory:/etc/ansible/hosts 如何查看模块帮助:ansible-doc-lansible-doc-sMODULE_NAME` ansible命令应用基础:语法:ansible<host-pattern>[options]-fforks
copycopy模块是将ansible管理主机上的文件拷贝上远程主机中,与fetch相反,如果目标路径不存在,则自动创建,如果src的目录带“/”则复制该目录下的所有东西,如果src的目录不带“/”则连同该目录一起复制到目标路径;常用模块src参数:用于指定需要copy的文件或目录
9.YAML9.1简介(1)YAML是一个可读性高的用来表达资料序列的格式。(2)YAML参考了其它多种语言。包括:XML、C语言、python、perl以及电子邮件格式的RFC2822等。ClarkEvans在2001年首次发表了这种语言。(3)YAML不是XML,在开发这种语言时,YAML的意思其实是:yetanothermarkuplanguage,9.2特性(1)YA
了解ansibleansible批量在远程主机上执行命令ansible主要是为了进行操作多个主机而进行的#!/bin/envpython文件中直接指向python文件#!/bin/base 指行脚本一.ansible第一步.下载epel源wget-O/etc/yum.repos.d/epel.repohttp://mirrors.aliyun.comepo/epel-7.repo
背景:在私有云环境下,遇到要开通某个项目,则需要快速的响应创建虚拟机,并且做一些基础的配置。为了提高效率以及减少手工犯错的概率,一般会采取ansible批量部署,但是使用ansible的前提是预先配置好免密。在密码一致的场景中,可以使用expect优化做免密的过程解决方案:1.
简单例子1:vars定义变量-hosts:allremote_user:rootvars:-package:nginx-service:nginx tasks:-name:installnginxpackage yum:name={{package}}state=latest-name:installconfigurationfileforhttpd copy:src=/etcginxginx
 ansible自动化运维工具的介绍      ansible结构特性:            模块化,调用特定的模块,完成特定的任务;        基于Python语言实现,由Paramiko、PyYAML和Jinja2三个关键模块;        部署简单,agentless        主从模
---恢复内容开始---Templates:模版 cat/etc/ansible/hosts  cattemplatesginx.conf.j2 -hosts:testremote_user:rootvars:-package:httpd-service:httpdtasks:-name:installnginxpackage yum:name={{package}}state=la
1、配置资源清单inventory文件[root@test1~]#cat>/etc/ansible/hosts<<EOF[k8s]192.168.0.92ansible_ssh_port=22ansible_ssh_user=k8sansible_ssh_pass='123'ansible_become_pass='123456'EOF解释:ansible_ssh_pass='123'