【ansible&token】ansible playbook中token的注册和引用

# cat  roles/deploy/tasks/main.yml

#############################################

---

- name: Get token

  shell: >

    curl -ks -X POST \

    -H "Accept:application/vnd.edusoho.v2+json" \

    -H "Authorization: Basic dGVzdDJlZHVvc2hvOjEyMzQ1Ng==" http://demo.edusoho.com/api/tokens  \

    | python -m json.tool | awk -F'"' '{for(i=1;i<=NF;i+=2)$i=""}{print $0}' | grep -w "token" | awk '{ print $2 }'

  register: token

- name: Print Token

  debug:

    msg: "{{ token }}"

- name: Print Token

  debug:

    msg: "{{ token.stdout }}"

- name: Use Token

  shell: >

    curl -X POST \

    -H "Accept:application/vnd.edusoho.v2+json" \

    -H 'X-Auth-Token: {{ token.stdout }}' \

    http://demo.edusoho.com/api/tokens

- name: Set Task ID

  set_fact: task_id="7cf6c71a-df55-4f77-a325-e22d729ef530"

#  curl -X POST \

#  -u admin:admin123 \

#  -H "Content-Type: application/json" \

#  -d '{"action":"coreui_Task","method":"run","data":["7cf6c71a-df55-4f77-a325-e22d729ef530"],"type":"rpc","tid":2020}'  \

#  http://192.168.1.102:8081/service/extdirect

- name: Run Test Task

  shell: >

    curl -X  POST \

    -u admin:admin123 \

    -H "Content-Type: application/json" \

    -d '{"action":"coreui_Task","method":"run","data":["{{ task_id }}"],"type":"rpc","tid":2019}'  \

    http://192.168.1.102:8081/service/extdirect

- name: Run Test Task

  shell: >

    curl -X  POST \

    -u admin:admin123 \

    -H "Content-Type: application/json" \

    -d '{"action":"coreui_Task","method":"run","data":["'"{{ task_id }}"'"],"type":"rpc","tid":2019}'  \

    http://192.168.1.102:8081/service/extdirect

- name: Run Test Task

  shell: >

    curl -X POST \

    -u admin:admin123 \

    -H "Content-Type: application/json"  \

    -d '{"action":"coreui_Task","method":"run","data":[\"{{ task_id }}\"],"type":"rpc","tid":2020}'  \

    http://192.168.1.102:8081/service/extdirect

 

- name: Run Test Task

  shell: >

    curl -X POST

    -H "Authorization: Basic YWRtaW46YWRtaW4xMjM="

    -H "Content-Type: application/json"

    -d '{"action":"coreui_Task","method":"run","data":[\"{{ task_id }}\"],"type":"rpc","tid":2020}'

    http://192.168.1.102:8081/service/extdirect

#############################################

注: 用shell模块的  ">"符号,会开启自动换行的功能,执行的shell命令无需添加换行符,当然添加了也不会报错。

 

【ansible&token】ansible playbook中token的注册和引用

# ansible-playbook -i localhost install.yml

 

【ansible&token】ansible playbook中token的注册和引用

【ansible&token】ansible playbook中token的注册和引用

 

Ansible的高级用法playbook

https://blog.51cto.com/12980155/2384548

 

如何在Ansible中执行多行shell脚本

https://www.thinbug.com/q/40230184

https://www.it-swarm.net/zh/shell/%E5%A6%82%E4%BD%95%E5%9C%A8ansible%E4%B8%AD%E6%89%A7%E8%A1%8C%E5%A4%9A%E8%A1%8C%E5%A4%96%E5%A3%B3%E8%84%9A%E6%9C%AC/827952452

https://mlog.club/article/1897542

 

如果字符串的内容比较长,可以使用">"来折叠换行。

【ansible&token】ansible playbook中token的注册和引用

 

 

How to set variable in the curl command in bash?

https://www.jianshu.com/p/effa0a9c868c

 

ansible中使用register变量

https://www.jianshu.com/p/36d88128d327

 

OpenStack API Documentation

https://docs.openstack.org/zh_CN/api-quick-start/api-quick-start.html

 

Working with Ansible Register Variables

https://www.mydailytutorials.com/ansible-register-variables

 

Register Ansible variable property

https://*.com/questions/37479605/register-ansible-variable-property