(1)编辑配置文件shell
[root@tiejiangSRC1 ~]# cd /etc/ansible/ [root@tiejiangSRC1 ansible]# vim test.yml //固定后缀为yml,必定要注意空格 --- - hosts: testhost user: root tasks: - name: playbook_test shell: touch /tmp/playbook.txt
注意:ubuntu
hosts参数指定了对哪些主机进行参做;vim
user参数指定了使用什么用户登陆远程主机操做;centos
tasks指定了一个任务,其下面的name参数一样是对任务的描述,在执行过程当中会打印出来。bash
(2)执行建立playbook文件文件服务器
[root@tiejiangSRC1 ansible]# ansible-playbook test.yml
PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [playbook_test] *********************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
(3)如今来查看是否批量建立成功playbook.txt
文件。运维
[root@tiejiangSRC1 ~]# ansible testhost -m command -a 'ls -l /tmp/playbook.txt' 192.168.2.73 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 33 4月 19 13:41 /tmp/playbook.txt 192.168.2.71 | SUCCESS | rc=0 >> -rw-r--r--. 1 root root 33 4月 19 13:41 /tmp/playbook.txt 192.168.2.72 | SUCCESS | rc=0 >> -rw-r--r--. 1 root root 33 4月 19 13:41 /tmp/playbook.txt
(4)给建立的playbook批量导入内容,并查看导入的结果ide
[root@tiejiangSRC1 ansible]# vim test.yml --- - hosts: testhost user: root tasks: - name: 铁匠运维网博客 shell: echo "www.tiejiang.org" >> /tmp/playbook.txt [root@tiejiangSRC1 ansible]# ansible-playbook test.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.72] ok: [192.168.2.73] TASK [铁匠运维网博客] ************************************************************* changed: [192.168.2.73] changed: [192.168.2.71] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0 [root@tiejiangSRC1 ansible]# ansible testhost -m command -a 'cat /tmp/playbook.txt' 192.168.2.73 | SUCCESS | rc=0 >> www.tiejiang.org 192.168.2.71 | SUCCESS | rc=0 >> www.tiejiang.org 192.168.2.72 | SUCCESS | rc=0 >> www.tiejiang.org
(1)编辑配置文件oop
[root@tiejiangSRC1 yml]# vim create_user.yml --- - name: create_user hosts: testhost user: root gather_facts: false vars: - user: "tiejiang" tasks: - name: create user user: name="{{ user }}"
name参数对该playbook实现的功能作一个概述,后面执行过程当中,会打印 name变量的值 ,能够省略;ui
gather_facts
参数指定了在如下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;
vars参数指定了变量,这里指字一个user变量,其值为test ,须要注意的是,变量值必定要用引号引住;
user提定了调用user模块,name是user模块里的一个参数,而增长的用户名字调用了上面user变量的值。
(2)执行配置文件
[root@tiejiangSRC1 ansible]# ansible-playbook create_user.yml
PLAY [create_user] ************************************************************* TASK [create user] ************************************************************* changed: [192.168.2.73] changed: [192.168.2.71] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=1 changed=1 unreachable=0 failed=0
(3)查看远程机器的passwd文件,是否建立出来了用户
[root@tiejiangSRC1 ansible]# ansible testhost -m command -a 'grep tiejiang /etc/passwd' 192.168.2.73 | SUCCESS | rc=0 >> tiejiang:x:502:502::/home/tiejiang:/bin/bash 192.168.2.72 | SUCCESS | rc=0 >> tiejiang:x:501:501::/home/tiejiang:/bin/bash 192.168.2.71 | SUCCESS | rc=0 >> tiejiang:x:502:502::/home/tiejiang:/bin/bash
实例:修改/tmp
目录下的1.txthe 2.txt
文件属性
(1)去新建实验文件
[root@tiejiangSRC1 yml]# cat touch1and2.yml --- - hosts: testhost user: root tasks: - name: 建立实验文件 shell: touch /tmp/{1.txt,2.txt} [root@tiejiangSRC1 yml]# ansible-playbook touch1and2.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [建立实验文件] ****************************************************************** changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch changed: [192.168.2.71] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
(2)编辑配置文件
[root@tiejiangSRC1 yml]# cat loop.yml --- - hosts: testhost user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 owner=root group=root with_items: - 1.txt - 2.txt
(3)执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook loop.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.72] ok: [192.168.2.73] TASK [change mode for files] *************************************************** changed: [192.168.2.73] => (item=1.txt) changed: [192.168.2.71] => (item=1.txt) changed: [192.168.2.72] => (item=1.txt) changed: [192.168.2.73] => (item=2.txt) changed: [192.168.2.71] => (item=2.txt) changed: [192.168.2.72] => (item=2.txt) PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
条件判断通常用于针对不一样版本的系统,好比对centos、ubuntu 等系统进行不一样的操做命令。
(1)编辑配置文件
[root@tiejiangSRC1 yml]# vim when.yml --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_default_ipv4.address == "192.168.2.73"
(2)执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook when.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [use when] **************************************************************** skipping: [192.168.2.71] skipping: [192.168.2.72] changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch PLAY RECAP ********************************************************************* 192.168.2.71 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
当咱们执行 tasks 后,服务器发生变化以后咱们要执行一些操做。好比咱们修改了某个服务的配置文件,须要重启下服务。实例以下:
(1)编辑配置文件
[root@tiejiangSRC1 yml]# vim handlers.yml --- - name: handlers test hosts: testhost user: root tasks: - name: test copy copy: src=/etc/passwd dest=/tmp/handlers.txt notify: test handlers handlers: - name: test handlers shell: echo "www.tiejiang.org" >> /tmp/handlers.txt
说明:只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操做,追加内容。也就是说若是 src 和 dest 内容是同样的,并不会去执行 handlers 里面的 shell 相关命令。因此这种比较适合配置文件发生更改后,须要重启服务的操做。
(2)执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook handlers.yml
PLAY [handlers test] *********************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [test copy] *************************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] RUNNING HANDLER [test handlers] ************************************************ changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.72 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.73 : ok=3 changed=2 unreachable=0 failed=0
(3)查看执行结果
[root@tiejiangSRC1 yml]# ansible testhost -m command -a 'tail -n 1 /tmp/handlers.txt ' //这里我直接用-n 1显示handlers.txt的最后一行内容 192.168.2.71 | SUCCESS | rc=0 >> www.tiejiang.org 192.168.2.73 | SUCCESS | rc=0 >> www.tiejiang.org 192.168.2.72 | SUCCESS | rc=0 >> www.tiejiang.org
可查看到 copy 文件成功,同时也执行了 handlers 的相关命令,追加了新的信息。