用ansible在客户端上创建一个用户,用户名为test,脚本语言如下:
[root@master?ansible]#?vim?1.yml?
---
-?name:?create_user???????????????/说明代码的作用。可以省略
??hosts:?192.168.1.112???????????/指定主机名
??user:?root???????????????????????/指明用户
??gather_facts:?false??????????????/是否获取客户机的相关信息
??vars:????????????????????????????/我们定义一个参数
????-?user:?"test"
??tasks:
????-?name:?create?user????????????
??????user:?name="{{?user?}}"
执行过程:
[root@master?ansible]#?ansible-playbook?1.yml?
PLAY?[create_user]?************************************************************?
skipping:?no?hosts?matched
PLAY?RECAP?********************************************************************?
[root@master?ansible]#?vim?1.yml?
[root@master?ansible]#?ansible-playbook?1.yml?
PLAY?[create_user]?************************************************************?
TASK:?[create?user]?***********************************************************?
changed:?[192.168.1.112]
PLAY?RECAP?********************************************************************?
192.168.1.112??????????????:?ok=1????changed=1????unreachable=0????failed=0???
[root@master?ansible]#
在客户端检查一下:
[root@client?~]#?grep?test?/etc/passwd
test:x:500:500::/home/test:/bin/bash
ansible-playbook中的循环:
---
-?hosts:?192.168.1.112
??user:?root
??tasks:
????-?name:?change?mode?for?file
??????file:?path=/root/{{?item?}}?mode=600???????/这里用到了ansible的一个模块file!
??????with_items:
????????-?1.tst
????????-?2.tst
????????-?3.tst
然后执行一下:
[root@master?ansible]#?ansible-playbook?1.yml?
PLAY?[192.168.1.112]?**********************************************************?
GATHERING?FACTS?***************************************************************?
ok:?[192.168.1.112]
TASK:?[change?mode?for?file]?**************************************************?
changed:?[192.168.1.112]?=>?(item=1.tst)
changed:?[192.168.1.112]?=>?(item=2.tst)
changed:?[192.168.1.112]?=>?(item=3.tst)
PLAY?RECAP?********************************************************************?
192.168.1.112??????????????:?ok=2????changed=1????unreachable=0????failed=0
最后在客户端检查一下,看文件的权限是否已经更改,注意这里的ansible不会自动创建文件,因此要改文件的权限的文件,必须已经存在:
[root@client?~]#?ll
total?48
-rw-------??1?root?root?????0?Mar??9?04:40?1.tst
-rw-------??1?root?root?????0?Mar??9?04:40?2.tst
-rw-------??1?root?root?????0?Mar??9?04:40?3.tst
-rw-------.?1?root?root??1226?Mar??5?05:05?anaconda-ks.cfg
-rw-r--r--.?1?root?root???393?Mar??7?04:37?id_rsa.pub
-rw-r--r--.?1?root?root?26210?Mar??5?05:05?install.log
-rw-r--r--.?1?root?root??7572?Mar??5?05:04?install.log.syslog
[root@client?~]#
可以用playbook实现条件判断的功能:
[root@master?ansible]#?vim?1.yml?
---
-?hosts:?testhosts
??user:?root
??gather_facts:?True????????????????????/抓取系统信息,为条件判断做准备
??tasks:
???-?name:?use?when
?????shell:?touch?/tmp/when.txt
?????when:?ansible_hostname?==?"client"???/运用when这个模块,当这个主机名为client时,创建文件!
~
看一下执行的情况:
[root@master?ansible]#?ansible-playbook?1.yml?
PLAY?[testhosts]?**************************************************************?
GATHERING?FACTS?***************************************************************?
ok:?[127.0.0.1]
ok:?[192.168.1.112]
TASK:?[use?when]?**************************************************************?
skipping:?[127.0.0.1]?????????????/忽略过不符合要求的主机
changed:?[192.168.1.112]
PLAY?RECAP?********************************************************************?
127.0.0.1??????????????????:?ok=1????changed=0????unreachable=0????failed=0???
192.168.1.112??????????????:?ok=2????changed=1????unreachable=0????failed=0
在客户端检查一下,playbook命令的执行的情况:
[root@client?~]#?ll??/tmp/when.txt??/已经创建了文件!
-rw-r--r--?1?root?root?0?Mar??9?05:28?/tmp/when.txt
[root@client?~]#
playbook的handlers应用:
执行完tasks的任务之后,服务器的配置发生了一些变化,但是我们仍需要操作,这时候就可以调用handlers:譬如,我们修改了nginx的配置文件,之后需要它重启,这时候我们就可以调用handlers:
[root@master?ansible]#?vim?1.yml?
---
-?hosts:?192.168.1.112
??name:?handlers?test
??tasks:
????-?name:?copy?file
??????copy:?src=/etc/passwd?dest=/tmp/aaa.txt??????/copy了一个文件,但我们又需要把文件给重定向!
??????notify:?test?handlers??????/notify的作用就是调用handlers模块
??handlers:
????-?name:?test?handlers
??????shell:?echo?"11111"?>?/tmp/aaa.txt
ansible的copy模块和rsync的功能比较像,若目的地址原来就有一个目的文件,ansible会判断内容是否相同,若相同则不会覆盖原来就有的文件,若内容不相同则会完成copy动作!
执行过程:
[root@master?ansible]#?ansible-playbook?1.yml?
PLAY?[handlers?test]?**********************************************************?
GATHERING?FACTS?***************************************************************?
ok:?[192.168.1.112]
TASK:?[copy?file]?*************************************************************?
changed:?[192.168.1.112]
NOTIFIED:?[test?handlers]?*****************************************************?
changed:?[192.168.1.112]
PLAY?RECAP?********************************************************************?
192.168.1.112??????????????:?ok=3????changed=2????unreachable=0????failed=0
在客户端检测一下:是否有文件copy,并且还被重定向!
[root@client?~]#?ll??/tmp/when.txt?
-rw-r--r--?1?root?root?0?Mar??9?05:28?/tmp/when.txt
[root@client?~]#?cat?/tmp/aaa.txt?
11111
[root@client?~]#