这篇文章主要介绍了centos7如何安装与配置ansible,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
环境 centos 7 server : 192.168.8.23 host1 : 192.168.8.24 host2 : 192.168.8.25
安装
# https://www.cnblogs.com/soymilk2019/p/11173203.html
# 每个节点安装ansible
yum install epel-release -y
yum install ansible -y
# server 免密访问各host
ssh-keygen
ssh-copy-id root@192.168.8.24
ssh-copy-id root@192.168.8.25
# 添加ansible客户机组 /etc/ansible/hosts
[group1]
192.168.8.24
192.168.8.25
模块
# https://www.cnblogs.com/keerya/p/7987886.html
# 主机连通性测试
ansible group1 -m ping
# command模块(该模块不支持| 管道命令)
ansible group1 -m command -a 'ss -ntl'
# shell模块(在远程主机上调用shell解释器运行命令)
ansible group1 -m shell -a 'ss -ntl |grep LISTEN'
# file模块
ansible group1 -m file -a "file=/root/c.sh state=touch mode=644"
ansible group1 -m file -a "path=/root/c state=directory mode=644"
ansible group1 -m file -a "path=/root/c/c.txt state=touch mode=644"
ansible group1 -m file -a "path=/root/c state=absent force=yes"
# copy模块
# 修改文件内容
ansible group1 -m copy -a 'content="ls /" dest=/root/c.sh mode=755'
# 拷贝文件到host
ansible group1 -m copy -a "src=/root/c.sh dest=/root"
# 拷贝目录到host(覆盖之前把源文件备份,备份文件包含时间信息)
ansible group1 -m copy -a "src=/root/d dest=/root backup=yes"
# fetch模块(该模块用于从远程某主机获取(复制)文件到本地)
ansible group1 -m fetch -a 'src=/root/a.txt dest=/root'
# script模块(该模块用于将本机的脚本在被管理端的机器上运行)
# 在各host中执行server端/root/c.sh脚本
ansible group1 -m script -a '/root/c.sh'
# setup模块(该模块主要用于收集信息,是通过调用facts组件来实现的)
ansible group1 -m setup -a 'filter="*mem*"'
# 保存到server的/tmp/facts中
ansible group1 -m setup -a 'filter="*mem*"' --tree /tmp/facts
# yum模块
ansible group1 -m yum -a "name=htop state=present"
# cron 模块(该模块适用于管理cron计划任务的)
# user模块
# group模块
# service模块(该模块用于服务程序的管理)
playbook
# https://blog.csdn.net/vanvan_/article/details/99354186
Hosts: 运行指定任务的目标主机,也可以是主机组,支持正则表达式
Tasks: 任务列表,一个playbook配置文件只能有一个tasks,一个tasks下可以编排多个任务
Varniables: 变量
Templates: 模板,使用templates可以针对不同的主机定制不同参数
Handlers: 由特定条件触发的任务,监控资源改变时才会触发,需要配合notify使用
Roles: Playbook的按固定目录结构组成
# index.html
<h>Hello world.</h>
# a.yml
---
- hosts: group1
remote_user: root
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: index.html
dest: /var/www/html/index.html
- name: start httpd
service:
name: "{{ item }}"
state: started
loop:
- httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
$ ansible-playbook a.yml
感谢你能够认真阅读完这篇文章,希望小编分享的“centos7如何安装与配置ansible”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/redhands/blog/5037538