ansible

1.配置yum源:上传epel.repo
yum clean all
yum update

安装ansible
查询是否有ansible   yum list *ansile
查看该ansible的信息   yum info ansible.noarch

安装ansible
yum install ansible.noarch  -y


2.ansible前期配置
cd /etc/ansible
主配置文件:ansible.cfg
主机清单inventory:  hosts  存放主机IP 账号密码 或基于秘钥认证

主机管理清单 /etc/ansible/hosts
[webserver]    -->主机组 主机角色
192.168.122.7  -->主机ip
192.168.122.8

[dbserver]
192.168.122.9

ansible端传公钥给客户主机
ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.122.7

测试并执行命令
ssh 192.168.122.7  'date'


3.ansible模块

查看文档:man ansible-doc

查看ansible支持的所有模块
ansible-doc  -l

查看模块怎么使用
ansible-doc -s  yum


基本语法 man ansiable查看命令的使用

ansible   <host-pattern>  [-f forks]   [-m module_name]   [-a args]

<host-pattern> 对哪些主机生效
[-f forks]  一批处理多少个主机 启动多少个并发线程
[-m module_name] 使用哪个模块
[-a args] 模块特有的参数


常用模块
默认command
ansible-doc -s command  查看 command模块怎么使用

ansible 192.168.122.7 -m command -a 'date'  主机192.168.122.7 用command模块 指定参数(命令) date     -a args

ansible webserver -m command -a 'date'  指定主机组

ansible all  -m command -a 'date'  清单里的所有主机

ansible all  -m command -a 'tail -2 /etc/passwd'

可以不指定command 默认是 command模块(该模块不能使用变量)
ansible  all  -a  ‘date’


cron模块
ansible-doc -s cron 查看帮助
state   absent移除任务  present加上任务
ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello"  name="test cron job" state=present'

其他时间不加的默认都是*  job定时任务执行的命令 name是注释 state=present 是加上这个定时任务 也可以不写默认加上
查看是否加上定时任务 ansible webserver -a 'crontab -l'


ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello"  name="test cron job" state=present' 移除定时任务


user模块
ansible-doc -s user 查看帮助

ansible all -m user  -a 'name=haha' 创建haha

查看是否创建成功 ansible all -a 'tail /etc/passwd'
查看是否默认创建私有组 ansible all -a 'tail /etc/group'

删除
ansible all -m user  -a "name='haha' state=absent"


group模块
ansible-doc -s group

ansible webserver -m group -a 'name=mysql gid=666 system=yes' 创建mysql组 gid:666 系统组

ansible webserver -m user -a 'name=mysql uid=666 group=mysql system=yes'  创建mysql用户

ansible webserver -m user -a 'name=mysql uid=666 group=mysql shell="/sbin/nologin" ' 创建用户 指定不登录系统


copy模块
ansible-doc -s copy
src:本地文件路径 可以是相对路径
dest: 远端文件保存路径 必须绝对路径
ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=mysql mode=640'  本地文件/etc/fstab 复制到远端=/tmp/fstab.ansible 属主:mysql 权限640
查看复制是否成功 ansible all -a 'ls -al /tmp'

content=指定文件内容
ansible all -m copy -a 'content="hello world\nyou are welcome\n" dest=/tmp/test.ansible'
直接复制内容给远端 并保存到远端的指定文件


file模块
ansible-doc -s file

ansible all -m file -a 'owner=root group=root mode=644 path=/tmp/fstab.ansible'
设置文件属性 远端文件路径为path=/tmp/fstab.ansible'

path:创建文件的路径 可以使用name或dest来替换
src:远端目标源文件
ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link' 建软连接



ping模块
批量测试目标主机是否连通
ansible all -m ping  ping所有主机



service模块
管理服务
ansible-doc -s service
ansible webserver -m service -a 'enabled=true name=rpcbind state=started'
enabled:开机开启服务 name:服务名称 state: 状态
ansible dbserver -m service -a 'enabled=true name=httpd state=started'



