码迷,mamicode.com
首页 > 其他好文 > 详细

playbook使用

时间:2018-11-24 15:15:25      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:disable   could not   home   自启动   ansbile   http   操作   director   引用   

转自:   https://blog.csdn.net/chengyuqiang/article/details/78529454

[root@kube-node3 ~]# cat /etc/ansible/hosts 

[testhosts]

127.0.0.1

192.168.0.73


[root@kube-node3 ~]# vi hello.yml

---
- hosts: testhosts
  tasks:
     - name: "helloworld"
       shell: echo "Hello World" `date` by `hostname` > /tmp/hello.log

[root@kube-node3 ~]# ansible-playbook hello.yml

PLAY [testhosts] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
ok: [192.168.0.73]

TASK: [helloworld] ************************************************************ 
changed: [127.0.0.1]
changed: [192.168.0.73]

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0   
192.168.0.73               : ok=2    changed=1    unreachable=0    failed=0   


playbook的说明:


hosts: 是主机组,用于指定操作对象节点,多个节点用逗号分隔 
tasks: 用于指定要处理的内容 
name:task的名称,ansible可以把很多task使用playbook编排起来,通过名称,实际执行的时候可以清楚地看到执行情况
shell: ansible的shell模块,在前面的实例中我们已经知道command/shell/raw等的区别,所以可以知道这个简单的例子中使用哪个模块都能实现这个>简单的功能


对照执行的结果,基本上已经无需说明。 
changed有2个,unreachable和失败的都没有。



怎么会有setup这个task呢,那是因为default的情况下,facts是会被收集的的,我们可以通过设定参数gather_facts让其不被收集。


[root@node1 ~]# cat hello.yml
---
- hosts: testhosts
  gather_facts: false
  tasks:
     - name: "helloworld"
       shell: echo "Hello World" `date` by `hostname` > /tmp/hello.log

[root@kube-node3 ~]# ansible-playbook hello.yml

PLAY [testhosts] ************************************************************** 

TASK: [helloworld] ************************************************************ 
changed: [127.0.0.1]
changed: [192.168.0.73]

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=1    changed=1    unreachable=0    failed=0   
192.168.0.73               : ok=1    changed=1    unreachable=0    failed=0   



3、playbook变量使用

Ansbile还内嵌了七个很有用的变量,使用得当也会带来很大的便利。

ansible的playbook中的变量引用使用{{ }}。 
hostvars变量 
groups变量 
group_names变量 
inventory_hostname变量 
inventory_hostname_short
inventory_dir 
inventory_file

在vars后设定变量message,并将此message内容输出到log中

gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;

[root@kube-node3 ~]# cat hello.yml

---
- hosts: testhosts
  vars:
    - message: "hello,world"
  gather_facts: false
  tasks:
     - name: "helloworld"
       shell: echo {{message}} `date` by `hostname` > /tmp/hello.log

[root@kube-node3 ~]# ansible-playbook hello.yml

PLAY [testhosts] ************************************************************** 

TASK: [helloworld] ************************************************************ 
changed: [127.0.0.1]
changed: [192.168.0.73]

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=1    changed=1    unreachable=0    failed=0   
192.168.0.73               : ok=1    changed=1    unreachable=0    failed=0   



4、Ansible中使用环境变量, 

下面是直接摘抄的,我的centos上没有java,没法做

[root@node1 ~]# vi java.yml
[root@node1 ~]# cat java.yml 
---
- hosts: node2,node3
  gather_facts: false
  tasks:
     - name: "java-test"
       shell: echo ${JAVA_HOME} >/tmp/hello.log
[root@node1 ~]# ansible-playbook java.yml

PLAY [node2,node3] **************************************************************************************************************************************************************************

TASK [java-test] ****************************************************************************************************************************************************************************
changed: [node3]
changed: [node2]

PLAY RECAP **********************************************************************************************************************************************************************************
node2                      : ok=1    changed=1    unreachable=0    failed=0   
node3                      : ok=1    changed=1    unreachable=0    failed=0


[root@node2 ~]# cat /tmp/hello.log
/opt/jdk1.8.0_112
[root@node2 ~]# 


[root@node3 ~]# cat /tmp/hello.log
/opt/jdk1.8.0_112
[root@node3 ~]#





5、playbook的条件和循环
Ansible中有众多的模块,可以写playbook,同时里面也可以写条件判断和循环,这样基本上脚本能做的事情ansible大体都可以作了。条件判断使用when,循环使用with_items,接下来看一下如何使用的简单实例。

