1. ansible Management Task Plan
# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
Name specifies the name of the task plan, job specifies what its command is, and then it specifies its time-sharing day, month, week, or *.
[root@yw02 ~]# crontab -l #Ansible: test cron * * * * 6 /bin/touch /tmp/1212.txt
The first line must not be changed, and once it is changed, it cannot be manipulated with this tool.
To delete the cron, just add a field state=absent
# ansible testhost -m cron -a "name='test cron' state=absent"
Other times are minute minutes, hour hours, date day, month month.
2. ansible installs rpm packages and management services
# ansible testhost -m yum -a "name=httpd"
You can also add state=installed/removed after name to install/uninstall
Start the httpd service, using the module tutorial service:
# ansible testhost -m service -a "name=httpd state=started enabled=yes"
Once the service is started, you can see it on the machine at ps aux.
Here, the name is the service name in the centos system, which can be found through chkconfig --list.
Use of Ansible documents
Ansible-doc-l Lists all modules
ansible-doc cron looks at the documentation for the specified module, which is equivalent to the man configuration within the system.
3. playbook using ansible
playbook says it's all about getting all the configurations into one configuration file and executing it.
This is equivalent to writing a module into a configuration file, for example:
#vi / etc/ansible/test.yml //Add the following --- - hosts: yw02 remote_user: root tasks: - name: test_playbook shell: touch /tmp/lishiming.txt
Description: File format, suffix name is yml; first line needs three bars, which is fixed format; hosts parameter specifies which hosts to operate on, if multiple machines can be separated by commas, or host group, defined in / etc/ansible/hosts;
The user parameter specifies which user to use to log on to the remote host operation;
tasks specifies a task, and the name parameter below is also a description of the task, which is printed during execution and the shell is the name of the ansible module.
Execution: ansible-playbook test.yml
PLAY [yw02] ************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************** ok: [yw02] TASK [test_playbook] ***************************************************************************************************************** [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [yw02] PLAY RECAP *************************************************************************************************************************** yw02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4. Variables in playbook
Another example of creating a user is:
#vi/etc/ansible/create_user.yml //Add the following --- - name: create_user hosts: yw02 user: root gather_facts: false vars: - user: "test" tasks: - name: create user user: name="{{ user }}"
Explain:
The name parameter gives an overview of what this playbook implements, and during the subsequent execution, the value of the name variable is printed, which can be omitted.
The gather_facts parameter specifies whether the setup module is executed to get host-related information before the following task sections are executed, which is used later when the task uses setup-acquired information. If this line is not written, facts will be collected by default. If there are too many machines, it is recommended that facts not be collected, which affects the efficiency of execution and also puts pressure on ansible.
vars parameter, specify the variable, specify a user variable here, its value is test, it is important to note that the variable value must be quoted;
The user under tasks specifies the call to the user module, name is a parameter in the user module, and the added user name calls the value of the user variable above, {{user}}=test.
[root@fuxi01 ansible]# ansible-playbook create_user.yml PLAY [create_user] ******************************************************************************************************************* TASK [create user] ******************************************************************************************************************* changed: [yw03] PLAY RECAP *************************************************************************************************************************** yw03 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If the test user already exists, it will not be created, changed=0.