Ansible and Playbook. How to convert shell commands into yaml syntax?

蓝咒 提交于 2020-02-03 06:43:24

问题


I'm a newbie in Ansible and I don't understand how all people easily write shell commands in the Ansible/YAML syntax. May be I've missed a page from the documentation where it is explained well.

For example: What do I need to write in my playbook.yml if I want to perform these commands in my remote machines:

sudo apt-get install software-properties-common
sudo apt-key adv –recv-keys –keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://mariadb.biz.net.id//repo/5.5/ubuntu precise main'

I think it would be something like this:

- name: install mariadb
  apt: ...
  sudo: yes

回答1:


As raw shell command modules will do the trick for plain translation of bash scripts. They will rarely end up to be idempotent actions. They can not be run twice without producing errors.

The Ansible way of doing this is to use the appropriate modules, in your case

  • apt_key : add the gpg key
  • apt_repository : install the repository
  • apt : install the package

A sample for mariadb




回答2:


The answer is Ansible Modules!) This is actually what I need. After quick search, I think my commands will be similar to:

-raw: sudo apt-get install software-properties-common
-raw: apt-key adv –recv-keys –keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
 etc..


来源:https://stackoverflow.com/questions/21005290/ansible-and-playbook-how-to-convert-shell-commands-into-yaml-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!