ansible

作者: 断水流大师兄vs魔鬼筋肉人 | 来源:发表于2018-06-19 09:44 被阅读0次

````

[ansible-server]#vim /etc/hosts    解析

192.168.0.107 ansible

192.168.0.116 host1

192.168.0.117 host2

192.168.0.118 host3

192.168.0.119 host4

````

yum install -y ansible   仅服务主机安装

免密码公钥:ssh-keygen

传送给客户端:ssh-copy-id host$1

定义主机清单:vim /etc/ansible/hosts    

添加:host1 host2 host3 host4

简洁输出:ansible host1 -m ping -u root -k -o

3.使用用户名密码测试连通性:ansible host1 -m ping -u root -k

免询问:vim /etc/ssh/ssh_config

StrictHostKeyChecking no

8.shell模块: ansible host1 -m shell -a 'uptime' -u root -k -o

9.yum模块:ansible host1 -m yum -a 'name=vsftpd state=latest' -u root -k

````

1 增加主机组:vim /etc/ansible/hosts

[webserver]

host1

host2

host3

host4

ansible webservers -m ping -u root -k -o

2 增加用户名 密码:vim /etc/ansible/hosts

[webserver]

host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

ansible webservers -m ping -o  免用户名和密码成功

请思考主机用户名密码不同。如何设置?

[webservers]

host1 ansible_ssh_user='root' ansible_ssh_pass='777777'

host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

请将host1的sshd程序端口修改为2222:vim /etc/ssh/sshd_config

Port 2222

则:vim /etc/ansible/hosts  端口必须一致

[webserver]

host1 ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'

host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

ansible内部变量可以帮助我们简化主机清单的设置

vim /etc/ansible/hosts

[webserver]

host[1:4]

[webserver:vars]

ansible_ssh_user='root'

ansible_ssh_pass='666666'

5 子分组

[apache]

host[1:2]

[nginx]

host[3:4]

[webserver:children]

apache

nginx

[webserver:vars]

ansible_ssh_user='root'

ansible_ssh_pass='666666'

模块案列

准备环境:ansible all -m yum -a 'name=httpd state=removed' -o

ansible机:yum install -y httpd

mkdir apache     cd apache

cp -rf /etc/httpd/conf/httpd.conf .

修改httpd.conf端口:Listen  8080

vim apache.yaml      格式严谨

- hosts: host2

  tasks:

  - name: install apache packges

    yum: name=httpd state=present

  - name: copy apache conf

    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf

  - name: ensure apache is running

    service: name=httpd state=started enabled=yes

语法检测:ansible-playbook apache.yaml --syntax-check

列出任务:ansible-playbook apache.yaml --list-tasks

列出主机:ansible-playbook apache.yaml --list-hosts

执行:ansible-playbook apache.yaml

结果:host2 httpd安装,并且端口 Listen :8080

修改配置端口:Listen :9000

再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器

触发器

如果配置文件再发生变化:Listen 9080

通过role远程部署nginx并配置

文件目录结构

准备目录结构

mkdir roles/ngin  {files,handlers,tasks,templates,vars} -p

touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml

echo 1234 > roles/nginx/files/index.html

yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2

2.准备配置文件:vim roles/nginx/templates/nginx.conf.j2

=============================================

3.编写剧本:vim roles/site.yaml

- hosts: host4

  roles:

  - nginx

4.编写任务:vim roles/nginx/tasks/main.yaml

---

- name: install nginx packge

  yum: name={{ item }} state=latest

  with_items:

  - epel-release

  - nginx

- name: copy index.html

  copy: src=index.html dest=/usr/share/nginx/html/index.html

- name: copy nginx.conf template

  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

  notify: restart nginx

- name: make sure nginx service running

  service: name=nginx state=started enabled=yes

5.编写处理程序:vim roles/nginx/handlers/main.yaml

---

- name: restart nginx

  service: name=nginx state=restarted

6.编写变量:vim roles/nginx/vars/main.yaml

worker_connections: 10240

测试:ansible-playbook site.yaml --syntax-check

实施剧本:ansible-playbook site.yaml

相关文章

  • 【Ansible学习】- Ansible初探

    安装 Ansible基础 Ansible架构图 Ansible核心组件说明 Ansible:Ansible的核心程...

  • 快速上手 Ansible

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 命令

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 主机清单配置文件

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible Playbook 剧本语法

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible(一)、实现SSH免密

    1.ansible安装 2.ansible配置 /etc/ansible/ansible.cfg /etc/ans...

  • Ansible第一篇:基础

    一、配置文件 ansible.cfg /etc/ansible/ansible.cfg 是ansible安装好后...

  • 自动化运维-ansible

    目录 十五、ansible介绍十六、ansible安装十七、ansible远程执行命令十八、ansible拷贝文件...

  • 04-invertory

    Ansible Ansible version : 2.6.2 inventory Ansible可以同时处理基础...

  • Ansible部署

    安装ansibleyum install ansible 确认ansible版本ansible --version...

网友评论

      本文标题:ansible

      本文链接:https://www.haomeiwen.com/subject/kuxwlftx.html