其他分享
首页 > 其他分享> > ansible学习笔记

ansible学习笔记

作者:互联网

Ansible

一、基本模块介绍

模块分类:

默认使用ssh协议:

  1. 基于ssh将公钥拷贝到hosts
  2. 在inventory里面添加

命令

如何查看模块帮助:

# ansible-doc -l
# ansible-doc -s module name

command:命令模块,用于远程执行命令(不能使用变量)

# ansible 192.168.38.131 -m command -a 'date'
-a 后面加命令

cron:定时任务

state:present安装,absent:移除

# ansible 192.168.38.131 -m cron -a 'minute=*/10 job="/bin/echo hello" name="test cron job"'
name是必须要加的,如果后面加state=absent表示把这个定时任务移除

user:

#ansible 192.168.38.131 -m user -a 'name="wjm" state=absent'
表示删除远程主机上wjm的用户
system=: 表示是否为系统用户

group:

name=:  gid=: 
添加组操作

copy:

# ansible 192.168.38.131 -m copy -a 'src=/root/1.txt dest=/tmp/1.txt.bak owner=root mode=755'
src表示本机目录,dest表示远程主机目录,为绝对路径,owner属组,mode权限
# ansible 192.168.38.131 -m copy -a 'content="hello wjm\nwjm dashuaige\n" dest=/tmp/2.txt'
content组件表示向文件里面添加内容,远程主机的文件可以是未创建的
和src组件不能同时使用

file:

# ansible 192.168.38.131 -m file -a 'owner=root mode=777 path=/tmp/2.txt'
该组件可以修改文件的属性,path可以用dest或者name代替
# ansible all -m file -a 'path=/tmp/2.txt.link src=/tmp/2.txt state=link'
创建文件的符号链接:
src指定源文件,path指定符号链接路径

ping:

# ansible all -m ping
返回“pong”说明正常通信

service:

# ansible all -m service -a 'enabled=true name=httpd state=started'
enabled表示开机启动
name表示服务名称

shell:

# ansible all -m shell -a 'echo wjm0401 | passwd --stdin wjm'
shell与commend相比多支持管道变量等

scripts:

# ansible all -m script -a 'test.sh'
-a后面只支持相对路径,脚本要放在本机的当前路径下执行

yum:

# ansible all -m yum -a 'name=zsh state=absent'
去掉absent就是安装程序
name可以带上版本号

setup:

收集远程主机的facts信息
每个被管理节点在接受并运行管理命令之前,会将自己主机相关的信息,报告给ansible主机
# ansible all -m setup

yaml的语法可以简单的表达清单,散列表,标量等数据结构,其结构(structure)通过空格来展示,序列(sequence)里的项用“-”来表示,map里的键值用“:”表示

name:john smith
age:41
gender:male
spose:
       name:jane smith
       age:37
       gender:female
children:
       -     name:jimmy smith
              age:17
              gender:male
       -     name:jenny smith
              age:13
              gender:female

ansible中使用的yaml基础元素:

playbooks组成结构

例1:

- hosts: webservers
 remote_user: root
 tasks:
 - name: create nginx group
   group: name=nginx system=yes gid=208
 - name: create nginx user
   user: name=nginx uid=208 group=nginx system=yes
 - name: copy a file
   copy: src=/root/1.txt dest=/root/test1.ans

例2:

- hosts: webservers
  remote_user: root
  tasks:
  - name: install apache
    yum: name=httpd
  - name: install conf file
    copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start service
    service: enabled=true name=httpd state=started

handlers:当条件被触发时,执行某操作

例3:

- hosts: webservers
  remote_user: root
  tasks:
  - name: install apache
    yum: name=httpd
  - name: install conf file
    copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start service
    service: enabled=true name=httpd state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted

引用变量:vars

例:表示此时会用变量的值来替代

- hosts: webservers
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install apache
    yum: name={{ package }}
  - name: install conf file
    copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start service
    service: enabled=true name={{ service }} state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
- hosts: webservers
  remote_user: root
  tasks:
  - name: copy file
    copy: content="{{ ansible_all_ipv4_addresses }}" dest=/root/vars.ans
content组件表示向文件里面添加内容,远程主机的文件可以是未创建的
ansible_all_ipv4_addresses是有ansible all -m setup提取到的信息
在主机或者组中定义的变量也可以直接调用
注:在主机后面加 ansible_ssh_user=root ansible_ssh_pass=wjm0401后,即使没有做ssh信任关系,也可以连接到远程主机

when:表示,当符合某个条件时,才会执行上面task

- hosts: all
  remote_user: root
  vars:
  - username: user1
  tasks:
  - name: create user
    user: name={{ username }}
    when: ansible_fqdn == "localhost.localdomain

迭代:

当有需要重复性执行的任务时,可以使用迭代机制,其格式为将需要迭代的内容定义为item,并通过with_item语句来指明迭代的元素列表

例:

- hosts: all
  remote_user: root
  tasks:
  - name: create user
    user: name={{ item }} state=present groups=wjm
    with_items:
        - testuser1
        - testuser2

事实上,with_items可使用元素还可以使用hashes

- hosts: all
  remote_user: root
  tasks:
  - name: create user
    user: name={{ item.name }} state=present groups={{ item.group }}
    with_items:
        - { name: 'testuser3',group: 'root' }
        - { name: 'testuser4',group: 'root' }

templates:模板,创建一个templates文件夹,将服务的配置文件移到该目录下,修改名为httpd.conf.j2

将80修改为 {{ http_port }} maxclient后面修改为{{ maxclients }}

然后在hosts文件的主机后面定义变量;

例:

- hosts: webservers
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install apache
    yum: name={{ package }}
  - name: install conf file
    templates: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start service
    service: enabled=true name={{ service }} state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted

tags:当第二次运行某个playbook时,我们只期望运行某个修改的任务,这时只需要给相应的任务打上标签,运行时指定标签即可

例:

- hosts: webservers
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install apache
    yum: name={{ package }}
  - name: install conf file
    templates: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
       tags:
       - conf
    notify:
    - restart httpd
  - name: start service
    service: enabled=true name={{ service }} state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
执行时:--tags="conf"
特殊tags:always  无论如何都能运行的

roles:角色

  1. 目录名同角色名
  2. 目录结构有固定格式

3.调用时在roles目录之外创建site.yml定义playbook,定义role时可以采用哈希调用多个变量,也可以和when结合,

例:创建一个安装httpd的roles

#mkdir -pv ansible_playbooks/roles/webservers/{tasks,files,templates,meta,handlers,vars}
# cd ansible_playbooks/roles/webservers/
# cp /etc/httpd/conf/httpd.conf files/

files目录用于存放静态文件

# vim tasks/main.yml
- name: install httpd
  yum: name=httpd
- name: install conf file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  tags:
  - conf
  notify:
  - restart httpd
- name: start httpd
  service: name=httpd state=started
# vim handlers/main.yml
- name: restart httpd
  service: name=httpd state=restarted

cd到和roles平行的目录

# vim site.yml
- hosts: webservers
  remote_user: root
  roles:
  - webservers
# ansible-playbook site.yml

标签:httpd,name,service,笔记,学习,ansible,conf,root
来源: https://www.cnblogs.com/wjm0401/p/16696424.html