Ansible搭建及配置使用

一、ansible的搭建及配置
1、wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
2、yum clean all && yum makecache
3、yum install ansible -y
4、ansible --version


二、配置设定免密登录及验证
1、vim /etc/ansible/hosts
[node1]
192.168.243.129
2、ssh-keygen -t rsa 生成秘钥
3、cd /root/.ssh/ && ssh-copy-id -i id_rsa.pub root@192.168.243.129 将密码分发给对应的主机
4、ssh root@192.168.243.129 验证无需输密码登录
5、ansible node1 -m ping 测试查看管理的主机通不通
192.168.243.129 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}

三、ansible常用模块

1、shell模块sudo的使用
ansible node01 -m shell -a "ls /root" -u lihu -k --become --become-user root -K #-u是指用哪个用户连入node01 --become-user是指提权root用户 -K是输入的lihu用户的密码
2、copy模块
ansible node01 -m copy -a "src=nginx-1.13.7.tar.gz dest=/tmp" && ansible node01 -m shell -a "ls /tmp" #copy及查看
3、file模块(在目标主机创建文件)
ansible node01 -m file -a "path=/tmp/a mode=600 state=touch" #创建一个文件,权限为600
ansible node01 -m file -a "path=/tmp/abc mode=600 state=directory" #创建一个目录,权限为600
ansible node01 -m file -a "path=/tmp/a mode=600 state=absent" #absent删除文件或目录
4、yum模块
ansible node01 -m yum -a "name=nginx state=present" #yum安装nginx
ansible node01 -m yum -a "name=nginx state=absent" #卸载nginx
5、user模块(添加删除用户)
ansible node01 -m user -a "name=foo password=123456 shell=/sbin/nologin" #添加用户设定不能登录
ansible node01 -m user -a "name=foo state=absent" #删除用户
6、git模块(拉取一些源代码)
ansible node01 -m git -a "repo=https://github.com/ansible/ansible.git dest=/tmp/ansible" #node01主机上要有git命令
7、service模块
ansible node01 -m service -a "name=memcached state=started(stopped restarted reloaded)" #启动服务
ansible node01 -m service -a "name=memcached enabled=true)" #设定服务开机启动
8、setup模块
ansible node01 -m setup -a "filter=ansible_*_mb" #查询目标主机的相关参数通过filter过滤出相关信息

 

四、ansible Playbook语法
1、Playbook核心元素
Hosts 执行的远程主机列表
Tasks 任务集
Varniables 内置变量或自定义变量在playbook中调用
Templates 模板,即使用模板语法的文件,比如配置文件等
Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
tags 标签,指定某条任务执行,用于选择运行playbook中的部分代码。

2、创建playbook文件及相关解释

[root@ansible ~]# cat playbook01.yml

---                       #固定格式
- hosts: node1     #定义需要执行主机
  remote_user: root       #远程用户
  vars:                   #定义变量
    http_port: 8088       #变量

  tasks:                             #定义一个任务的开始
    - name: create new file          #定义任务的名称
      file: name=/tmp/playtest.txt state=touch   #调用模块,具体要做的事情
    - name: create new user
      user: name=test02 system=yes shell=/sbin/nologin
    - name: install package
      yum: name=httpd
    - name: config httpd
      template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify:                 #定义执行一个动作(action)让handlers来引用执行,与handlers配合使用
        - restart apache      #notify要执行的动作,这里必须与handlers中的name定义内容一致
    - name: copy index.html
      copy: src=/var/www/html/index.html dest=/var/www/html/index.html
    - name: start httpd
      service: name=httpd state=started
  handlers:                                    #处理器:更加tasks中notify定义的action触发执行相应的处理动作
    - name: restart apache                     #要与notify定义的内容相同
      service: name=httpd state=restarted      #触发要执行的动作

3、ansible-playbook的用法 (执行yml文件)
ansible-playbook playbook01.yml -C
ansible-playbook playbook01.yml -v
[root@ansible PlayBook]# ansible-playbook -h
ansible-playbook常用选项:
--check or -C \\只检测可能会发生的改变,但不真正执行操作
--list-hosts \\列出运行任务的主机
--list-tags \\列出playbook文件中定义所有的tags
--list-tasks \\列出playbook文件中定义的所以任务集
--limit \\主机列表 只针对主机列表中的某个主机或者某个组执行
-f \\指定并发数,默认为5个
-t \\指定tags运行,运行某一个或者多个tags。(前提playbook中有定义tags)
-v \\显示过程 -vv -vvv更详细

4、ansible-doc的用法
[root@ansible ~]# ansible-doc -l \\列出所有模块
[root@ansible ~]# ansible-doc modulename \\查询某个模块使用方法
[root@ansible ~]# ansible-doc -s modulename \\查询某个模块使用方法 (以简洁的方式)

5、playbook结合notify handlers及meta的用法

---
- hosts: node01
  tags: mkd
  tasks:
  - name: task1
    tags: t1,tag1                    #设定标签,多个标签名可以用逗号隔开
    file:
      path: /tmp/testfile1
      state: directory
    notify:                          #task1结束后触发notify,如果task1没有任何changed改变将不会触发handlers任务
      - handler1
  - meta: flush_handlers             #默认从上至下依次执行,如遇meta,将meta前的任务执行完跳转去执行对应调用的handler任务
  - name: task2
    file:
      path: /tmp/testfile2
      state: directory
    notify:
      - handler2
  handlers:                         #和tasks任务平级
  - name: handler1
    listen: handler group1          #可将handler任务加到相同的组内,task下通过nofify调用组名来触发执行多个handler任务
    file:
      path: /tmp/testfile1/task1.txt
      state: touch
  - name: handler2
    file:
      path: /tmp/testfile2/task2.txt
      state: touch

ansible-playbook --tags=t1 mkdir.yml #执行标签为t1的任务
ansible-playbook --skip-tags=t1 mkdir.yml #执行跳过标签为t1的任务
ansible-playbook --tags tagged mkdir.yml #只执行有标签的任务,没有任何标签的任务不会被执行
ansible-playbook --skip-tags tagged mkdir.yml #执行跳过包含标签的任务,即使对应的任务包含always标签,也会被跳过
ansible-playbook --tags untagged mkdir.yml #只执行没有标签的任务,但是如果某些任务包含always标签,那么这些任务也会被执行
ansible-playbook --skip-tags untagged mkdir.yml #执行跳过没有标签的任务

注:当tags写在play中而非task中时,play中的所有task会继承当前play中的tags,同时还有拥有自己的标签。
ansible-playbook --tags=mkd mkdir.yml

6、示例,批量创建用户
vim useradd.yml

---
- hosts: node1
  become: yes
  vars:
    user_list: ['lihu11','lihu22','lihu33']
  tasks:
    - name: create user
      user:
        name: "{{ item }}"
        password: "{{ '123456'| password_hash('sha512') }}"
        state: present
      with_items:
        - "{{ user_list }}"

 示例,搭建nginx

---
- hosts: node01
  vars:
    username: LIHU
  tasks:
  - name: Add repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: 1
  - name: Install nginx
    yum:
      name: nginx
      state: latest
  - name: Copy nginx configuration file
    copy:
      src: /root/myplaybook/site.conf
      dest: /etc/nginx/conf.d/site.conf
  - name: Start nginx
    service:
      name: nginx
      state: started
  - name: create wwwroot directory
    file:
      dest: /var/www/html
      state: directory
  - name: create test page index.html
    shell: echo "hello {{username}}" > /var/www/html/index.html

 

posted @ 2022-05-25 15:33  Me-lihu  阅读(624)  评论(0编辑  收藏  举报