Automatically add 3 users and ask: a b c
#!/bin/bash #This line is only a comment and has no special meaning for i in a b c;do useradd $i;done
Add password redhat for all users
#!/bin/bash for a in a b c; do echo redhat |passwd --stdin $a ;doneLoop loop of playbook
--- - name: loop loop hosts: all tasks: - name: create group group: name: "{}" #Where item means to find loop loop: - groupname: abc - name: create user user: name: "{}" state: present groups: "{}" uid: "{}" loop: - username: a group: abc uid: 2500 - username: b group: abc uid: 2501 - username: c uid: 2502 group: abcConditional expression of when:
1. Judge the value of the variable
example:
Generate a setup
ansible all -m setup >111.txt
Determine the free cache size of the target host:
--- - name: test hosts: all tasks: - name: Print free cache debug: msg: "{}" when: ansible_swapfree_mb > 2000
2. Judge whether the variable exists
Use "is defined" and "is undefined" to determine whether the variable exists
Example: judge whether there is an sdb disk on the target host. If so, output the size of the sdb disk
msg: "{{ ansible_devices.sdb.size}}" when: ansible_devices.sdb is defined
3. Judge whether the variable is in the specified list
Use "in" to judge
Determine whether the host name is in the mysql group
when ansible_hostname in group.mysqlA complete instance
1. Create a locker.yml first
Create two passwords, as shown below
pw_developer: redhat pw_manager: redhat
2. Create another passwd.txt to store the password of the encrypted locker
snfwelknfwlnfknwefdsif(Just lose)
3. Use the anisble value command to encrypt the locker file
ansible-vault encrypt --vault-password-file=passwd.txt locker.yml
4.vim user_list.yml
aaa: - username: eoe uid: 3600 job: a - username: pop uid: 3691 job: a
5 vim user.yml
--- - name: 111 hosts: all vars_files: - user_list.yml - locker.yml tasks: - name: create group group: name: aaa state: present - name: create user user: name: "{}" state: present uid: "{}" password: "{}" #Using hash algorithm to encrypt pw_manager password loop: "{}" #Call user_ aaa in list.yml when: item.job == 'a' #Judge whether the job value is a. if yes, execute create userhandler trigger
A simple handler trigger example:
--- - name: test hosts: all tasks: - name: install httpd yum: name: httpd state: present - name: start httpd service: name: httpd state: started notify: stopped httpd handlers: - name: stopped httpd #Only the same tasks as the name of notify will be triggered service: name: httpd state: stopped - name: remove httpd #It will not be triggered here because the name is different from notify yum: name: httpd state: removeexception handling
1. Ignore errors
ignore_errors: yes errors can be ignored
2. block
In Ansible, you can use block to define multiple tasks as a block. A block block can be regarded as a group or a whole. We can judge the conditions of the whole block. When the conditions are established, all tasks in the block will be executed. Block can also be used in conjunction with rescue and always. Rescue can catch errors in the tasks in the block. When any task in the block fails, it will execute the tasks in rescue. Always means that any task in always will be executed regardless of whether the task in the block block is successfully executed
Business. "When there is an error in the block, execute rescue"
Example 1:
Judge the requirements of the block block: if there is a disk vdb, create a 4GB partition on the vdb. If there is not enough 4GB space, create a 2GB partition.
Used module parted: disk partition and partition sizing tool
--- - hosts: all tasks: - block: - name: create 4GiB part parted: device: /dev/vdb number: 1 state: present part_end: 4Gib rescue: - name: create 1 GiB part parted: device: /dev/vdb number: 1 part_end: 2GiB state: present when: ansible_devices.vdb is defined
Example 2:
lvol creates, deletes, and dynamically changes the size of logical volumes.
Create a playbook named / home / Devops / adaptive / lv.yml, which will run on all managed nodes to perform the following tasks:
Create logical volumes that meet the following requirements:
Logical volumes are created in the research volume group
The logical volume name is data
The logical volume size is 1500MiB
Format logical volumes using the ext4 file system
If the requested logical volume size cannot be created, the error message Could not create logical volume of that size should be displayed and the size 800Mib should be used instead.
If the volume group research does not exist, the error message Volume group not exist should be displayed. Do not mount logical volumes in any way
Environment construction:
Prepare two virtual machines and add 5G hard disk respectively
Configure the first virtual machine
fdisk /dev/sdc n #establish p #primary Last sector : +840M t #Change partition type 82 #Linux swap t 8e #Linux LVM w #Save exit pvcreate /dev/sdc1 #Create physical volume sdc1 vgcreate research /dev/sdc1 #Create volume group research
The configuration of the second virtual machine is basically the same as that of the first one, except that the size of the primary partition is set to 1600M
Test on any virtual machine with ansible service:
--- - name: 111 hosts: all tasks: - debug: mgs: "Volume group done not exist" when: ansible_lvm.vgs.research is undefined - block: - name: create lv1500 lvol: vg: research lv: data size: 1500M rescue: - debug: msg: "could not create logical volume of that size" - name: create lv800 lvol: vg: research lv: data size: 800M when: ansible_lvm.vgs.research is defined always: - name: create filesystem filesystem: dev: /dev/research/data fstype: ext4