优势:便于功能的重用

本质上就是.yml结尾的文件

遵循YAML语法编写


YAML语法注意事项:

1、一个键对应一个值时      key: value

2、一个键对应多个值时      

key:

  value1

  value2

3、同级别代码,缩进要一样,建议4个空格


- hosts:被管理机

 tasks:

    - name: 操作名称

      模块名称:

           参数1

           参数2

    - name: 操作名称

      模块名称:

           参数1

           参数2


vi userCreate.yml

- hosts: web          

 gather_facts: false         (取消收集数据的行为)

 tasks:

      - name: create user

        user:

            name: hodoop

            shell: /sbin/nologin

:wq


# ansible-playbook userCreate.yml


调用变量的语法:{{变量名称}}


定义变量:

vars


# vi user02.yml

- hosts: web

 vars:

    - username: "AA"

    - sh_name: "/sbin/nologin"

 tasks:

     - name: create user

       user:

           name: "{{ username }}"

           shell: "{{ sh_name }}"

:wq


# ansible-playbook user02.yml


# vi /etc/ansible/hosts

[web:vars]

username="BB"

sh_name="/bin/bash"

:wq


vi user03.yml

- hosts: web

 tasks:

     - name: create user

       user:

           name: "{{ username }}"

           shell: "{{ sh_name }}"

:wq


# ansible-playbook user03.yml

# vi /etc/ansible/hosts

[web]

10.0.1.1  username="CC" sh_name="/bin/bash"

10.0.1.1  username="DD" sh_name="/bin/false"

:wq


外部文件定义变量


# vi userInfo.txt

username: "AA"

sh_name: "/sbin/nologin"

:wq


# vi user04.yml

- hosts: web

 vars_files:

    - /root/userInfo.txt

 tasks:

     - name: create user

       user:

           name: "{{ username }}"

           shell: "{{ sh_name }}"

:wq

# ansible-playbook user04.yml


# ansible-vault  encrypt(加密)   decrypt(解密)

# ansible-vault encrypt userInfo.txt

# ansible-playbook --ask-vault user04.yml

输入加密密码

# ansible-vault decrypt userInfo.txt