shell模块
ansible-doc -s shell
用于有变量或特殊功能的命令时 用shell模块
ansible all -m shell  -a 'echo 123456 | passwd --stdin user1'
查看是否有密码 cat /etc/shadow


script模块
将本地脚本复制到远程服务器并执行
ansible-doc -s script
 ansible all -m script -a  '~/test.sh'



yum模块
ansible-doc -s yum
安装程序包
name:指定安装的程序 state:latest 最新或指定版本   state:absent 卸载 state:present 安装     默认安装
ansible all -m yum -a 'name=tree state=latest'
 ansible all -m yum -a 'name=tree state=absent'   卸载



setup模块
收集远程主机的信息
包括主机 操作系统版本 IP地址
ansible-doc -s setup


4.ansible  yaml模块
yaml基础元素变量 inventory 条件 迭代


playbook组成

Inventory
Modules
Ad Hoc Commands

playbooks
Tasks:任务 调用模块完成任务
variables:变量
templates:模板
Handlers:处理器,某条件触发时执行的操作
Roles:角色

playbook基本结构
- host:webserver
remote_user:
tasks:
 
- task1
       module_name:module_args
  - task2

比如:

nginx.yaml
- hosts: webserver
  remote_user: root
  tasks:
 
- name: create nginx group #任务名字
   
group: name=ginx gid=505 system=yes #group模板  后面是3个参数
 
- name: create nginx user #任务名字
   
user: name=nginx uid=505 group=nginx system=yes
- hosts: dbserver
  remote_user: root
  tasks:
 
- name: copy file to dbserver
    copy: src=/etc/inittab dest=/tmp/inittab.ansible
    ignore_errors: True  #忽略所有错误


httpd.yaml
- hosts: webserver
  remote_user: root
  vars:
    package:
httpd  #定义变量
   
service: httpd
  tasks:
   
- name: install httpd package
      yum: name={{package}} state=latest  #使用变量{{package}}
   
- name: install configuration file for httpd
      copy: src=/root/conf/httpd.conf  dest-/etc/httpd/conf/httpd.conf
      notify:   #/etc/httpd/conf/httpd.conf 与之前发生改变时触发handlers
     
- restart httpd
    - name: start httpd serice
      service: enabled=true name={{service}} state=started

  handlers:   notify触发的任务
  - name: restart httpd #与前面notify后面的一致
   
service: name=httpd state=restarted #模块 操作

vi /etc/ansible/hosts
[webserver]
192.168.122.7 testvar="100.7" ansiable_ssh_user=root ansible_ssh_pass=123456
[dbserver]
192.168.122.9 testvar="100.9"


var.yaml
- hosts: webserver
  remote_user: root
  tasks:
 
- name: copy file
    copy: content="{{ansible_date_time}},{{testvar}}" dest=/tmp/var.ansible  #引用ansible变量


条件测试:
when:
实例:cond.yaml

- hosts: all
  remote_user: root
  vars:
    username:
user10
  tasks:
   
- name: create {{username}} user
      user: name={{username}}
      when:  ansible_fqdn == ”www1.rhce.cc”
    - name: add several users
      user: name={{item}} state=present groups=wheel
      with_items:
       
- testuser1
        - testuser2


ansible变量获取: ansible 192.168.122.7 -m setup


迭代:重复同类task时使用
调用item
定义循环列表: with_items
- name: add several users
  user: name={{item}} state=present groups=wheel
  with_items:
   
- testuser1
    - testuser2

等同于:
 
- name: add several users
   user: name=testuser1 state=present groups=wheel
 - name: add several users
   user: name=testuser2 state=present groups=wheel

with_items中的列表值也可以是字典,引用时要使用item.KEY
实例
- name add several users
  user:name={{item.name}}  state=present  groups={{item.groups}}
  with_items:
   
- {name: ’testuser1’,  groups: ’wheel’}
    - {name: ’testuser2’,  groups: ’root’ }
   相当于:
