zoukankan      html  css  js  c++  java
  • ansible

    配置

    1、vim /etc/ansible/hosts                             加入IP地址并分组

    [web]

    192.168.1.101

    192.168.1.102

    192.168.1.103

    [db]

    192.168.1.[103:104]                                  表示192.168.1.103,192.168.1.104两台

    192.168.1.105

    [centos6]

    192.168.1.101

    192.168.1.102

    [centos7]

    192.168.1.103

    192.168.1.104

    192.168.1.105

    2、vim /etc/ansible/ansible.cfg     查找首次登陆不不提示

    host_key_checking = False       取消注释

    log_path = /var/log/ansible        地址可以更改,取消注释,开启日志功能

    3、ssh-keygen   密钥分发

    ssh-copy-id 192.168.1.101   对于这步,服务器多,可以写脚本执行

    ssh-copy-id 192.168.1.102

    ssh-copy-id 192.168.1.103

    ssh-copy-id 192.168.1.104

    ssh-copy-id 192.168.1.105

    常用模块:command,shell,copy,group,user,yum,file,fetch

    copy:拷贝本地文件并传递到远程服务器

      src=/root/f1.sh dest=/tmp/f2.sh owner=wang mode=600 backup=yes(如果有同名文件,备份)

      content=‘test content ’ dest=/tmp/f1.txt  利用content里的内容直接生成目标文件

    fetch:抓取远程服务器上的文件到本地(和copy相反)

      src=root/a.sh dest=date/scripts          如果是目录可以先tar打包再传送

    group: 创建删除组

      name=testgroup system=yes(添加系统组)

      name=testgroup state=absent

    user:创建删除用户,设置用户属性

      name=tony comment=“test user” (注释)  uid=2048  home=/app/tony group=root  system=yes(系统用户添加此项) 

      name=user1 state=absent remove=yes(添加此项删除家目录)

    yum:安装包

      name=httpd state=latest/absent(最新版本/删除)

    file:创建文件,设置属性,软连接

      path=file.yml state=directory|touch|absent|link  mode=600 owner=root 

    service:启动停止模块,设置自启动

      name=httpd state=started|stopped|reload

    script:调用脚本模块

    cron:计划任务模块   minute,hour,day,month,weekday

      minute=*/5 job="/usr/bin/wall hello world"  name=hello

      state=absent name=hello

    hostname:主机名模块

      name=centos7    设置主机名

    ping:用于检测服务器是否上线

    setup:记录了系统各种状态参数

    ansible-galaxy    从网上下载已经做好的roles

      ansible-galaxy roles_name  

        连接https://galaxy.ansible.com 下载相应的roles

      ansible-galaxy list   

        列出所有已安装的galaxy

      ansible-galaxy install geerligguy.redis

        安装galaxy

      ansible-galaxy remove geerlingguy.redis

        删除galaxy

    ansible-vault  管理加密解密文件

      ansible-vault [create|decrypt|edit|encrypt|rekey|view]

      ansible-vault encrypt hello.yml 加密

      ansible-vault decrypt hello.yml 解密

      ansible-vault view hello.yml 查看

      ansible-vault edit hello.yml 编辑加密文件

      ansible-vault rekey hello.yml 修改口令

      ansible-vault create new.yml 创建新文件

    Ansible-console:可交互执行命令,支持tab

      root@test (2)[f:10] $  执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$

        设置并发数: forks n 例如: forks 10

        切换组: cd 主机组 例如: cd web

        列出当前组主机列表: list

        列出所有的内置命令: ?或help

    ansible-playbook

      hosts 执行的远程主机列表

      tasks 任务集

      varniables 内置变量或自定义变量在playbook中调用

      templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件  

      Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行

    tags:标签,当需要只执行其中某个动作时使用          ansible-playbook  -t  install_httpd,copy_conf   httpd.yml

    limit:只执行主机名单中的某些主机,不用全部执行   ansible-playbook --limit  ''192.168.1.103 192.168.1.105"  httpd.yml

    httpd.yml


    ---
    - hosts: centos7
    remote_user: root

    tasks:
    - name: install httpd
    tags: install_httpd
    yum: name=httpd state=present
    - name: copy conf file
    copy: src=/app/httpd.conf dest=/etc/httpd/conf/ backup=yes
    tags: copy_conf
    notify: restart httpd
    - name: start httpd
    tags: start_httpd
    service: name=httpd state=started enabled=yes

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

    ansible-playbook 中的变量

      变量名:仅能由字母、数字和下划线组成,且只能以字母开头

      变量来源:

        1 ansible setup facts 远程主机的所有变量都可直接调用

        2 在/etc/ansible/hosts中定义

          普通变量:主机组中主机单独定义,优先级高于公共变量

          公共(组)变量:针对主机组中所有主机定义统一变量

        3 通过命令行指定变量,优先级最高

          ansible-playbook –e varname=value

        4 在playbook中定义

          - var1: value1

          - var2: value2

        5 在role中定义

        6 在创建专门的变量文件,将变量统一放置,使用时调用,文件后缀为yml

    变量定义使用方式

      1、var1.yml  

    ---
    - hosts: centos7
    remote_user: root

    tasks:
    - name: install package
    yum: name={{ pkname }} state=present
    - name: copy file
    copy: scr=/app/{{filename}} dest=/app/

      ansible-playbook -e "pkname=httpd,vsftpd  filename=1,2"  var1.yml

      2、va2.yml  

    ---
    - hosts: centos7
    remote_user: root
    vars:
    - username: user1
    - groupname: group1

    tasks:
    - name: create group
    group: name={{ groupname }} state=present
    - name: create name
    user: name={{ username }} group={{ groupname }} home=/app/{{ username }}

      ansible-playbook va2.yml

      3、vim /etc/ansible/hosts   在主机配置文件中添加变量

    [centos7]     
    192.168.1.103 httpd_port=85
    192.168.1.105 httpd_port=84
    192.168.1.104 httpd_port=83

    [centos7:vars]              针对主机组定义变量

    hname=c7               称为公共变量

      ansible centos7 -m hostname -a 'name=web{{httpd_port}}'

      4、创建单独的变量文件,使用变量统一从文件中调取

      vim /vars.yml 

      var1: httpd

      var2: nginx  

      vim /var5.yml

      ---

      - hosts: centos7

      remote_user: root

      vars_files:

         -  vars.yml

      tasks:

         -  name: create file

           file: name=app/{{var1}}-{{var2}}.log state=touch mode=600 owner=tony

    template:此功能主要用于传送配置文件到符合条件的主机上,并且配置文件可以内置变量以根据不同主机的情况进行自动配置

      

    ---
    - hosts: all
    remote_user: root

    tasks:
    - name: install httpd
    yum: name=httpd
    - name: template
    template: src=/root/ansible/templates/httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf
    when: ansible_distribution_major_version=="6"
    - name: template
    template: src=/root/ansible/templates/httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf
    when: ansible_distribution_major_version=="7"

      !!注意centos6的配置文件,可能会出故障,故障现象无法启动服务,提示没有获取主机名,将配置文件中servername的注释取消可以解决。

        when 功能来自ansible -m setup -a 'filter=*version*' 搜索关键字而来,setup中有系统中几乎所有的变量

    item迭代 

    - hosts: centos7
      remote_user: root

      tasks:
        - name: create servel users
          user: name={{ item }} group=root groups=tony,bin
          with_items:
            - user1
            - user2

    - hosts: centos7
      remote_user: root

      tasks:
      - name: create servel users
        copy: src={{ item }} dest=/app/
        with_items:
          - /app/f1
          - /app/f2
          - /app/f3
          - name: install packages
        yum: name={{ item }}
        with_items:
          - vsftpd
          - hping3
          - memcached

    迭代

    ---
    - hosts: centos7
      remote_user: root

      tasks:
        - name: create groups
          group: name={{ item }}
          with_items:
            - itemgroup1
            - itemgroup2
            - itemgroup3

    - name: create users
      user: name={{ item.name }} group={{ item.group }}
      with_items:
        - { name: testuser1, group: itemgroup1 }
        - { name: testuser2, group: itemgroup2 }
        - { name: testuser3, group: itemgroup3 }

    for-if,defined

    for2-if.yml

    - hosts: centos7
      remote_user: root
      vars:
      vhosts:
      - web1:
        port: 81
        #name: web1.magedu.com
        root: /app/webroot1
      - web2:
        port: 82
        name: web2.magedu.com
        root: /app/webroot2
      - web3:
        port: 83
        name: web3.magedu.com
        root: /app/webroot3

    tasks:
      - name: test for1
        template: src=for2-if.conf.j2 dest=/app/for2.conf

     

    for2-if.conf.j2

    {%for vhost in vhosts %}
    server {
           listen {{vhost.port}};
    {%if vhost.name is defined %}
                servername {{vhost.name}};
    {%endif%}
                rootdir {{vhost.root}};
    }
    {%endfor%}

  • 相关阅读:
    Twitter OA prepare: Rational Sum
    Java: Best Way to read a file
    Summary: gcd最大公约数、lcm最小公倍数算法
    Twitter OA prepare: Flipping a bit
    Twitter OA prepare: Equilibrium index of an array
    echo -e 参数
    openwrt 添加luci选项
    基于TLS的EAP 认证方法
    linux命令 dirname
    freeradius 错误: error:140890C7:SSL routines:ssl3_get_client_certificate:peer did not return a certificate
  • 原文地址:https://www.cnblogs.com/tony3154/p/10218670.html
Copyright © 2011-2022 走看看