ansible+docker

1.准备镜像:

1007 docker run -itd --name client2 ff37bc5ab732
1008 docker run -itd --name client ff37bc5ab732
1009 docker run -itd --name client1 ff37bc5ab732
1010 docker run -itd --name ansible ff37bc5ab732
1011 docker exec -it ansible /bin/bash

2.容器ansible安装:

yum --enablerepo=epel -y install ansible openssh-clients

mv /etc/ansible/hosts /etc/ansible/hosts.org 
[root@784390b5dd19 /]# cat /etc/ansible/hosts

## db-[99:101]-node.example.com

# write clients you manage
172.17.0.2

# possible to group
# define any group name you like
[target_servers]
# write clients to be grouped
172.17.0.2
172.17.0.3
172.17.0.4
172.17.0.5

3.客户端安装SSH-Agent.:简单两步完成:

ansible:
ssh-keygen -t rsa
ssh  172.17.0.5 mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh 172.17.0.5 'cat >> .ssh/authorized_keys'
ssh 172.17.0.5测试一下,秒过:
4.ansible安装成功:

[root@784390b5dd19 /]# ansible target_servers -m ping
172.17.0.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.17.0.4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.17.0.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.17.0.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@784390b5dd19 /]#

[root@784390b5dd19 /]# ansible target_servers -k -m command -a "uptime"
SSH password:
172.17.0.5 | SUCCESS | rc=0 >>
07:07:33 up 22:13, 1 user, load average: 0.40, 0.12, 0.07

172.17.0.2 | SUCCESS | rc=0 >>
07:07:34 up 22:13, 1 user, load average: 0.40, 0.12, 0.07

172.17.0.3 | SUCCESS | rc=0 >>
07:07:34 up 22:13, 1 user, load average: 0.40, 0.12, 0.07

172.17.0.4 | SUCCESS | rc=0 >>
07:07:34 up 22:13, 1 user, load average: 0.40, 0.12, 0.07

[root@784390b5dd19 /]#

5.当然是玩ansible playbook:
格式要注意!!!
5.1:
cat playbook_sample.yml 

- hosts: target_servers
tasks:
- name: jt
file: path=/home/jt.conf state=touch mode=0600

 运行:ansible-playbook playbook_sample1.yml

 

好了基本可以了,现在练习,当然一切都是容器,越来越喜欢docker了:playbook

5.2.

[root@784390b5dd19 ~]# cat playbook_sample1.yml
- hosts: target_servers
tasks:
- name: httpd is installed
yum: name=httpd state=installed
- name: httpd is running and enabled
service: name=httpd state=started enabled=yes

5.3

[root@784390b5dd19 ~]# cat playbook_sample2.yml
- hosts: target_servers
tasks:
- name: General packages are installed
yum: name={{ item }} state=installed
with_items:
- vim-enhanced
- wget
- unzip
tags: General_Packages
[root@784390b5dd19 ~]#

5.4使用变量:

[root@784390b5dd19 ~]# cat playbook_sample3.yml
- hosts: target_servers
tasks:
- name: Refer to Gathering Facts
command: echo "{{ ansible_distribution }} {{ ansible_distribution_version }} {{ ansible_memory_mb }}"
register: dist
- debug: msg="{{ dist.stdout }}"
[root@784390b5dd19 ~]#

5.5.

[root@784390b5dd19 ~]# cat playbook_sample4.yml
- hosts: target_servers
tasks:
- name: index file exists or not
shell: test -f /var/www/html/index.html
ignore_errors: true
register: file_exists
failed_when: file_exists.rc not in [0, 1]

- name: put index.html
shell: echo "httpd index" > /var/www/html/index.html
when: file_exists.rc == 1

[root@784390b5dd19 ~]#

后面太深了!!!!!

posted on 2017-06-09 11:48  Jt00  阅读(396)  评论(0编辑  收藏  举报

导航