In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1 the script is ansible
Playbook translates to "script", and the composition of playbook is as follows
Play: defines the role of the host
Task: defines the specific tasks to be performed
Playbook: consists of one or more play, and a play can contain multiple task tasks
Simply understand: use different modules to accomplish one thing
2 the advantages of the script recalculable
1, which is more powerful than the ansible command
2. Be able to control the sequence of execution and dependencies
3. The syntax is more intuitive.
3Query ansible uses yaml syntax
1) indentation represents the relationship between different levels
2) there are strict requirements for indentation
3)-horizontal bar, followed by a space to represent the list
4): colon, with a space after the colon, indicating assignment
4Diansible script small instance template
Ansible nfs- m group-a "name=www gid=666 state=present" ansible nfs- m user-a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no" ansible nfs- m yum-a "name=nfs-utils" ansible nfs- m file-a "path=/data state=directory" Ansible nfs-m copy-a "src=exports dest=/etc/exports backup=yes" ansible nfs-m service-a "name=rpcbind state=started enabled=yes" ansible nfs-m service-a "name=nfs state=started enabled=yes" ansible nfs-m shell-a "showmount-e"
Try it out:
Small trial Niudao:-hosts: nfs host group tasks: task-name: create group take any name It is convenient to remember group: referenced module name: www parameter 1 gid: 666parameter 2 state: present parameter 3-name: create user user: Name: www uid: 666group: www shell: / sbin/nologin create_home: no-name: install nfs soft yum: name : nfs-utils-name: mkdir directory file: path: / data state: directory-name: copy file copy: src: / root/exports Dest: / etc/exports backup: yes-name: start rpcbind service: name: rpcbind state: started enbaled: yes-name: start nfs Service: name: nfs state: started enbaled: yes-name: showmount shell: showmount-e
5Perfect ansible execution mode
1) ansible-playbook-- syntax-check xxx.yaml syntax checking
2) ansible-playbook-C xxx.yaml simulation execution
3) ansible-playbook xxx.yaml executes the script
6 advanced features of loop anisble screenplay
Usage scenario: in writing ansible scripts, we often write repetitive commands, such as creating multiple users, groups, directories, and installing multiple software.
It is too troublesome to write one by one, and it does not show the superiority of the ansible script. So we need to use some of its advanced features.
-hosts: nfs tasks:-name: create directory file: path: "{{item}}" state: present loop:-/ data -/ dat2-name: add group group: name: "{{item.name}}" gid: "{{item.gid}}" state: present loop:-{name: group1 Gid: 1111}-{name: group2 Gid: 2222}-name: add user user: name: "{{item.name}}" uid: "{{item.uid}}" group: "{{item.group}}" shell: / sbin / nologin create_home: no loop:-{name: user1 Uid: 7777 group: group1}-{name: user2, uid: 8888 group: group2}
6. Advanced features of ansible script-variables
Usage scenarios:
1. Customize a name, which will be referenced multiple times in the task
two。 Extract a variable from the system information collected by the host and reference it, such as network card information
-hosts: nfs var: path: / opt/data tasks:-name: create directory file: path: "{{path}}" state: present
It can also be written on / etc/ansible/hosts
-hsots: nfs tasks:-name: show ip shell: "echo {{ansible_facts.eth2.ipv4.address}} > / root/ip_eth2.txt" shell: "echo {{ansible_facts.eth0.ipv4.address}} > / root/ip_eth0.txt "
(ansible built-in variables extract variables for ip addresses)
Sixth, advanced features of ansible scripts-registered variables
Usage scenario: register the status of the configuration file as a variable to facilitate reference by other tasks
Example:
-hosts: nfs tasks:-name: show ip shell: "echo {{ansible_facts.eth2.ipv4.address}}" register: eth2 (register is a fixed usage Means to register a variable called eth2)-name: echo eth2 debug: msg: "{{eth2.stdout}}" (fixed usage, plus .stdout to show a line with stdout because it happens to have ip)
Sixth, the advanced feature of ansible script-trigger mechanism
Usage scenario: usually, once the configuration file defined in the script is modified, we have to restart the service, but the service module defined by ansible can only state: started
So there should be a trigger mechanism. When the configuration file is modified, the service will restart automatically.
-hosts: nfs tasks:-name: copy export copy: src: / root/exports dest: / etc/exports backup: yes notify: restart_nfs_server handlers:-name: restart_nfs_server service: Name: "{{item}}" state: restarted loop:-rpcbind-nfs
6. Advanced features of ansible script-tag tags
Application scenario: tags each module executed in the script. When you execute, you can flexibly specify which module to execute or execute separately for the module that reports an error, without having to execute it all over again.
-hosts: 172.16.1.41 tasks:-name: 01-add group group: name=www gid=666 tags: 01-add-group-name: 02-add user User: name=www create_home=no shell=/sbin/nologin group=www uid=666 tags: 02-add-user-name: 03-install rsync yum: name=rsync state=installed tags: 03-install-rsync-name: 04-copy rsync conf Copy: src=/server/scripts/rsyncd.conf dest=/etc/ tags: 04-copy-conf-name: 05-create passwd conf copy: content='rsync_backup:oldboy' dest=/etc/rsync.passwd mode=600 Tags: 05-create-passwd-name: 06-create backup dir file: path=/backup state=directory owner=www group=www tags: 06-create-backup-name: 07-create backup dir file: path=/data state=directory owner=www group=www Tags: 07-create-data-name: 08-start rsyncd service service: name=rsyncd state=started tags: 08-start-rsyncd-name: 09-enabled rsyncd service systemd: name=rsyncd Enabled=yes tags: 09-enable
1) specify to run a certain tags:
Ansible-playbook-t 05-create-passwd tags2.yml
2) specify to run multiple tags:
Ansible-playbook-t 05color create talk backup tags2.yml
3) specify that a tags should not be run:
Ansible-playbook-skip-tags=05-create-passwd tags2.yml
4) specify not to run multiple tags:
Ansible-playbook-skip-tags=05-create-passwd,06-create-backup tags2.yml
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.