- name add several users
  user: name=testuser1 state=present groups=wheel
- name add several users
  user: name=testuser2 state=present groups=root


实例:
yum:name={{item.name}}  state=present  conf_file={{item.conf}}
with_items:
 
- {name: apache, conf: conffiles/httpd.conf}
  - {name: php, conf: conffiles/php.ini}
  - {name: mysql-server, conf: conffiles/my.cnf}


tempaltes:
可自定义主机名变量 也可以用ansible变量
vi /etc/ansible/hosts
[webserver]
192.168.122.7 testvar="100.7" http_port=1007
[dbserver]
192.168.122.9 testvar="100.9" http_port=1009

模板
vim templates/httpd.conf.j2
Listen  {{http_port}}
ServerName {{ansible_fqdn}}


修改playbook文件
cp httpd.yaml httpd2.yaml
vim http2.yaml
- hosts: all
  remote_user: root
  vars:
    package:
httpd
    service: httpd
  tasks:
 
- name: install httpd package
    yum: name={{package}} state=latest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf.d/http.conf
    notify:
   
- restart httpd
  - name: after installed and started service
    service: enabled=true name={{service}} state=started

  handlers:
 
- name: restart httpd
    service: name=httpd state=restarted


tags:在playbook中可以为某个任务定义一个标签,执行此playbook时,通过命令
ansible-playbook httpd.yaml --tags="conf"  实现仅运行指定的tags 而非所有

特殊tags:
   
- always
无论指定哪个tags 这个tags都会运行

cp httpd2.yaml httpd3.yaml
vim httpd3.yaml
- hosts: all
  remote_user: root
  vars:
    package:
httpd
    service: httpd
  tasks:
 
- name: install httpd package
    yum: name={{package}} state=latest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf.d/http.conf
    tags:
   
- conf
    notify:
   
- restart httpd
  - name: after installed and started service
    service: enabled=true name={{service}} state=started

  handlers:
 
- name: restart httpd
    service: name=httpd state=restarted


roles:
1,目录名同角色名
2,目录结构有固定格式
  files:直接复制的静态文件
  templates: 模板文件 或jinjia2
  tasks:至少有main.yml文件,定义各tasks
  hanlder:至少有一个main.yml文件,定义各handlers
  vars:至少有一个main.yml文件,定义变量
  meta:定义依赖关系等信息
3,site.yml 定义 playbook

实例:
ansible_playbooks/
├── roles
│   ├── dbserver
│   │   ├── files
│   │   │   └── my.cnf
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   └── webserver
│       ├── files
│       │   └── httpd.conf
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       └── vars

└── site.yml



site.yaml
- hosts: 192.168.122.7
  remote_user: root
  roles:
 
- webserver

- hosts: 192.168.122.9
  remote_user: root
  roles:
 
- dbserver


- hosts: 192.168.122.8
  remote_user: root
  roles:
 
- webserver
  - dbserver


webserver角色
tasks-->main.yml
- name: install httpd package
  yum: name=httpd
- name: install configuration file
  template: src=httpd.conf.j2 dest=/etc/httpd/conf.d/http.conf
  notify:
 
- restart httpd
- name: start httpd
  service: name=httpd state=started

handlers-->main.yml
- name: restart httpd
  service: name=httpd state=restarted

templates-->httpd.conf.j2

dbserver角色
tasks-->main.yml
- name: install mysql-server package
  yum: name=mariadb state=latest
- name: install configuration file
  copy: src=my.cnf dest=/etc/my.cnf
  tags:
 
- myconf
  notify:
 
- restart mariadb
- name: start mariadb
  service: name=mariadb enabled=true state=started


handlers-->main.yml
- name: restart mariadb
  service: name=mariadb state=restarted

files-->my.cnf


运行playbook
man ansible-playbook
ansible-playbook site.yaml

原文地址:https://blog.51cto.com/13707996/2432211

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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'