Ansible2.9安装与配置

发布时间 2023-04-07 15:20:42作者: 沈丘

一、先决条件

Centos7,Python 2(2.7 版)或 Python 3(3.5 及更高版本), 关闭SELinux,关闭防火墙。

二、安装Ansible

1、yum install epel-release -y

2、yum install ansible -y

三、配置SSH免密登录

1、ssh-keygen

2、ssh-copy-id root@192.168.1.1

3、ssh-keyscan 192.168.1.2 >> ~/.ssh/known_hosts

4、ssh root@192.168.1.1

四、添加主机清单

1、vim /etc/ansible/hosts

[webservers]

192.168.1.1

192.168.1.2

五、测试Ansible命令

1、ansible all -m ping

2、ansible all -a "/bin/echo hello"

3、ansible all -m user -a "name=user_name password=your_name"

4、ansible webservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"

5、ansible webservers -m yum -a "name=nginx state=present"

6、ansible webservers -m service -a "name=nginx state=started"

7、ansible webservers -m setup

六、使用剧本安装httpd服务

1、ansible-palybook verify-apache.yml

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service:
      name: httpd
      state: started
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted