Ansible-playbook start

2024-07-23  本文已影响0人  carvin

Ansible大纲:

Ansible被红帽收购--->

什么是Ansible

Ansible特性\优点....

ansible jinja模板

        keeplaived

        nginx_proxy

ansible role角色

        编排工具--->清晰目录规划--->严格按照目录规划来

ansible galaxy

ansible tower   

ansible部署集群架构

 ansile 配置文件存在优先级的问题

        ANSIBLE_CONFIG

        anslibe.cfg         项目目录

        .ansible.cfg        当前用户的家目录

        /etc/ansible/ansible.cfg

1.5 ansilbe主机清单host

#方法1

[webservers]

192.168.6.21 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='jinlong431//'

192.168.6.22 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='jinlong431//'

#方法2

[webserver]

web[1:2].oldboy.com ansible_ssh_pass='jinlong431//'

#方法3

[webserver]

web[1:2].oldboy.com

[webserver:vars]

ansible_ssh_pass='jinlong431//'

列出组内所有 机器:

ansible webservers1 --list-hosts

1.7 使用ad-hoc执行一次远程命令,注意观察返回结果的颜色

1.7.1 首先学习的三个模块

  • 命令 command shell scripts
  • 安装 yum
  • 配置 copy file get_url
  • 启动 service systemd
  • 用户 user group
  • 任务 cron
  • 挂载 mount
  • 防火墙 selinux firewall
  • command shell 本质上执行的都是shell命令
  • command 不能使用管道符

1.7.2 yum模块

示例1. 安装当前最新的apache软件,如果存在则更新

    ansible webservers1 -m yum -a "name=httpd state=latest"

示例2. 安装单项最新的Apache软件,通过epel仓库安装

ansible webservers1 -m yum -a "name=httpd state=latest enablerepo=epel"

示例3. 通过公网URL安装rpm软件

    ansible webservers1 -m yum -a "name=https://zabbix.rpm  state=latest"

示例4. 更新所有的软件包,大排除和kernel有关的

    ansible webservers1 -m yum -a "name="*"  state=latest  exclude=kernel*"

示例5. 删除Apache软件

    ansible webservers1 -m yum -a "name=httpd  state=absent"

1.8 ansible 常用模块-copy

ansible webservers1 -m copy -a "src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644 backup=yes"


4. 在被控端加入文件,并文件写入内容
    ansible webservers1 -m copy -a "content=HttpServer... dest=/var/www/html/index.html"

1.9 get_url

file模块:创建文件夹  授权

state touch directory

recurse

owner group mode

1.10 ansible常用模块service

1.11 ansible 常用的group模块

ansible webservers1 -m user -a 'name=jsm password=$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1 create_home=yes'

1.12 ansible 常用模块-cron

关闭selinux

ansible webservers1 -m selinux -a "state=disabled"

** 如果报错

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'selinux'

** 在受控客户端安装:

yum -y install libseliunx-python3

firewalld模块

1.13 playbook

playbook 剧本

play  (找谁)

task   (干什么)

找一个人干多件事情 playbook 1个play 多个task

找多个人干多件事情 playbook 多个play 多个task

playbook 是由yaml语法书写,结构清晰,可读性强,所以必须掌握yaml基础语法

语法 描述

缩进 yaml使用固定的缩进风格表示层级结构,每一个缩进有两个空格组成,不能使用tabs冒号 以冒号结尾的除外,其他所有冒号后面必须有空格。

短横线 表示列表项,使用一个短横线加一个空格。多个项使用同样的缩进级别作为同一列表。

[root@ansible-master ~]# cat /etc/ansible/http.yaml 

- hosts: webservers1

  tasks:

    - name: Install Httpd Server

      yum:

        name: httpd

        state: present

    - name: Configure Httpd Server

      copy: 

        src: /root/httpd.conf

        dest: /etc/httpd/conf/httpd.conf

        backup: yes

    - name: Configure Httpd Server

      copy:

        src: /root/tt.html

        dest: /var/www/html/tt.html

        owner: http

        group: http

        mode: 644

    - name: Start Httpd Server

      service:

        name: httpd

        state: started

        enabled: yes

    - name: Start Firewalld Server

      service:

        name: firewalld

        state: started

    - name: Configure Firewalld Server

      firewalld:

        zone: public

        port: 8090/tcp

        permanent: yes

        immediate: yes

        state: enabled
[root@ansible-master ~]# ansible-playbook --syntax  /etc/ansible/http.yaml

playbook: /etc/ansible/http.yaml
    ansible-playbook -C /etc/ansible/http.yaml
[root@ansible-master ~]# cat /etc/ansible/nfs.yaml 

- hosts: 192.168.6.21

  tasks:

    - name: Install NFS Server

      yum:

        name: nfs-utils

        state: present

    - name: Configure NFS Server

      copy:

        src: /root/exports

        dest: /etc/exports

        backup: yes

    - name: Create NFS Group

      group:

        name: www

        gid: 666

    - name: Create NFS User

      user:

        name: www

        uid: "666"

        group: "666"

        shell: /sbin/nologin

        create_home: no

    - name: Create NFS Data

      file:

        path: /data

        state: directory

        owner: www

        recurse: yes

    - name: Start NFS Server

      service:

        name: nfs

        state: started

        enabled: yes

- hosts: 192.168.6.22

  tasks:

    - name: Client Create NFS Data

      file:

        path: /nfs_client

        state: directory

    - name: Client Mount NFS Server

      mount:

        src: 192.168.6.21:/data

        path: /nfs_client

        fstype: nfs

        opts: defaults

        state: mounted
上一篇 下一篇

猜你喜欢

热点阅读