ansible基本操作

ansible模块

1、主机连通性测试

ansible qf -m   ping

2、command模块  ,默认就是command模块

3、shell模块

ansible qf  -m  shell  -a  'cat  /etc/passwd | grep "root"'

4.copy模块

====================

- hosts: qf

vars:

IP: "{{ ansible_ens33['ipv4']['address'] }}"

tasks:

- name: 将原有的hosts文件备份

shell: mv /etc/hosts /etc/hosts_bak

- name: 将ansible端的hosts复制到各自机器上

copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644

- name: 将新的hosts文件后面追加各自机器内网和hostname

lineinfile: dest=/etc/hosts line="{{ IP }} {{ ansible_hostname }} "

=======================

- name: copy {{ rpmname }}.conf

copy: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf  backup=yes

ansible qf -m copy  -a  'content="jfsakf\n" dest=/data/name mode=666'

5.file模块

ansible qf -m  file -a 'path=/data/app state=directory'

- name: 创建目录

file: path=/data/a state=directory

file: path=/data/a state=absent  //删除

6.fetch模块

ansible qf -m fetch -a 'src=/data/hello.txt dest=/data'  //只能是文件

7.cron计划任务模块

ansible qf -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'

ansible qf -m cron -a 'name="df everyday" hour=15 job="df -lh >> /tmp/disk_total &> /dev/null" state=absent'

8.yum模块

name=  #所安装的包的名称

state =  #present--->安装, latest--->安装最新的, absent---> 卸载软件。

- name: install httpd

yum: name=httpd state=present

9.service模块

ansible qf -m service -a 'name=nginx state=started enabled=true'

- name: start {{ rpmname }} service

service: name={{ rpmname }} state=started//stopped//restarted

10.user模块

ansible qf -m user -a 'name=keer uid=11111'

11.group模块

ansible qf -m group -a 'name=sanguo gid=12222'

12.script模块

vim  /tmp/df.sh

chmod  -v  +x  df.sh

ansible web -m script -a '/tmp/df.sh'

13.setup模块

ansible web -m setup -a 'filter="*mem*"'   #查看内存

========================

安装mariadb服务

---

- hosts: qf

remote_user: root

tasks:

- name: install mariadb-server package

yum: name=mariadb-server state=present

- name: starting mariadb-server service

service: name=mariadb state=started

============================

---

- hosts: qf

remote_user: root

vars:

- rpmname: nginx

nginxport: 8080

tasks:

- name: install {{ rpmname }}

yum: name={{ rpmname }} state=present

- name: copy {{ rpmname }}.conf

template: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

notify: reload

tags: reload{{ rpmname }}

- name: start {{ rpmname }} service

service: name={{ rpmname }} state=started

tags: start{{ rpmname }}

handlers:

- name: reload

service: name={{ rpmname }} state=restarted

ansible-playbook nginx.yml   &&   ansible qf -m  shell  -a  'ss -tunlp|grep :80'

ansible  qf  -m  shell  -a  'systemctl  stop  nginx'    ansible qf -m  shell  -a  'ss -tunlp|grep :80'

ansible-playbook  nginx.yml  -t  startnginx

===============================================

---

- hosts: qf

vars:

IP: "{{ ansible_ens33['ipv4']['address'] }}"

tasks:

- name: 将原有的hosts文件备份

shell: mv /etc/hosts /etc/hosts_bak

- name: 将ansible端的hosts复制到各自机器上

copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644

- name: 将新的hosts文件后面追加各自机器内网和hostname

lineinfile: dest=/etc/hosts line="{{ IP }} {{ ansible_hostname }} "

===========================

---

- hosts: qf

remote_user: root

tasks:

- name: install {{ rpmname }}

yum: name={{ rpmname }} state=present

- name: copy {{ rpmname }}.conf

template: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

notify: reload

tags: reload{{ rpmname }}

- name: start {{ rpmname }} service

service: name={{ rpmname }} state=started

tags: start{{ rpmname }}

handlers:

- name: reload

service: name={{ rpmname }} state=restarted

cp  /etc/keepalived/keepalived.conf  /tmp/keepalived.conf.j2

ansible-playbook  nginx.yml  -e  rpmname=keepalived

=================================

template模板

---

- hosts: qf

tasks:

- name: install conf file to centos7

shell: cat /etc/redhat-release > /root/aa.txt

when: ansible_distribution_major_version == "7"

- name: install conf file to centos6

template: src=files/nginx.cong.c6.j2

when: ansible_distribution_major_version == "6"

- name: install web packages

yum:

name: [ 'tcpdump', 'net-tools' ]

state: present

- name: add some groups

group: name={{ item }} state=present

with_items:

- group11

- group12

- group13

- name: add some users

user: name={{ item.name }} group={{ item.group }} state=present

with_items:

- { name: 'user11', group: 'group11' }

- { name: 'user12', group: 'group12' }

- { name: 'user13', group: 'group13' }

============================================

[root@server tmp]# vim nginx.conf.j2

worker_processes  {{ ansible_processor_vcpus }};

listen       {{ nginxport }};

---

- hosts: qf

remote_user: root

vars:

- rpmname: nginx

nginxport: 8888

tasks:

- name: install {{ rpmname }}

yum: name={{ rpmname }} state=present

- name: copy {{ rpmname }}.conf

template: src=/etc/{{ rpmname }}/{{ rpmname }}.conf dest=/etc/{{ rpmname }}/{{ rpmname }}.conf.bak backup=yes

notify: reload