[root@node1 ~]# vi when.yml
[root@node1 ~]# cat when.yml 

---
- hosts: testhosts
  gather_facts: true
  tasks:
     - name: "redhat-test"
       shell: echo "RedHat"  `date` by `hostname` >> /tmp/hello.log
       when:  ansible_os_family ==  "RedHat"
     - name:  "other linux"
       shell: echo "Not RedHat"  `date` by `hostname` >> /tmp/hello.log
       when:  ansible_os_family !=  "RedHat"

[root@kube-node3 ~]# ansible-playbook when.yml

PLAY [testhosts] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
ok: [192.168.0.73]

TASK: [redhat-test] *********************************************************** 
changed: [127.0.0.1]
changed: [192.168.0.73]

TASK: [other linux] *********************************************************** 
skipping: [127.0.0.1]
skipping: [192.168.0.73]

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0   
192.168.0.73               : ok=2    changed=1    unreachable=0    failed=0   


[root@kube-node2 ~]# cat /tmp/hello.log
hello,world Tue Oct 2 14:03:39 CST 2018 by kube-node2
RedHat Tue Oct 2 14:10:43 CST 2018 by kube-node2

[root@kube-node3 ~]# cat /tmp/hello.log 
hello,world Tue Oct 2 14:03:39 CST 2018 by kube-node3
RedHat Tue Oct 2 14:10:43 CST 2018 by kube-node3



5.2循环

[root@node1 ~]# vi with_items.yml
[root@kube-node3 ~]# cat with_items.yml

- hosts: testhosts
  gather_facts: true
  tasks:
    - name:  "循环测试"
      shell: echo {{item}}  `date` by `hostname` >> /tmp/hello.log
      with_items:
        - message item1
        - message item2
        - message item3
        - message item4
        - message item5

[root@kube-node3 ~]# ansible-playbook with_items.yml 

PLAY [testhosts] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
ok: [192.168.0.73]

TASK: [循环测试] ****************************************************************** 
changed: [127.0.0.1] => (item=message item1)
changed: [192.168.0.73] => (item=message item1)
changed: [127.0.0.1] => (item=message item2)
changed: [192.168.0.73] => (item=message item2)
changed: [127.0.0.1] => (item=message item3)
changed: [192.168.0.73] => (item=message item3)
changed: [127.0.0.1] => (item=message item4)
changed: [192.168.0.73] => (item=message item4)
changed: [127.0.0.1] => (item=message item5)
changed: [192.168.0.73] => (item=message item5)

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0   
192.168.0.73               : ok=2    changed=1    unreachable=0    failed=0  


[root@kube-node2 ~]# cat /tmp/hello.log
hello,world Tue Oct 2 14:03:39 CST 2018 by kube-node2
RedHat Tue Oct 2 14:10:43 CST 2018 by kube-node2
RedHat Tue Oct 2 14:14:35 CST 2018 by kube-node2
message item1 Tue Oct 2 14:15:38 CST 2018 by kube-node2
message item2 Tue Oct 2 14:15:39 CST 2018 by kube-node2
message item3 Tue Oct 2 14:15:39 CST 2018 by kube-node2
message item4 Tue Oct 2 14:15:39 CST 2018 by kube-node2
message item5 Tue Oct 2 14:15:40 CST 2018 by kube-node2


[root@kube-node3 ~]# cat /tmp/hello.log 
hello,world Tue Oct 2 14:03:39 CST 2018 by kube-node3
RedHat Tue Oct 2 14:10:43 CST 2018 by kube-node3
RedHat Tue Oct 2 14:14:35 CST 2018 by kube-node3
message item1 Tue Oct 2 14:15:38 CST 2018 by kube-node3
message item2 Tue Oct 2 14:15:39 CST 2018 by kube-node3
message item3 Tue Oct 2 14:15:39 CST 2018 by kube-node3
message item4 Tue Oct 2 14:15:39 CST 2018 by kube-node3
message item5 Tue Oct 2 14:15:40 CST 2018 by kube-node3



6、通过Playbook安装apache

yum模块: 
    目的:在指定节点上安装 apache 服务 
    命令:ansible all -m yum -a “state=present name=httpd” 
    state=latest=>>安装最新版本
service模块: 
    目的:启动指定节点上的 httpd 服务,并让其开机自启动
    命令:ansible 10.1.1.113 -m service -a ‘name=httpd state=restarted enabled=yes’


