Ansible notes
Hosts: remote host
User: Users who are executing the task group
Remote_user: Same as user
sudo: If set to Yes, the user performing the task group gets root permission when the task is performed.
Sudo_user: If you set user to Tom,sudo as Yes,sudo_user for Jerry, then Tom gets the permissions of the Jerry user.
Connection: How to connect to the remote host.
# # #gather: Collecting Facts: Facts
Gather_facts: Unless you explicitly indicate that you do not need to execute the Setup module on a remote host, it will be performed automatically by default. You can enable this option if you do not really need the variables that the Setup module passes over.
File module
Ansible test-m file-a "Src=/etc/fstab dest=/tmp/fstab state=link" ansible test-m file-a ' Path=/tmp/fstab state=absent ' a Nsible test-m file-a ' path=/tmp/file1 state=touch ' ansible test-m file-a ' Path=/tmp/dir1 state=directory mode=755 owner =root Group=root '
-V or-VVV parameter
Ansible executes an automated script to get detailed information about the execution process.
Ansible test-m ping-vansible test-m ping-vvv# ansible 192.168.121.128:192.168.121.129-m ping-o192.168.121.129 | SUCCESS = {"Changed": false, "ping": "Pong"}192.168.121.128 | SUCCESS = {"Changed": false, "ping": "Pong"}
Copy module:
Ansible test-m copy-a ' src=jdk.yml dest=/tmp '
Ansible adding users in bulk:
echo ansible | The password parameterof the OpenSSL passwd-1-stdin//ansible user needs to accept the encrypted value.
Ansible test-m user-a ' Name=meng password= "$1$nz1s3fva$gzqzd8fzuyrildm0hmqnm/" '-F 5-o
Command
Creates: Specifies a filename when the file exists and the following command will not be executed.
Removes: Specifies a file name when the file does not exist, and subsequent commands do not execute.
[Email protected] ansible]# ansible test-a ' creates=/tmp/22 ls/home ' 192.168.121.129 | SUCCESS | Rc=0 >>skipped, SINCE/TMP/22 exists192.168.121.128 | SUCCESS | Rc=0 >>skipped, SINCE/TMP/22 exists[[email protected] ansible]# ansible test-a ' creates=/tmp/file4 ls/home ' 192.1 68.121.129 | SUCCESS | Rc=0 >>mengody192.168.121.128 | SUCCESS | Rc=0 >>mengody
The command module does not support pipeline filtering.
Ansible test-a ' netstat-tnlp|grep ssh '
Shell Support Pipeline
Ansible test-m shell-a ' netstat-tnlp|grep ssh '
Raw modules similar to shell commands also support pipelines.
Ansible test-m raw-a ' netstat-tnlp|grep ssh '
CHDIR: Switches to the specified directory before executing the command.
Ansible test-a ' chdir=/tmp tar czf file.tar.gz file '
Service Module
Enable=yes|no Boot from Boot
Name: Service names
RunLevel: Run Level
State: Current service's operational status (started,stopped,restarted,reloaded)
Sleep: Performs a restarted operation on the service, sleeping between stop and start for a few seconds.
Pattern: Defines a matching pattern that, if not responded to when the status directive looks at the service state, is found in the process based on the PS command, and if it matches, the service is considered to be still running.
Restart Service:
Ansible test-m raw-a ' name=httpd state=restarted sleep=3 '
Stop service:
Ansible test-m raw-a ' name=httpd state=stopped '
Cron: Scheduling Task management
Backup: Back up before modifying the contents of the original task on the remote host.
Cron_file: If this option is specified, replace the user plan under the CRON.D directory on the remote host with this file
Day: Sun (1-31,*,*/2,)
Hour: Hours (0-23,*,*/2)
minute: minutes (0-59,*,*/2)
Mouth: Month (1-12,*,*/2)
Weekday: Zhou (0-7,*,)
Job: The task to be performed depends on the state=present
Name: Description of the task
Special_time: Specify when to execute, parameters: reboot,yearly,annually,monthly,weekly,daily,hourly
State: Verify that the task schedule is created or deleted.
User: Who will be executing as.
Instance:
To create a scheduled task:
Ansible test-m cron-a ' name= "reboot system" hour=2 user=root job= "/sbin/reboot" '
To view scheduled tasks:
Ansible test-m command-a ' crontab-l '
To delete a scheduled task:
Ansible test-m cron-a ' name= "reboot system" hour=2 user=root job= "/sbin/reboot" State=absent '
FileSystem: Creating a file system on a block device
Options:
Dev: target block device
Force: Forces the creation on a device that already has a file system
Fstype: type of File system
OPTs: Options passed to the MKFS command
Yum Package Management:
Configuration file for Config_file:yum
Disable_gpg_check: Close Gpg_check
Disablerepo: Do not enable a source
Enablerepo: Enable a source
List
Name: You can also pass a URL or a local rpm path for the package to be manipulated.
State: Status (Present,absent,latest)
Instance:
-Name: Install the latest version of Apache YUM:NAME=HTTPD state=latest-name: Remove Apache yum:name=httpd state=absent-name: Install a special version of Apache YUM:NAME=HTTPD-2.2.29-1.4.AMZN1 State=present-name: Upgrade all Packages yum:name=* state=latest-name: Install Nginx yum:na from a remote Yum repository me=http://nginx.org/packages/centos/6/noarch/rpms/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present- Name: Install Nginx yum:name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present-name from the local warehouse: Install the entire development tools-related package yum:name= "@Development Tools" state=present
DD if:
Ansible test-m command-a ' dd if=/dev/zero of=/disk.img bs=4k count=1024 ' ansible-m commmand-a ' Losetup/dev/loop0/disk . IMG '
Syntax validation for Ansible-playbook.
[Email protected] ansible]# Ansible-playbook jdk.yml--syntax-check
Playbook:jdk.yml
Displays the task in the specified playbook.
[Email protected] ansible]# Ansible-playbook jdk.yml--list-task
Playbook:jdk.yml
Play #1 (test): TestTAGS: []
Tasks
jdk:jdk-8u101-linux-x64.tar.gzTAGS: []
Jdk:tar jdk-8u101-linux-x64.tar.gzTAGS: []
Jdk:java_profile_1TAGS: []
Jdk:java_profile_2TAGS: []
Jdk:java profile_3TAGS: []
Jdk:java_4TAGS: []
View hosts for Playbook
[Email protected] ansible]# Ansible-playbook jdk.yml--list-hostsplaybook:jdk.yml play #1 (test): testtags: [] Patte RN: [u ' Test '] hosts (2): 192.168.121.129 192.168.121.128
Specifies that the task executes from the first line.
Ansible-playbook jdk.yml--start-at-task= ' java_profile_1 '
This article is from the "LINUX Super Dream" blog, make sure to keep this source http://215687833.blog.51cto.com/6724358/1886305
Ansible Common Modules