tags: reload{{ rpmname }}

- name: start {{ rpmname }} service

service: name={{ rpmname }} state=started

tags: start{{ rpmname }}

handlers:

- name: reload

service: name={{ rpmname }} state=restarted

~

=============================================

roles       角色定制

[root@node12 roles]# tree

.

└── nginx

├── files

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

├── templates

│   └── nginx.cong.j2

└── vars

└── main.yml

==================================

vim handlers/main.yml    //触发器

---

- name: reload

service: name=nginx state=restarted

========================================

vim  tasks/main.yml

---

- name: install {{ rpmname }}

yum: name={{ rpmname }} state=present

- name: copy {{ rpmname }}.conf

template: src={{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

notify: reload

tags: reload{{ rpmname }}

- name: start {{ rpmname }} service

service: name={{ rpmname }} state=started

tags: start{{ rpmname }}

=========================================

vim vars/main.yml    //变量

---

nginxport: 9999

rpmname: nginx

========================================

vim  templates/nginx.conf.j2   //模板  //源文件   从nginx配置文件中复制过来的

=======

在roles统计目录

vim  /nginx_test.yml

---

- hosts: qf

roles:

- nginx

ansible-playbook nginx_test.yml

=============================================

转载于:https://blog.51cto.com/14181888/2379777

ansible基本操作相关推荐

  1. ansible 基本操作(初试)

    ansible 初级使用 yum install ansible -y 设置本机免密钥登录到 192.168.1.21 ssh-copy-id 192.168.1.21 vim /etc/ansibl ...

  2. ansible介绍+基本操作

    1ansible介绍 - Ansible基于Python语言实现,由paramiko和PyYAML两个关键模块构建 - 不需要安装客户端,通过sshd去通信 - 基于模块工作,模块可以由任何语言开发 ...

  3. ansible一些基本操作

    一.介绍 特性 (1).no agents:不需要在被管控主机上安装任何客户端: (2).no server:无服务器端,使用时直接运行命令即可: (3).modules in any languag ...

  4. ansible的模块使用

    查看ansible都有哪些模块: 查看模块的用法: fetch模块:将受管主机中的文件拉取到ansible主机上. 执行完之后,会自动创建192.168.116.152/etc/这个目录,并将拉取来的 ...

  5. Linux红帽认证工程师(RHCE)考试笔记(Ansible学习笔记)

    写在前面: 笔记是因为考红帽所以整理的,大都是老师的笔记,主要是常用模块整理,后面有些类似考试的实战题目,不是教程,教程建议大家到下面的学习网站,这篇博客适合温习用,层次有些乱,嘻嘻,生活加油,天天开 ...

  6. Ansible之ansible.cfg

    文章目录 配置文件概述 管理配置文件中的设置 基本操作配置 配置说明 连接设置 升级特权 例子:使管理主机可以通过密钥登录受管主机 环境介绍 配置要求 过程 在管理主机上创建普通用户 为用户启用完整的 ...

  7. 自动化运维-Ansible(redhat 8)

    在控制节点安装Ansible redhat 8自带python 3:如果没有安装,需要自行安装 查看是否安装python3 [root@localhost ~]# yum list installed ...

  8. 管理Ansible配置文件

    部署Ansible 文章目录 部署Ansible 1. Ansible清单 1.1 清单的作用 1.2清单文件的位置 1.3 清单文件的优先级 2. Ansible配置文件 2.1 ansible配置 ...

  9. 02管理Ansible配置文件

    管理Ansible配置文件 1.配置Ansible 2.配置文件的优先级 3管理配置文件的设置 4.配置连接 4.1清单位置 4.2连接设置 4.3升级特权 4.4非ssh连接 4.5配置文件注释 5 ...

最新文章

  1. Java IO的一些思考
  2. windows中Navicat连接本地的mysql的问题解决
  3. leetcode算法题--不同的二叉搜索树
  4. JDK1.8 stream详解(转)
  5. 查看库中所有表有多少数据
  6. PyCharm出现module ‘matplotlib’ has no attribute ‘verbose问题
  7. 在SQL Server 2008中调用.net,dll
  8. Spring-BeanFactory源码分析
  9. java解压中文乱码_java使用解压zip文件,文件名乱码解决方案
  10. 洛谷 1087——FBI树
  11. java aio socket_java核心学习(三十三) 网络编程---AIO实现异步Socket通信
  12. Linux断点方法,一种基于Linux问题断点的定位方法及系统与流程
  13. Linux sh/bash[精华]
  14. python怎么创建变量balance_在Python中将变量从一个函数修改为另一个函数
  15. Python SciPy教程
  16. 文库网站开发,文库网站定制,仿百度在线文档网站建设
  17. 云电脑与远控软件有什么区别?如何选?
  18. 三门问题:张三模拟了100000遍告诉你答案 内附Matlab代码
  19. java+上传整个文件夹的所有文件
  20. java里字符的大小写转换

热门文章

  1. 程序员在囧途之软件投标实战
  2. 常用的上网 发帖技巧
  3. 苹果手机怎么设置时间24小时制_8款手机电池测试,iPhone 12 5G让人意外
  4. BUGKU 密码题:这不是摩斯密码
  5. 第五篇:JMeter 定时器
  6. hbase基础建表语句
  7. 华为代码质量军规 (1) 数组访问,必须进行越界保护
  8. FastDFS FAQ (欢迎反馈,我将及时整理)
  9. 机房管理系统——vb与excel链接2
  10. SQL SERVER 优化 50法