Previous article 1: Creating an Ansible self-made module-Part 1: Life you want to receive arguments- Previous article 2: Creating ansible self-made module-Part 2: Life just wanting to execute commands-
--Made a module template (Part 1) --Received multiple arguments (1) --The argument was combined with the command and executed (Part 2)
-Ansible module development (practice) ――Thank you for your help this time as well.
Also for a review. First, create a template (see "Previous Article 1" for reference). The command execution part created as a test (see the reference "Previous article 2") was once removed.
mkfifo.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import AnsibleModule
#Main processing
#-----------------------------------------------------------
def main():
#AnsibleModule class:Create module
module = AnsibleModule(
#Argument reception
argument_spec=dict(
#argument: data(str type,def:pong)
data=dict(type='str', default='pong'),
),
#Argument check enabled
supports_check_mode=True
)
#Result dict:Create result
result = dict(
# key:The key given as an argument to ping:Store the value of data
ping=module.params['data'],
)
#The contents of result is key=value,Output in the form of
module.exit_json(**result)
if __name__ == '__main__':
main()
Implement the variable receiving part in this.
mkfifo.py
: #(abridgement)
#AnsibleModule class:Create module
module = AnsibleModule(
#Argument reception
argument_spec=dict(
#argument: path(Mandatory,str type)
path=dict(required=True,type='str'),
#argument: owner(str type,Default=root)
owner=dict(type='str',default='root'),
#argument: group(str type,Default=root)
group=dict(type='str',default='root'),
#argument: mode(str type,Default=0644)
mode=dict(type='str',default='0644'),
),
#Argument check enabled
supports_check_mode=True
)
: #(abridgement)
At the moment, there is no branching depending on the OS version or distribution. Just set a changed
variable ... but where do you define changed
? Is it okay outside the `ʻAnsibleModule`` class? Give it a try.
sakrua.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import AnsibleModule
#Main processing
#-----------------------------------------------------------
def main():
#AnsibleModule class:Create module
module = AnsibleModule(
{}
)
changed =False ★ Change this value to True
module.exit_json(changed=changed)
if __name__ == '__main__':
main()
# changed =If True
$ ansible -i test_grp 192.168.56.104 -m sakura -M library -u root
192.168.56.104 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true
}
# changed =If False
$ ansible -i test_grp 192.168.56.104 -m sakura -M library -u root
192.168.56.104 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
Good. It is defined outside the `ʻAnsibleModule`` class.
Changed
The conditions for changed
to be True
are as follows
path
does not existʻower``:
group``:
mode`` given in
`path`` is not as an argumentCondition 1 can be judged by ReturnCode
of test -p xxxxx
.
Condition 2 can be judged by the standard output of stat -c% U:% G:% a
.
However, the result of stat -c% a
only shows the last 3 digits, so use stat -c 0% a
to deal with it.
Also, since stdout
is output up to the line feed code, \ n
was inserted at the time of comparison.
mkfifo.py
: #(abridgement)
#Initialize whether or not there is a change(No change)
changed = False
#Check if there is a named pipe in path with the test command
rc, stdout, stderr = module.run_command("test -p " + module.params['path'])
#Check result
if ( rc != 0 ):
#Since it does not exist, it is subject to change
changed = True
else:
#Owner if it exists:group:Get mode
rc, stdout, stderr = module.run_command("stat -c %U:%G:0%a " + module.params['path'])
#Comparison. The command result is the line feed code at the end\Note that n is attached
if ( stdout != module.params['owner'] + ':' + module.params['group'] + ':' + module.params['mode'] + '\n'):
# owner:group:Since the mode is different, it is subject to change
changed = True
#End
module.exit_json(changed=changed,stdout=stdout,stderr=stderr)
: #(abridgement)
Let's move it up to this point.
$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/testfifo owner=root group=root mode=0777"
192.168.56.104 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
Try to create / tmp / testfifo
on the target server.
# /tmp/testfifo root:root:Let's make it with 0644
$ ssh [email protected] "mkfifo /tmp/testfifo; chmod 0644 /tmp/testfifo"
$ ssh [email protected] "ls -l /tmp/testfifo"
prw-r--r--.1 root root 0 June 1 01:30 2020 /tmp/testfifo
$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/testfifo owner=root group=root mode=0777"
192.168.56.104 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"stderr": "",
"stderr_lines": [],
"stdout": "root:root:0644\n",
"stdout_lines": [
"root:root:0644"
]
}
# /tmp/testfifo root:root:Try to set it to 0777
$ ssh [email protected] "chmod 0777 /tmp/testfifo"
$ ssh [email protected] "ls -l /tmp/testfifo"
prwxrwxrwx.1 root root 0 June 1 01:30 2020 /tmp/testfifo
$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/testfifo owner=root group=root mode=0777"
192.168.56.104 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"stderr": "",
"stderr_lines": [],
"stdout": "root:root:0777\n",
"stdout_lines": [
"root:root:0777"
]
}
Oke. This section ends here. Next is the command input edition. .. The content seems to be thin.
Recommended Posts