[Playbook script Ansible of] --2019-08-08 17:57:00

Original: http://106.13.73.98/__/150/

@ (Ansible's playbook screenplay)


ymal:

  • List: -
  • Dictionary: Key-value
  • File extension: YML

ansible-playbook command format:

  • -C : - the Check run dry, will not be executed
  • -f : FORKS concurrent execution
  • the Check---syntax : grammar check
  • hosts---list : list Host List


Basic Usage

test.yml script file as follows:

sts: db  # 指定主机
  remote_user: root  # 指定用户
  tasks:  # 任务列表
  - name: create_user  # 任务1
    user: name=user01
  - name: create_group  # 任务2
    group: name=group01

Run the command: ansible-playbook test.yml
will perform the second task when all the machines are complete to perform the first task.
There may be a plurality of scripts, the script between the scripts with a space within a document.

Use parameter passing

- hosts: db
  remote_user: root
  tasks:
  - name: create_user{{ user }}  # !
    user: name={{ user }}  # !

Specifies the command:ansible-playbook -e user=user01 test.yml

Other parameter passing mode


In the / etc / ansible / hosts file transfer parameters, the following example:

[db]
9.0.0.2 user=user02  # user=user02指定参数
9.0.0.3 user=user03

In the hosts write file [groupname: vars]

In playbook by file vars to specify the parameters

By register register parameter passing, when used with a parameter of .stdout value


Analyzing conditions when

Only perform a task only after certain conditions are met, the basic format is as follows:

- hosts: db
  remote_user: root
  tasks:
  - name: 任务1
    动作:动作1
    when: 条件1
  - name: 任务2
    动作: 动作2
    when: 条件2

Examples

test.yml script as follows:

- hosts: db
  remote_user: root
  tasks:
  - name: create_user
    user: name=user01
    when: num == '1'
  - name: create_group
    group: name=group01
    when: num == '2'

Excuting an order:

# 当指定num='1'时,则执行create_user任务
ansible-playbook -e num='1' test.yml

# 当指定num='2'时,则执行create_group任务
ansible-playbook -e num='1' test.yml

We generally use the information collected to do judgment variable, rarely used variables to make a judgment, see below.

Collecting device information control

Collection command:ansible db -m setup

  • ansible_all_ipv4_addresses : ipv4 addresses of all
  • ansible_all_ipv6_addresses : ipv6 addresses of all
  • ansible_architecture : System Architecture
  • ansible_data_time : System Time
  • LANG : System Language
  • ansible_default_ipv4 : the default address ipv4
  • sda : disk information
  • ansible_distribution_file_path family system:
  • ansible_distribution_major_version : System Version
  • ansible_domain : the domain in which
  • ansible_env : Environment Variables
  • ansible_hostname : host name, abbreviated
  • ansible_lvm : lvm disk information
  • ansible_os_family family Information Systems:
  • ansible_processor_cores : the CPU cores
  • ansible_processor_count : number of teeth of the CPU
  • ansible_processor_vcpus : Number of CPU
  • ansible_python : Python Information


The control unit information is determined to do

test.yml file as follows:

- hosts: db
  remote_user: root
  tasks:
  - name: create_user
    user: name=user01
    when: ansible_processor_vcpus==1
  - name: create_group
    group: name=group01
    when: ansible_processor_vcpus==2

Run: ansible-playbook test.yml

may also take a value within a dictionary like this:when ansible_python.version.major==1


Label tags

- hosts: db
  remote_user: root
  tasks:
  - name: start_firewalld
    service: name=firewalld state=started
    tags: start


Template template

- hosts: db
  remote_user: root
  tasks:
    - name: copy_file
      template: dest=/etc/yum.conf src=yum.conf.j2
      # src可以使用相对路径,在当前目录的templates目录里面


Cycle with_item

The basic usage is as follows:

- hosts: db
  remote_user: root
  tasks:
    - name: create_user
      user: name={{ item }}
      with_items:
      - user001
      - user002
      - user003

# 这里将创建3个用户

Nested loop:

- hosts: db
  remote_user: root
  tasks:
    - name: create_user
      user: name={{ item.name }} group={{ item.group }}
      with_items:
      - {name: user001, group: group001}
      - {name: user002, group: group002}
      - {name: user003, group: group003}


handlers

If tasks tasks under execution is successful, the execution handlers tasks under otherwise does:

- hosts: db
  remote_user: root
  tasks:
    - name: create_user
      user: name=user03
  handlers:
    - name: test_handlers
      shell: echo "create user is ok." > /tmp/text.txt

Original: http://106.13.73.98/__/150/

Guess you like

Origin www.cnblogs.com/gqy02/p/11322750.html