[root@node1 ~]# vi apache.yml 
[root@kube-node3 ~]# cat apache.yml

---
- hosts: testhosts
  remote_user: root
  gather_facts: true
  tasks:
   - name: "install apache on CentOS"
     yum: name=httpd state=present
     when: ansible_os_family =="RedHat"
   - name: "install apache on Debian"
     yum: name=apache2 state=present
     when: ansible_os_family =="Debian"
   - name: "启动Apache,并设置开机启动"
     service: name=httpd state=started enabled=yes


[root@kube-node3 ~]# ansible-playbook apache.yml 

PLAY [testhosts] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
ok: [192.168.0.73]

TASK: [install apache on CentOS] ********************************************** 
changed: [127.0.0.1]
changed: [192.168.0.73]

TASK: [install apache on Debian] ********************************************** 
skipping: [127.0.0.1]
skipping: [192.168.0.73]

TASK: [启动Apache,并设置开机启动] ****************************************************** 
changed: [127.0.0.1]
changed: [192.168.0.73]

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=3    changed=2    unreachable=0    failed=0   
192.168.0.73               : ok=3    changed=2    unreachable=0    failed=0   


验证apache服务是否安装成功

[root@kube-node2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-10-02 14:24:11 CST; 1min 17s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 67034 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   Memory: 2.9M
   CGroup: /system.slice/httpd.service
           ├─67034 /usr/sbin/httpd -DFOREGROUND
           ├─67035 /usr/sbin/httpd -DFOREGROUND
           ├─67036 /usr/sbin/httpd -DFOREGROUND
           ├─67037 /usr/sbin/httpd -DFOREGROUND
           ├─67038 /usr/sbin/httpd -DFOREGROUND
           └─67039 /usr/sbin/httpd -DFOREGROUND

Oct 02 14:24:11 kube-node2 systemd[1]: Starting The Apache HTTP Server...
Oct 02 14:24:11 kube-node2 httpd[67034]: AH00558: httpd: Could not reliably determine the servers fully qualified domain n...message
Oct 02 14:24:11 kube-node2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.


[root@kube-node3 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-10-02 14:24:11 CST; 2min 1s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 64251 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   Memory: 2.8M
   CGroup: /system.slice/httpd.service
           ├─64251 /usr/sbin/httpd -DFOREGROUND
           ├─64256 /usr/sbin/httpd -DFOREGROUND
           ├─64257 /usr/sbin/httpd -DFOREGROUND
           ├─64258 /usr/sbin/httpd -DFOREGROUND
           ├─64260 /usr/sbin/httpd -DFOREGROUND
           └─64261 /usr/sbin/httpd -DFOREGROUND

Oct 02 14:24:11 kube-node3 systemd[1]: Starting The Apache HTTP Server...
Oct 02 14:24:11 kube-node3 httpd[64251]: AH00558: httpd: Could not reliably determine the servers fully qualified domain n...message
Oct 02 14:24:11 kube-node3 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.



通过Playbook卸载apache

[root@kube-node3 ~]# cat apache.yml 

---
- hosts: testhosts
  remote_user: root
  gather_facts: true
  tasks:
   - name: "remove apache on CentOS"
     yum: name=httpd state=removed
     when: ansible_os_family =="RedHat"
   - name: "install apache on Debian"
     yum: name=apache2 state=removed
     when: ansible_os_family =="Debian"
   - name: "卸载apache完毕"
     service: name=httpd state=stopped


[root@kube-node3 ~]# ansible-playbook apache.yml 

PLAY [testhosts] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
ok: [192.168.0.73]

TASK: [remove apache on CentOS] *********************************************** 
changed: [127.0.0.1]
changed: [192.168.0.73]

TASK: [install apache on Debian] ********************************************** 
skipping: [127.0.0.1]
skipping: [192.168.0.73]

TASK: [卸载apache完毕] ************************************************************ 
failed: [127.0.0.1] => {"cmd": "None show None", "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory
failed: [192.168.0.73] => {"cmd": "None show None", "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/root/apache.retry

127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=1   
192.168.0.73               : ok=2    changed=1    unreachable=0    


验证:

[root@kube-node2 ~]# systemctl status httpd
Unit httpd.service could not be found.


[root@kube-node3 ~]# systemctl status httpd
Unit httpd.service could not be found.

 

playbook使用

标签:disable   could not   home   自启动   ansbile   http   操作   director   引用   

原文地址:https://www.cnblogs.com/effortsing/p/10012084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!