🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# ansible剧本编写规范 [TOC] ## 1 编写剧本规范: ### 1.1 官方链接 http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html 遵循pyyaml语法 ### 1.2 语法说明 ## YAML基础 列表的所有成员都是以相同的缩进级别开头的行,并以(破折号和空格)开头:`"- "` ``` --- # A list of tasty fruits - Apple - Orange - Mango ... ``` 字典以简单的形式表示:`key: value` ``` # An employee record martin: name: Martin D'vloper job: Developer skill: Elite (冒号后面必须有一个空格) ``` 更复杂的混合数据结构 ``` # Employee records - martin: name: Martin D'vloper skills: - python - perl - tabitha: name: Tabitha Bitumen skills: - lisp - fortran ``` YAML示例 ``` name: Martin D'vloper employed: True foods: - Apple - Mango languages: perl: Elite pascal: Lame education: | 4 GCSEs 3 A-Levels ``` 单引号和双引号之间的区别在于,在双引号中可以使用转义符: ``` foo: "a \t TAB and a \n NEWLINE" ``` 变量使用“ {{var}}”。 ``` foo: "{{ variable }}" ``` 补充:必须使用空格分隔ansible剧本级别,一定不要使用tab键进行分割 ### 1.3 剧本执行方法 ```sh ansible-playbook /etc/ansible/ansible-playbook/test.yaml ansible-playbook -C /etc/ansible/ansible-playbook/test.yaml ``` ## 2 语法示例 ### 1.2 rsync一键部署 ```sh [root@xxxx ~]# cat /server/tools/rsync.yml # command play-book - hosts: 172.16.1.41 tasks: - name: step01:install rsync yum: name=rsync state=installed - name: step02:edit rsync conf file copy: src=/etc/ansible/conf/rsync_conf/rsyncd.conf dest=/etc/ - name: step03:create rsync user user: name=rsync state=present createhome=no shell=/sbin/nologin - name: step04:create auth file copy: src=/etc/ansible/conf/rsync_conf/rsync.password dest=/etc/ mode=600 - name: step05:create backup dir file: dest=/backup state=directory owner=rsync group=rsync - name: step06:boot rsync server shell: rsync --daemon creates=/var/run/rsyncd.pid - hosts: 172.16.1.31 tasks: - name: step01:create auth file copy: src=/etc/ansible/conf/rsync_conf/rsync_client.password dest=/etc/rsync.password mode=600 ```